Samples JDK
Test.java
1 package com.freemindcafe.concurrency.sample5;
2 
3 import java.util.List;
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.locks.Condition;
7 import java.util.concurrent.locks.Lock;
8 import java.util.concurrent.locks.ReentrantLock;
9 import java.util.function.Consumer;
10 
11 public class Test {
12 
13  @org.junit.Test
14  public void test_with_out_coordination() throws Exception{
15  //Semaphore mutex = new Semaphore(1);
16  List<Integer> firstTenInts = new CopyOnWriteArrayList<>();
17 
18  Runnable r1 = () -> {
19  for(int i=0; i <=4; i++){
20  firstTenInts.add(2*i+1);
21  }
22  };
23 
24  Runnable r2 = () -> {
25  for(int i=1; i <=5; i++){
26  firstTenInts.add(2*i);
27  }
28  };
29 
30  Thread t1 = new Thread(r1);
31  Thread t2 = new Thread(r2);
32 
33  t1.start();
34  t2.start();
35 
36  t1.join();
37  t2.join();
38 
39  System.out.println(firstTenInts);
40 
41 
42  }
43 
44  @org.junit.Test
45  public void test_with_coordination_using_semaphore() throws Exception{
46  Semaphore firstOneDone = new Semaphore(0);
47  Semaphore secondOneDone = new Semaphore(0);
48  List<Integer> firstTenInts = new CopyOnWriteArrayList<>();
49 
50  Runnable r1 = () -> {
51  try{
52  for(int i=0; i <=4; i++){
53  //System.out.println("first");
54  firstTenInts.add(2*i+1);
55  firstOneDone.release();
56  secondOneDone.acquire();
57  }
58  }
59  catch(Exception ex){
60  throw new RuntimeException(ex);
61  }
62  };
63 
64  Runnable r2 = () -> {
65  try{
66  for(int i=1; i <=5; i++){
67  //System.out.println("second");
68  firstOneDone.acquire();
69  firstTenInts.add(2*i);
70  secondOneDone.release();
71  }
72  }
73  catch(Exception ex){
74  throw new RuntimeException(ex);
75  }
76  };
77 
78  Thread t1 = new Thread(r1);
79  Thread t2 = new Thread(r2);
80 
81  t1.start();
82  t2.start();
83 
84  t1.join();
85  t2.join();
86 
87  System.out.println(firstTenInts);
88 
89 
90  }
91 
92  //[FIXME] Can we write is using condition vatiables
93  @org.junit.Test
94  public void test_with_coordination_using_condition() throws Exception{
95  Lock lock = new ReentrantLock();
96  Condition firstOneDone = lock.newCondition();
97  Condition secondOneDone = lock.newCondition();
98  List<Integer> firstTenInts = new CopyOnWriteArrayList<>();
99 
100  Runnable r1 = () -> {
101  try{
102  for(int i=0; i <=4; i++){
103  //System.out.println("first");
104  firstTenInts.add(2*i+1);
105  firstOneDone.signal();
106  secondOneDone.wait();
107  }
108  }
109  catch(Exception ex){
110  throw new RuntimeException(ex);
111  }
112  };
113 
114  Runnable r2 = () -> {
115  try{
116  for(int i=1; i <=5; i++){
117  //System.out.println("second");
118  firstOneDone.wait();
119  firstTenInts.add(2*i);
120  secondOneDone.signal();
121  }
122  }
123  catch(Exception ex){
124  throw new RuntimeException(ex);
125  }
126  };
127 
128  Thread t1 = new Thread(r1);
129  Thread t2 = new Thread(r2);
130 
131  t1.start();
132  t2.start();
133 
134  t1.join();
135  t2.join();
136 
137  System.out.println(firstTenInts);
138 
139 
140  } <T> Block<T> exceptionWrappingBlock(Block<T> b) {
141  return () -> {
142  try { b.accept(); }
143  catch (Exception ex) { throw new RuntimeException(ex); }
144  };
145  }
146 
147 }
148 
149 interface Block<T>{
150  void accept() throws Exception;
151 }