zoukankan      html  css  js  c++  java
  • 使用condition 实现线程顺序执行

    书上给的例子都是ABCABC这种,比较简单,复杂点的如A0B0C0, A0A1A2没有,手动实现下,做个记录

    1. A0 A1 A2 A3

     1 public class Demo0 {
     2 
     3     private static volatile  int nextPrintWho = 0;
     4 
     5     public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
     6         ReentrantLock lock = new ReentrantLock();
     7         Condition condition = lock.newCondition();
     8 
     9         Runnable runnable = ()-> {
    10             lock.lock();
    11             String name = Thread.currentThread().getName();
    12             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
    13             while (Demo0.nextPrintWho != index) {
    14                 try {
    15                     condition.await();
    16                 } catch (InterruptedException e) {
    17                     e.printStackTrace();
    18                 }
    19             }
    20             Demo0.nextPrintWho += 1;
    21             println(Thread.currentThread().getName());
    22             try {
    23                 condition.signAll();
    24             }catch (Exception e) {
    25                 e.printStackTrace();
    26             }finally {
    27                 if(lock.isHeldByCurrentThread()) {
    28                     lock.unlock();
    29                 }
    30             }
    31         };
    32         int size = 5;
    33         List<Thread> threadList = new ArrayList<>(size);
    34         for(int i=0; i<size; i++) {
    35             threadList.add(new Thread(runnable,"A" + i));
    36         }
    37         threadList.forEach(i->i.start());
    38     }
    39 
    40     private static void println(Object object) {
    41         System.out.println(object);
    42     }
    43 
    44 }

    2.单condition实现ABC ABC ABC

     1 /**
     2  * @author tele
     3  * @Description     使用condition实现顺序启动线程 ABC ABC ABC
     4  * @create 2019-12-24
     5  */
     6 public class Demo1 {
     7 
     8     private static volatile  int nextPrintWho = 1;
     9 
    10     public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
    11         ReentrantLock lock = new ReentrantLock();
    12         Condition condition = lock.newCondition();
    13 
    14         Runnable runnableA = ()-> {
    15             lock.lock();
    16             while (Demo1.nextPrintWho != 1) {
    17                 try {
    18                     condition.await();
    19                 } catch (InterruptedException e) {
    20                     e.printStackTrace();
    21                 }
    22             }
    23             Demo1.nextPrintWho = 2;
    24             println(Thread.currentThread().getName());
    25             try {
    26                 condition.signAll();
    27             }catch (Exception e) {
    28                 e.printStackTrace();
    29             }finally {
    30                 if(lock.isHeldByCurrentThread()) {
    31                     lock.unlock();
    32                 }
    33             }
    34         };
    35 
    36         Runnable runnableB = ()-> {
    37             lock.lock();
    38             while (Demo1.nextPrintWho != 2) {
    39                 try {
    40                     condition.await();
    41                 } catch (InterruptedException e) {
    42                     e.printStackTrace();
    43                 }
    44             }
    45             Demo1.nextPrintWho = 3;
    46             println(Thread.currentThread().getName());
    47             try {
    48                 condition.signal();
    49             }catch (Exception e) {
    50                 e.printStackTrace();
    51             }finally {
    52                 if(lock.isHeldByCurrentThread()) {
    53                     lock.unlock();
    54                 }
    55             }
    56         };
    57 
    58 
    59         Runnable runnableC = ()-> {
    60             lock.lock();
    61             while (Demo1.nextPrintWho != 3) {
    62                 try {
    63                     condition.await();
    64                 } catch (InterruptedException e) {
    65                     e.printStackTrace();
    66                 }
    67             }
    68             Demo1.nextPrintWho = 1;
    69             println(Thread.currentThread().getName());
    70             try {
    71                 condition.signal();
    72             }catch (Exception e) {
    73                 e.printStackTrace();
    74             }finally {
    75                 if(lock.isHeldByCurrentThread()) {
    76                     lock.unlock();
    77                 }
    78             }
    79         };
    80 
    81         for(int i=0; i<5; i++) {
    82             new Thread(runnableA,"A").start();
    83             new Thread(runnableB,"B").start();
    84             new Thread(runnableC,"C").start();
    85         }
    86 
    87 
    88     }
    89 
    90     private static void println(Object object) {
    91         System.out.println(object);
    92     }
    93 
    94 }

    3.多个condition实现ABC ABC

     1 /**
     2  * @author tele
     3  * @Description     使用多个condition实现顺序启动线程  ABC ABC ABC
     4  * @create 2019-12-24
     5  */
     6 public class Demo2 {
     7 
     8     private static volatile  int nextPrintWho = 1;
     9 
    10     public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
    11         ReentrantLock lock = new ReentrantLock();
    12         Condition conditionA = lock.newCondition();
    13         Condition conditionB = lock.newCondition();
    14         Condition conditionC = lock.newCondition();
    15 
    16         Runnable runnableA = ()-> {
    17             lock.lock();
    18             while (Demo2.nextPrintWho != 1) {
    19                 try {
    20                     conditionA.await();
    21                 } catch (InterruptedException e) {
    22                     e.printStackTrace();
    23                 }
    24             }
    25             Demo2.nextPrintWho = 2;
    26             println(Thread.currentThread().getName());
    27             try {
    28                 conditionB.signal();
    29              }catch (Exception e) {
    30                 e.printStackTrace();
    31             }finally {
    32                 if(lock.isHeldByCurrentThread()) {
    33                     lock.unlock();
    34                 }
    35             }
    36         };
    37 
    38         Runnable runnableB = ()-> {
    39             lock.lock();
    40             while (Demo2.nextPrintWho != 2) {
    41                 try {
    42                     conditionB.await();
    43                 } catch (InterruptedException e) {
    44                     e.printStackTrace();
    45                 }
    46             }
    47             Demo2.nextPrintWho = 3;
    48             println(Thread.currentThread().getName());
    49             try {
    50                 conditionC.signal();
    51             }catch (Exception e) {
    52                 e.printStackTrace();
    53             }finally {
    54                 if(lock.isHeldByCurrentThread()) {
    55                     lock.unlock();
    56                 }
    57             }
    58         };
    59 
    60 
    61         Runnable runnableC = ()-> {
    62             lock.lock();
    63             while (Demo2.nextPrintWho != 3) {
    64                 try {
    65                     conditionC.await();
    66                 } catch (InterruptedException e) {
    67                     e.printStackTrace();
    68                 }
    69             }
    70             Demo2.nextPrintWho = 1;
    71             println(Thread.currentThread().getName());
    72             try {
    73                 conditionA.signal();
    74             }catch (Exception e) {
    75                 e.printStackTrace();
    76             }finally {
    77                 if(lock.isHeldByCurrentThread()) {
    78                     lock.unlock();
    79                 }
    80             }
    81         };
    82 
    83         for(int i=0; i<5; i++) {
    84             new Thread(runnableA,"A").start();
    85             new Thread(runnableB,"B").start();
    86             new Thread(runnableC,"C").start();
    87         }
    88 
    89 
    90     }
    91 
    92     private static void println(Object object) {
    93         System.out.println(object);
    94     }
    95 
    96 }

    4.A0 B0 C0 A1 B1 C1 A2 B2 C2

      1 /**
      2  * @author tele
      3  * @Description     使用condition实现顺序启动线程  A0 B0 C0 A1 B1 C1 A2 B2 C2
      4  * @create 2019-12-24
      5  */
      6 public class Demo3 {
      7 
      8     private static volatile  int nextPrintWho = 0;
      9 
     10     private static volatile int order = 1;
     11 
     12     private static final int ORDER_A = 1;
     13 
     14     private static final int ORDER_B = 2;
     15 
     16     private static final int ORDER_C = 3;
     17 
     18     public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
     19         ReentrantLock lock = new ReentrantLock();
     20         Condition conditionA = lock.newCondition();
     21         Condition conditionB = lock.newCondition();
     22         Condition conditionC = lock.newCondition();
     23 
     24         Runnable runnableA = ()-> {
     25             lock.lock();
     26             String name = Thread.currentThread().getName();
     27 
     28             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     29 
     30             while (!name.contains("A") || Demo3.nextPrintWho != index || Demo3.order != Demo3.ORDER_A) {
     31                 try {
     32                     conditionA.await();
     33                 } catch (InterruptedException e) {
     34                     e.printStackTrace();
     35                 }
     36             }
     37 
     38             Demo3.order = Demo3.ORDER_B;
     39             println(Thread.currentThread().getName());
     40             try {
     41                 conditionB.signalAll();
     42             }catch (Exception e) {
     43                 e.printStackTrace();
     44             }finally {
     45                 if(lock.isHeldByCurrentThread()) {
     46                     lock.unlock();
     47                 }
     48             }
     49         };
     50 
     51         Runnable runnableB = ()-> {
     52             lock.lock();
     53             String name = Thread.currentThread().getName();
     54 
     55             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     56 
     57             while (!name.contains("B") || Demo3.nextPrintWho != index || Demo3.order != Demo3.ORDER_B) {
     58                 try {
     59                     conditionB.await();
     60                 } catch (InterruptedException e) {
     61                     e.printStackTrace();
     62                 }
     63             }
     64             Demo3.order = Demo3.ORDER_C;
     65             println(Thread.currentThread().getName());
     66             try {
     67                 conditionC.signalAll();
     68             }catch (Exception e) {
     69                 e.printStackTrace();
     70             }finally {
     71                 if(lock.isHeldByCurrentThread()) {
     72                     lock.unlock();
     73                 }
     74             }
     75         };
     76 
     77 
     78         Runnable runnableC = ()-> {
     79             lock.lock();
     80             String name = Thread.currentThread().getName();
     81             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     82             while (!name.contains("C") || Demo3.nextPrintWho != index || Demo3.order != Demo3.ORDER_C) {
     83                 try {
     84                     conditionC.await();
     85                 } catch (InterruptedException e) {
     86                     e.printStackTrace();
     87                 }
     88             }
     89             Demo3.nextPrintWho += 1;
     90             Demo3.order = Demo3.ORDER_A;
     91             println(Thread.currentThread().getName());
     92             try {
     93                 conditionA.signalAll();
     94             }catch (Exception e) {
     95                 e.printStackTrace();
     96             }finally {
     97                 if(lock.isHeldByCurrentThread()) {
     98                     lock.unlock();
     99                 }
    100             }
    101         };
    102 
    103         for(int i=0; i<5; i++) {
    104             new Thread(runnableA,"A" + i).start();
    105             new Thread(runnableB,"B" + i).start();
    106             new Thread(runnableC,"C" + i).start();
    107         }
    108 
    109 
    110     }
    111 
    112     private static void println(Object object) {
    113         System.out.println(object);
    114     }
    115 
    116 }

    5.A0 A1 A2 A3 B0 B1 B2 B3

      1 /**
      2  * @author tele
      3  * @Description     使用condition实现顺序启动线程 A0 A1 A2 A3 B0 B1 B2 B3
      4  * @create 2019-12-24
      5  */
      6 public class Demo4 {
      7 
      8     private static volatile  int nextPrintWho = 0;
      9 
     10     private static volatile int order = 1;
     11 
     12     private static final int LIST_SIZE = 5;
     13 
     14     private static final int ORDER_A = 1;
     15 
     16     private static final int ORDER_B = 2;
     17 
     18     private static final int ORDER_C = 3;
     19 
     20     public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
     21         ReentrantLock lock = new ReentrantLock();
     22         Condition condition = lock.newCondition();
     23         Runnable runnableA = ()-> {
     24             lock.lock();
     25             String name = Thread.currentThread().getName();
     26 
     27             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     28 
     29             while (!name.contains("A") || Demo4.nextPrintWho != index || Demo4.order != Demo4.ORDER_A) {
     30                 try {
     31                     condition.await();
     32                 } catch (InterruptedException e) {
     33                     e.printStackTrace();
     34                 }
     35             }
     36             Demo4.nextPrintWho += 1;
     37             if(Demo4.nextPrintWho == LIST_SIZE) {
     38                 Demo4.order = Demo4.ORDER_B;
     39                 Demo4.nextPrintWho = 0;
     40             }
     41             println(Thread.currentThread().getName());
     42             try {
     43                 condition.signalAll();
     44             }catch (Exception e) {
     45                 e.printStackTrace();
     46             }finally {
     47                 if(lock.isHeldByCurrentThread()) {
     48                     lock.unlock();
     49                 }
     50             }
     51         };
     52 
     53         Runnable runnableB = ()-> {
     54             lock.lock();
     55             String name = Thread.currentThread().getName();
     56 
     57             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     58 
     59             while (!name.contains("B") || Demo4.nextPrintWho != index || Demo4.order != Demo4.ORDER_B) {
     60                 try {
     61                     condition.await();
     62                 } catch (InterruptedException e) {
     63                     e.printStackTrace();
     64                 }
     65             }
     66             Demo4.nextPrintWho += 1;
     67             if(Demo4.nextPrintWho == LIST_SIZE) {
     68                 Demo4.order = Demo4.ORDER_C;
     69                 Demo4.nextPrintWho = 0;
     70             }
     71             println(Thread.currentThread().getName());
     72             try {
     73                 condition.signalAll();
     74             }catch (Exception e) {
     75                 e.printStackTrace();
     76             }finally {
     77                 if(lock.isHeldByCurrentThread()) {
     78                     lock.unlock();
     79                 }
     80             }
     81         };
     82 
     83         Runnable runnableC = ()-> {
     84             lock.lock();
     85             String name = Thread.currentThread().getName();
     86             final int index = Integer.parseInt(name.substring(name.length() - 1, name.length()));
     87             while (!name.contains("C") || Demo4.nextPrintWho != index || Demo4.order != Demo4.ORDER_C) {
     88                 try {
     89                     condition.await();
     90                 } catch (InterruptedException e) {
     91                     e.printStackTrace();
     92                 }
     93             }
     94             Demo4.order = Demo4.ORDER_C;
     95             Demo4.nextPrintWho += 1;
     96             println(Thread.currentThread().getName());
     97             try {
     98                 condition.signalAll();
     99             }catch (Exception e) {
    100                 e.printStackTrace();
    101             }finally {
    102                 if(lock.isHeldByCurrentThread()) {
    103                     lock.unlock();
    104                 }
    105             }
    106         };
    107 
    108         List<Thread> threadAList = new ArrayList<>(LIST_SIZE);
    109         List<Thread> threadBList = new ArrayList<>(LIST_SIZE);
    110         List<Thread> threadCList = new ArrayList<>(LIST_SIZE);
    111 
    112         for(int i=0; i<LIST_SIZE; i++) {
    113             threadAList.add(new Thread(runnableA,"A" + i));
    114             threadBList.add(new Thread(runnableB,"B" + i));
    115             threadCList.add(new Thread(runnableC,"C" + i));
    116         }
    117 
    118         threadAList.forEach(i->i.start());
    119         threadBList.forEach(i->i.start());
    120         threadCList.forEach(i->i.start());
    121 
    122 
    123     }
    124 
    125     private static void println(Object object) {
    126         System.out.println(object);
    127     }
    128 
    129 }
  • 相关阅读:
    Java web实验 Login.jsp session属性设置和获取
    Java web实验Outapp.jsp
    Java web实验Accept.jsp
    Java web实验Register.jsp
    Java web实验 Redirect.jsp
    requestAPP1.jps
    out对象
    Request获取请求路径方法介绍
    数据流图
    web应用程序的请求和响应
  • 原文地址:https://www.cnblogs.com/tele-share/p/12106904.html
Copyright © 2011-2022 走看看