x = 100                                     fix(             x = x -                                  Runnable r =               Foo foo =                                                                         ( i = 0; i < 3; i++                         foo.fix(30                         System.out.println(Thread.currentThread().getName() + " :当前foo对象的x值= " +                       Thread a =  Thread(r, "test-1"         Thread b =  Thread(r, "test-2"     }

 

  1 import java.util.concurrent.Callable;  2 import java.util.concurrent.ExecutionException;  3 import java.util.concurrent.ExecutorService;  4 import java.util.concurrent.Executors;  5 import java.util.concurrent.Future;  6   7   8 /**  9  * 为什么要使用线程池? 为了减少创建和销毁线程的次数,让每个线程可以多次使用,可根据系统情况调整执行的线程数量,防止消耗过多内存,所以我们可以使用线程池. 10  * 
 11  * java中线程池的顶级接口是Executor(e可rai kei 12  * ter),ExecutorService是Executor的子类,也是真正的线程池接口,它提供了提交任务和关闭线程池等方法。调用submit方法提交任务还可以返回一个Future(fei 13  * 曲儿)对象,利用该对象可以了解任务执行情况,获得任务的执行结果或取消任务。 14  * 
 15  * 由于线程池的配置比较复杂,JavaSE中定义了Executors类就是用来方便创建各种常用线程池的工具类。通过调用该工具类中的方法我们可以创建单线程池(newSingleThreadExecutor),固定数量的线程池(newFixedThreadPool),可缓存线程池(newCachedThreadPool),大小无限制的线程池(newScheduledThreadPool),比较常用的是固定数量的线程池和可缓存的线程池,固定数量的线程池是每提交一个任务就是一个线程,直到达到线程池的最大数量,然后后面进入等待队列直到前面的任务完成才继续执行.可缓存线程池是当线程池大小超过了处理任务所需的线程,那么就会回收部分空闲(一般是60秒无执行)的线程,当有任务来时,又智能的添加新线程来执行. 16  * 
 17  * 
 18  * 
 19  * Executors类中还定义了几个线程池重要的参数,比如说int corePoolSize核心池的大小,也就是线程池中会维持不被释放的线程数量.int 20  * maximumPoolSize线程池的最大线程数,代表这线程池汇总能创建多少线程。corePoolSize 21  * :核心线程数,如果运行的线程数少corePoolSize,当有新的任务过来时会创建新的线程来执行这个任务,即使线程池中有其他的空闲的线程。maximumPoolSize:线程池中允许的最大线程数. 22  * 23  */ 24 public class ThreadPool { 25  26      27     public static void main(String[] args) throws InterruptedException, ExecutionException { 28  29         Thread t1 = new Thread(() -> { 30             System.out.println(Thread.currentThread().getName() + " t1"); 31         }); 32  33         Thread t2 = new Thread(() -> { 34             System.out.println(Thread.currentThread().getName() + " t2"); 35         }); 36  37         Thread t3 = new Thread(() -> { 38             System.out.println(Thread.currentThread().getName() + " t3"); 39         }); 40  41         /** 42          * 固定线程池:newFixedThreadPool 43          * 
 44          * 当线程池大小为小于线程个数 
 45          * pool-1-thread-1 t1 
 46          * pool-1-thread-1 t2 
 47          * pool-1-thread-1 t3 48          * 
 49          * 当线程池大小大于等于线程个数 50          * 
 51          * pool-1-thread-2 t2 
 52          * pool-1-thread-3 t3 
 53          * pool-1-thread-1 t1 54          * 
 55          * 对于以上两种连接池,大小都是固定的,当要加入的池的线程(或者任务)超过池最大尺寸时候,则入此线程池需要排队等待。 56          * 一旦池中有线程完毕,则排队等待的某个线程会入池执行。 57          * 
 58          */ 59 //        ExecutorService pool = Executors.newFixedThreadPool(10); 60 //        pool.execute(t1); 61 //        pool.execute(t2); 62 //        pool.execute(t3); 63 //        pool.shutdown(); 64          65         /** 66          * 单任务线程池:newSingleThreadExecutor 67          * 结果: 68          * pool-1-thread-1 t1 69          * pool-1-thread-1 t2 70          * pool-1-thread-1 t3 71          */ 72 //        ExecutorService pool = Executors.newSingleThreadExecutor(); 73 //        pool.execute(t1); 74 //        pool.execute(t2); 75 //        pool.execute(t3); 76 //        pool.shutdown(); 77          78         /** 79          * 以上俩种都是固定大小的线程池 80          * 现在说一下可变大小的线程池: 81          * newCachedThreadPool:根据需要创建线程池 82          * pool-1-thread-1 t1 83          * pool-1-thread-3 t3 84          * pool-1-thread-2 t2 85          */ 86 //        ExecutorService pool = Executors.newCachedThreadPool(); 87 //        pool.execute(t1); 88 //        pool.execute(t2); 89 //        pool.execute(t3); 90 //        pool.shutdown(); 91          92         /** 93          * newScheduledThreadPool:延迟连接池 94          * pool-1-thread-1 t1 95            * pool-1-thread-2 t2 96           * pool-1-thread-1 t3 97          * pool-1-thread-2 t1 98          * pool-1-thread-1 t2 99          * pool-1-thread-2 t3100          */101 //        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);102         //放入线程池中执行103 //        pool.execute(t1);104 //        pool.execute(t2);105 //        pool.execute(t3);106         //使用延迟执行107 //        pool.schedule(t1, 10, TimeUnit.SECONDS);108 //        pool.schedule(t2, 10, TimeUnit.SECONDS);109 //        pool.schedule(t3, 10, TimeUnit.SECONDS);110 //        pool.shutdown();111         112         /**113          * newSingleThreadScheduledExecutor:单任务延迟线程池114          * pool-1-thread-1 t1115          * pool-1-thread-1 t2116          * pool-1-thread-1 t3117          * pool-1-thread-1 t1118          * pool-1-thread-1 t2119          * pool-1-thread-1 t3120          */121 //        ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();122 //        //放入线程池中执行123 //        pool.execute(t1);124 //        pool.execute(t2);125 //        pool.execute(t3);126 //        //使用延迟执行127 //        pool.schedule(t1, 10, TimeUnit.SECONDS);128 //        pool.schedule(t2, 10, TimeUnit.SECONDS);129 //        pool.schedule(t3, 10, TimeUnit.SECONDS);130 //        pool.shutdown();131         132         /**133          * ThreadPoolExecutor:自定义线程池134          */135 //        BlockingQueuebQueue = new ArrayBlockingQueue<>(20);136 //        ThreadPoolExecutor pool = new ThreadPoolExecutor(1,3 , 10, TimeUnit.MILLISECONDS, bQueue);137 //        pool.execute(t1);138 //        pool.execute(t2);139 //        pool.execute(t3);140 //        pool.shutdown();141         /**142          * 在这多说一句:自定义线程池涉及到拒绝策略143          * 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize时,144          * 如果还有任务到来就会采取任务拒绝策略,通常有以下四种策略:145          * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出146          * RejectedExecutionException异常。 
147          * ThreadPoolExecutor.DiscardPolicy:丢弃任务,但是不抛出异常。 
148          * ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务 
149          * ThreadPoolExecutor.CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务 
150          * 
151          * (1)AbortPolicy152          * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。153          * A handler for rejected tasks that throws a {@code RejectedExecutionException}.154          * 这是线程池默认的拒绝策略,在任务不能再提交的时候,抛出异常,及时反馈程序运行状态。如果是比较关键的业务,推荐使用此拒绝策略,这样子在系统不能承载更大的并发量的时候,能够及时的通过异常发现。155          * (2)DiscardPolicy156          * ThreadPoolExecutor.DiscardPolicy:丢弃任务,但是不抛出异常。如果线程队列已满,则后续提交的任务都会被丢弃,且是静默丢弃。157          * A handler for rejected tasks that silently discards therejected task.158          * 使用此策略,可能会使我们无法发现系统的异常状态。建议是一些无关紧要的业务采用此策略。例如,本人的博客网站统计阅读量就是采用的这种拒绝策略。159          * (3)DiscardOldestPolicy160          * ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务。161          * A handler for rejected tasks that discards the oldest unhandled request and then retries {@code execute}, unless the executor is shut down, in which case the task is discarded.162          * 此拒绝策略,是一种喜新厌旧的拒绝策略。是否要采用此种拒绝策略,还得根据实际业务是否允许丢弃老任务来认真衡量。163          * (4)CallerRunsPolicy164          * ThreadPoolExecutor.CallerRunsPolicy:由调用线程处理该任务165          * A handler for rejected tasks that runs the rejected task directly in the calling thread of the {@code execute} method, unless the executor has been shut down, in which case the task is discarded.166          * 如果任务被拒绝了,则由调用线程(提交任务的线程)直接执行此任务167          */168         169         /*170          * 获取线程的返回值:需要实现171          * 结果:A172          */173         Callable callable = new Callable() {174             @Override175             public Object call() throws Exception {176                 return "A";177             }178         };179         ExecutorService pool = Executors.newFixedThreadPool(2); 
180         Future f4 = pool.submit(callable);  
181         //这个获取值有一个执行线程顺序的坑,在异步的时候会出现,记不清啦182         System.out.println(f4.get().toString());183     }184 }

 

  1 import java.util.concurrent.ExecutorService;  2 import java.util.concurrent.Executors;  3 import java.util.concurrent.locks.Lock;  4 import java.util.concurrent.locks.ReentrantLock;  5   6 /**  7  * 在Java5中,专门提供了锁对象,利用锁可以方便的实现资源的封锁,用来控制对竞争资源并发访问的控制,这些内容主要集中在java.util.concurrent.locks包下面,里面有三个重要的接口Condition、Lock、ReadWriteLock。  8  * 
  9  * 
 10  * 
 11  * Condition 12  * 
 13  * Condition将Object监视器方法(wait、notify和 14  * notifyAll)分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待 set(wait-set)。 15  * 
 16  * Lock 17  * 
 18  * Lock实现提供了比使用synchronized方法和语句可获得的更广泛的锁定操作。 19  * 
 20  * ReadWriteLock 21  * 
 22  * ReadWriteLock维护了一对相关的锁定,一个用于只读操作,另一个用于写入操作。 23  * 24  * 
 25  * 切記:锁是针对对象的 26  */ 27 public class ThreadLock { 28  29     /** 30      * 信用卡的用户 31      */ 32     static class User implements Runnable { 33         private String name; // 用户名 34         private MyCount myCount; // 所要操作的账户 35         private int iocash; // 操作的金额,当然有正负之分了 36         private Lock myLock; // 执行操作所需的锁对象 37  38         User(String name, MyCount myCount, int iocash, Lock myLock) { 39             this.name = name; 40             this.myCount = myCount; 41             this.iocash = iocash; 42             this.myLock = myLock; 43         } 44  45         public void run() { 46             // ’获取锁 47             myLock.lock(); 48             myCount.setCash(myCount.getCash() + iocash); 49             // ’执行现金业务 50             System.out.println(name + "正在操作" + myCount + "账户,金额为" + iocash + ",当前金额为" + myCount.getCash()); 51             // ‘释放锁,否则别的线程没有机会执行了 52             myLock.unlock(); 53         } 54     } 55  56     /** 57      * 信用卡账户,可随意透支 58      */ 59     static class MyCount { 60         private String oid; // 账号 61         private int cash; // 账户余额 62  63         MyCount(String oid, int cash) { 64             this.oid = oid; 65             this.cash = cash; 66         } 67  68         public String getOid() { 69             return oid; 70         } 71  72         public void setOid(String oid) { 73             this.oid = oid; 74         } 75  76         public int getCash() { 77             return cash; 78         } 79  80         public void setCash(int cash) { 81             this.cash = cash; 82         } 83  84         @Override 85         public String toString() { 86             return "MyCount{" + "oid='" + oid + '\'' + ", cash=" + cash + '}'; 87         } 88     } 89  90     public static void main(String[] args) { 91         // ’创建并发访问的账户 92         MyCount myCount = new MyCount("95599200901215522", 10000); 93         // ‘创建一个锁对象 94         Lock lock = new ReentrantLock(); 95         // ‘创建一个线程池 96         ExecutorService pool = Executors.newCachedThreadPool(); 97         //’创建一些并发访问用户,一个信用卡,存的存,取的取,好热闹啊 98         User u1 = new User("张三", myCount, -4000, lock); 99         User u2 = new User("张三他爹", myCount, 6000, lock);100         User u3 = new User("张三他弟", myCount, -8000, lock);101         User u4 = new User("张三", myCount, 800, lock);102         // ’在线程池中执行各个用户的操作103         pool.execute(u1);104         pool.execute(u2);105         pool.execute(u3);106         pool.execute(u4);107         // ‘关闭线程池108         pool.shutdown();109     }110 }

 

 1 import java.util.concurrent.ArrayBlockingQueue; 2 import java.util.concurrent.BlockingQueue; 3  4 /* 5  * 阻塞队列是Java5线程新特征中的内容,Java定义了阻塞队列的接口java.util.concurrent.BlockingQueue,阻塞队列的概念是,一个指定长度的队列,如果队列满了,添加新元素的操作会被阻塞等待,直到有空位为止。同样,当队列为空时候,请求队列元素的操作同样会阻塞等待,直到有可用元素为止。 6  7   8  9 有了这样的功能,就为多线程的排队等候的模型实现开辟了便捷通道,非常有用。10 11  12 13 java.util.concurrent.BlockingQueue继承了java.util.Queue接口,可以参看API文档。14 15 另外,阻塞队列还有更多实现类,用来满足各种复杂的需求:ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue,具体的API差别也很小。16  */17 public class ThreadBlockingQueue {18     public static void main(String[] args) throws InterruptedException {19         BlockingQueue bqueue = new ArrayBlockingQueue<>(10);20         for (int i = 0; i < 20; i++) {21             bqueue.put(i);22             System.err.println(i);23         }24         //’可以看出,输出到元素9时候,就一直处于等待状态,因为队列满了,程序阻塞了。25         System.out.println("exit");26     }27 }

 

 1 import java.util.concurrent.BlockingDeque; 2 import java.util.concurrent.LinkedBlockingDeque; 3  4 /** 5  * 对于阻塞栈,与阻塞队列相似。不同点在于栈是“后入先出”的结构,每次操作的是栈顶,而队列是“先进先出”的结构,每次操作的是队列头。 6  * 
 7  * 
 8  * 
 9  * 这里要特别说明一点的是,阻塞栈是Java6的新特征。、10  * 
11  * 
12  * 
13  * Java为阻塞栈定义了接口:java.util.concurrent.BlockingDeque,其实现类也比较多,具体可以查看JavaAPI文档。14  * 
15  * 从上面结果可以看到,程序并没结束,二是阻塞住了,原因是栈已经满了,后面追加元素的操作都被阻塞了。16  */17 public class ThreadBlockingDeque {18 19     public static void main(String[] args) throws InterruptedException {20         BlockingDeque bDeque = new LinkedBlockingDeque<>(10);21         for (int i = 0; i < 20; i++) {22             bDeque.putFirst(i);23             System.err.println(i);24         }25         System.out.println("exit");26     }27 }

 

  1 import java.util.concurrent.ExecutorService;  2 import java.util.concurrent.Executors;  3 import java.util.concurrent.locks.Condition;  4 import java.util.concurrent.locks.Lock;  5 import java.util.concurrent.locks.ReentrantLock;  6   7 /**  8  * 条件变量是Java5线程中很重要的一个概念,顾名思义,条件变量就是表示条件的一种变量。但是必须说明,这里的条件是没有实际含义的,仅仅是个标记而已,并且条件的含义往往通过代码来赋予其含义。  9  * 
 10  * 
 11  * 
 12  * 这里的条件和普通意义上的条件表达式有着天壤之别。 13  * 
 14  * 
 15  * 
 16  * 条件变量都实现了java.util.concurrent.locks.Condition接口,条件变量的实例化是通过一个Lock对象上调用newCondition()方法来获取的,这样,条件就和一个锁对象绑定起来了。因此,Java中的条件变量只能和锁配合使用,来控制并发程序访问竞争资源的安全。 17  * 
 18  * 
 19  * 
 20  * 条件变量的出现是为了更精细控制线程等待与唤醒,在Java5之前,线程的等待与唤醒依靠的是Object对象的wait()和notify()/notifyAll()方法,这样的处理不够精细。 21  * 
 22  * 
 23  * 
 24  * 而在Java5中,一个锁可以有多个条件,每个条件上可以有多个线程等待,通过调用await()方法,可以让线程在该条件下等待。当调用signalAll()方法,又可以唤醒该条件下的等待的线程。有关Condition接口的API可以具体参考JavaAPI文档。 25  * 
 26  * 
 27  * 
 28  * 条件变量比较抽象,原因是他不是自然语言中的条件概念,而是程序控制的一种手段。 29  * 
 30  * 
 31  * 
 32  * 下面以一个银行存取款的模拟程序为例来揭盖Java多线程条件变量的神秘面纱: 33  * 
 34  * 
 35  * 
 36  * 有一个账户,多个用户(线程)在同时操作这个账户,有的存款有的取款,存款随便存,取款有限制,不能透支,任何试图透支的操作都将等待里面有足够存款才执行操作。 37  * 38  */ 39 public class ThreadCondition { 40     static class MyAccount { 41         private String oid; 42         private int cash; 43         private Lock lock = new ReentrantLock(); 44         private Condition _save = lock.newCondition(); 45         private Condition _draw = lock.newCondition(); 46  47         public String getOid() { 48             return oid; 49         } 50  51         public void setOid(String oid) { 52             this.oid = oid; 53         } 54  55         public int getCash() { 56             return cash; 57         } 58  59         public void setCash(int cash) { 60             this.cash = cash; 61         } 62  63         public MyAccount(String oid, int cash) { 64             super(); 65             this.oid = oid; 66             this.cash = cash; 67         } 68  69         public void saving(int x, String name) { 70             lock.lock(); 71             setCash(cash + x); 72             System.out.println(Thread.currentThread().getName() + " name " + name + "x " + x + " ,cash " + cash); 73             _draw.signalAll(); 74             lock.unlock(); 75         } 76  77         public void drawing(int x, String name) { 78             lock.lock(); 79             try { 80                 while (cash - x < 0) { 81                     _draw.await(); 82                     _save.signalAll(); 83                 } 84                 setCash(cash - x); 85                 System.out.println(Thread.currentThread().getName() + " name " + name + "x " + x + " ,cash " + cash); 86             } catch (InterruptedException e) { 87                 // TODO Auto-generated catch block 88                 e.printStackTrace(); 89             } finally { 90                 lock.unlock(); 91             } 92  93         } 94  95     } 96  97     static class SaveThread extends Thread { 98         private String name; 99         private MyAccount myAccount;100         private int x;101 102         public SaveThread(String name, MyAccount myAccount, int x) {103             super();104             this.name = name;105             this.myAccount = myAccount;106             this.x = x;107         }108 109         @Override110         public void run() {111             myAccount.saving(x, name);112         }113 114     }115 116     static class DrawThread extends Thread {117         private String name;118         private MyAccount myAccount;119         private int x;120 121         public DrawThread(String name, MyAccount myAccount, int x) {122             super();123             this.name = name;124             this.myAccount = myAccount;125             this.x = x;126         }127 128         @Override129         public void run() {130             myAccount.drawing(x, name);131         }132     }133 134     public static void main(String[] args) {135         // ‘创建并发访问的账户136         MyAccount myAccount = new MyAccount("95599200901215522", 10000);137         // ’ 创建一个线程池138         ExecutorService pool = Executors.newFixedThreadPool(12);139         Thread t1 = new SaveThread("张三", myAccount, 2000);140         Thread t2 = new SaveThread("李四", myAccount, 3600);141         Thread t3 = new DrawThread("王五", myAccount, 2700);142         Thread t4 = new SaveThread("老张", myAccount, 600);143         Thread t5 = new DrawThread("老牛", myAccount, 1300);144         Thread t6 = new DrawThread("胖子", myAccount, 800);145         // ‘ 执行各个线程146         pool.execute(t1);147         pool.execute(t2);148         pool.execute(t3);149         pool.execute(t4);150         pool.execute(t5);151         pool.execute(t6);152         // ’ 关闭线程池153         pool.shutdown();154     }155 }

 

 1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 import java.util.concurrent.atomic.AtomicLong; 4 import java.util.concurrent.locks.Lock; 5 import java.util.concurrent.locks.ReentrantLock; 6  7 /** 8  * 
 9  * Java线程:新特征-原子量 所谓的原子量即操作变量的操作是“原子的”,该操作不可再分,因此是线程安全的。10  * 
11  * 
12  * 
13  * 为何要使用原子变量呢,原因是多个线程对单个变量操作也会引起一些问题。在Java5之前,可以通过volatile、synchronized关键字来解决并发访问的安全问题,但这样太麻烦。14  * 
15  * Java5之后,专门提供了用来进行单变量多线程并发安全访问的工具包java.util.concurrent.atomic,其中的类也很简单。16  * 
17  * 这里使用了一个对象锁,来控制对并发代码的访问。不管运行多少次,执行次序如何,最终余额均为21000,这个结果是正确的。18  * 
19  * 
20  * 
21  * 有关原子量的用法很简单,关键是对原子量的认识,原子仅仅是保证变量操作的原子性,但整个程序还需要考虑线程安全的。22  */23 public class ThreadAtomic {24 25     static class MyRunnable implements Runnable {26         private AtomicLong atomicLong = new AtomicLong(10000);27 28         private String name; // 操作人29         private int x; // 操作数额30         private Lock lock = new ReentrantLock();31 32         public String getName() {33             return name;34         }35 36         public void setName(String name) {37             this.name = name;38         }39 40         public int getX() {41             return x;42         }43 44         public void setX(int x) {45             this.x = x;46         }47 48         public MyRunnable(String name, int x) {49             super();50             this.name = name;51             this.x = x;52         }53 54         @Override55         public void run() {56             lock.lock();57             System.out.println(Thread.currentThread().getName() + " name: " + name + " x " + x + " 执行啦 "58                     + atomicLong.addAndGet(x));59             lock.unlock();60         }61 62     }63 64     public static void main(String[] args) {65         ExecutorService pool = Executors.newFixedThreadPool(2);66 //           Lock lock = new ReentrantLock(false);67         Runnable t1 = new MyRunnable("张三", 2000);68         Runnable t2 = new MyRunnable("李四", 3600);69         Runnable t3 = new MyRunnable("王五", 2700);70         Runnable t4 = new MyRunnable("老张", 600);71         Runnable t5 = new MyRunnable("老牛", 1300);72         Runnable t6 = new MyRunnable("胖子", 800);73         // 执行各个线程74         pool.execute(t1);75         pool.execute(t2);76         pool.execute(t3);77         pool.execute(t4);78         pool.execute(t5);79         pool.execute(t6);80         // 关闭线程池81         pool.shutdown();82 83     }84 }

 

 1 import java.util.concurrent.BrokenBarrierException; 2 import java.util.concurrent.CyclicBarrier; 3  4 /** 5  * java5中,添加了障碍器类,为了适应一种新的设计需求,比如一个大型的任务,常常需要分配好多子任务去执行,只有当所有子任务都执行完成时候,才能执行主任务,这时候,就可以选择障碍器了。 6  * 所有子任务完成的时候,主任务执行了,达到了控制的目标。 7  */ 8 public class ThreadCyclicBarrier { 9     static class MainTask implements Runnable {10 11         @Override12         public void run() {13             System.out.println("mainTask");14         }15 16     }17 18     static class SubTask extends Thread {19         private CyclicBarrier cb;20         private String name;21 22         public SubTask(String name, CyclicBarrier cb) {23             super();24             this.cb = cb;25             this.name = name;26         }27 28         @Override29         public void run() {30 31             try {32                 System.out.println(name);33                 cb.await();34             } catch (InterruptedException e) {35                 // TODO Auto-generated catch block36                 e.printStackTrace();37             } catch (BrokenBarrierException e) {38                 // TODO Auto-generated catch block39                 e.printStackTrace();40             }41         }42     }43 44     public static void main(String[] args) {45         // 创建障碍器,并设置MainTask为所有定数量的线程都达到障碍点时候所要执行的任务(Runnable)46         CyclicBarrier cb = new CyclicBarrier(7, new MainTask());47         new SubTask("A", cb).start();48         new SubTask("B", cb).start();49         new SubTask("C", cb).start();50         new SubTask("D", cb).start();51         new SubTask("E", cb).start();52         new SubTask("F", cb).start();53         new SubTask("G", cb).start();54     }55 }