zoukankan      html  css  js  c++  java
  • Java模拟并发

    =========================one=============================

    public class Bingfa {

        public static void main(String[] args) throws InterruptedException {            
            // 锁住所有线程,等待并发执行  
            final CountDownLatch begin = new CountDownLatch(1);      
            final ExecutorService exec = Executors.newFixedThreadPool(10);    
     
            for (int index = 0; index < 100; index++)   
            {  
                final int NO = index + 1;                 
                Runnable run = new Runnable()   
                {  
                    public void run() {    
                        try {    
                            // 等待,所有一起执行  
                            begin.await();  
                            //*****执行程序去********//

              ????????????

                          //*****执行程序去********//
                        } catch (InterruptedException e)   
                        {    
                            e.printStackTrace();  
                        }   
                        finally {    
                        }    
                    }    
                };    
                exec.submit(run);  
            }    
              
            System.out.println("开始执行");    
            // begin减一,开始并发执行  
            begin.countDown();              
            //关闭执行  
            exec.shutdown();    
        }
    }

    ==========================two==================================

     public class CountdownLatchTest {  
     
        public static void main(String[] args) {  
            ExecutorService service = Executors.newCachedThreadPool(); //创建一个线程池  
            final CountDownLatch cdOrder = new CountDownLatch(1);//构造方法参数指定计数的次数 
            final CountDownLatch cdAnswer = new CountDownLatch(3);//构造方法参数指定计数的次数 
            for(int i=0;i<3;i++){  
                Runnable runnable = new Runnable(){  
                    public void run(){  
                        try {  
                            System.out.println("线程" + Thread.currentThread().getName() +  
                                    "正准备接受命令");  
                            cdOrder.await(); //战士们都处于等待命令状态  
                            System.out.println("线程" + Thread.currentThread().getName() +  
                                    "已接受命令");  
                            Thread.sleep((long)(Math.random()*10000));  
                            System.out.println("线程" + Thread.currentThread().getName() +  
                                    "回应命令处理结果");  
     
                        } catch (Exception e) {  
                            e.printStackTrace();  
                        } finally {  
                            cdAnswer.countDown(); //任务执行完毕,返回给指挥官,cdAnswer减1。  
                        }  
                    }  
                };  
                service.execute(runnable);//为线程池添加任务  
            }  
            try {  
                Thread.sleep((long)(Math.random()*10000));  
     
                System.out.println("线程" + Thread.currentThread().getName() +  
                        "即将发布命令");  
                cdOrder.countDown(); //发送命令,cdOrder减1,处于等待的战士们停止等待转去执行任务。  
                System.out.println("线程" + Thread.currentThread().getName() +  
                        "已发送命令,正在等待结果");  
                cdAnswer.await(); //命令发送后指挥官处于等待状态,一旦cdAnswer为0时停止等待继续往下执行  
                System.out.println("线程" + Thread.currentThread().getName() +  
                        "已收到所有响应结果");  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
            }  
            service.shutdown(); //任务结束,停止线程池的所有线程  
        }  
    }

    转载:http://blog.csdn.net/zhao9tian/article/details/40346899

  • 相关阅读:
    云计算openstack核心组件——keystone身份认证服务(5)
    分布式存储ceph——(6)ceph 讲解
    分布式存储ceph——(5)ceph osd故障硬盘更换
    分布式存储ceph——(4)ceph 添加/删除osd
    kvm虚拟化介绍(1)
    kvm虚拟机管理(2)
    云计算openstack共享组件——Memcache 缓存系统(4)
    云计算openstack共享组件——消息队列rabbitmq(3)
    妹子UI-yepnope.js使用详解及示例分享(异步加载数据)
    前端组件整理
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/6094927.html
Copyright © 2011-2022 走看看