Skip to content

Commit 2eb032e

Browse files
committed
advanced java multi threading
1 parent 3cf97a3 commit 2eb032e

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package CoreJava.advancedMultiThreading;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* Created by dheeraj on 7/18/2016.
7+
*/
8+
public class CountDownLatchDemo {
9+
10+
static class Task implements Runnable {
11+
12+
private CountDownLatch countDownLatch;
13+
14+
public Task(CountDownLatch countDownLatch) {
15+
this.countDownLatch = countDownLatch;
16+
}
17+
18+
@Override
19+
public void run() {
20+
try {
21+
Thread.sleep(3000);
22+
countDownLatch.countDown();
23+
countDownLatch.await();
24+
System.out.println("done waiting " + Thread.currentThread().getName());
25+
} catch (Exception e) {
26+
27+
}
28+
}
29+
}
30+
31+
public static void main(String[] strings) {
32+
CountDownLatch countDownLatch = new CountDownLatch(3);
33+
34+
for (int k = 0; k < 3; k++) {
35+
Thread thread = new Thread(new Task(countDownLatch));
36+
thread.setName("num " + k);
37+
thread.start();
38+
}
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package CoreJava.advancedMultiThreading;
2+
3+
import java.util.concurrent.CyclicBarrier;
4+
5+
/**
6+
* Created by dheeraj on 7/18/2016.
7+
*/
8+
9+
public class CyclicBarrierDemo {
10+
11+
static class Task implements Runnable {
12+
13+
private CyclicBarrier cyclicBarrier;
14+
15+
public Task(CyclicBarrier cyclicBarrier) {
16+
this.cyclicBarrier = cyclicBarrier;
17+
}
18+
19+
@Override
20+
public void run() {
21+
try {
22+
Thread.sleep(3000);
23+
cyclicBarrier.await();
24+
System.out.println("done waiting "+Thread.currentThread().getName());
25+
} catch (Exception e) {
26+
27+
}
28+
}
29+
}
30+
31+
public static void main(String[] strings){
32+
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
33+
@Override
34+
public void run() {
35+
System.out.println(Thread.currentThread().getName());
36+
}
37+
});
38+
for(int k=0;k<3;k++){
39+
Thread thread = new Thread(new Task(cyclicBarrier));
40+
thread.setName("num "+k);
41+
thread.start();
42+
}
43+
}
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package CoreJava.advancedMultiThreading;
2+
3+
import java.util.LinkedList;
4+
import java.util.Random;
5+
6+
/**
7+
* Created by dheeraj on 7/18/2016.
8+
*/
9+
public class ProducerConsumer {
10+
11+
private static final int size = 10;
12+
private static LinkedList<Integer> queue = new LinkedList<Integer>();
13+
private static Object lock = new Object();
14+
private static Random random = new Random();
15+
16+
static class Producer implements Runnable {
17+
@Override
18+
public void run() {
19+
while (true) {
20+
synchronized (lock) {
21+
while (queue.size() == size) {
22+
try {
23+
System.out.println("queue full");
24+
lock.wait();
25+
} catch (Exception e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
queue.add(random.nextInt());
30+
lock.notifyAll();
31+
}
32+
}
33+
}
34+
}
35+
36+
static class Consumer implements Runnable {
37+
@Override
38+
public void run() {
39+
while (true) {
40+
try {
41+
Thread.sleep(1000);
42+
} catch (Exception e) {
43+
}
44+
synchronized (lock) {
45+
while (queue.size() == 0) {
46+
try {
47+
lock.wait();
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
System.out.println(queue.pollFirst() + " size = " + queue.size());
53+
lock.notifyAll();
54+
}
55+
}
56+
}
57+
}
58+
59+
public static void main(String[] strings) {
60+
new Thread(new Producer()).start();
61+
new Thread(new Consumer()).start();
62+
new Thread(new Consumer()).start();
63+
new Thread(new Consumer()).start();
64+
new Thread(new Consumer()).start();
65+
new Thread(new Consumer()).start();
66+
new Thread(new Consumer()).start();
67+
68+
}
69+
70+
71+
}

0 commit comments

Comments
 (0)