Skip to content

Commit a257d91

Browse files
committed
Added: Threads example
1 parent 3d68ab1 commit a257d91

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.stacktips.threads;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
class Task implements Runnable {
6+
7+
String service;
8+
CountDownLatch latch;
9+
10+
public Task(String service, CountDownLatch latch) {
11+
this.service = service;
12+
this.latch = latch;
13+
}
14+
15+
@Override
16+
public void run() {
17+
try {
18+
Thread.sleep(1000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
System.out.println(service + " is up");
23+
latch.countDown();
24+
}
25+
26+
}
27+
28+
public class CountDownLatchExample {
29+
30+
public static void main(String[] args) {
31+
CountDownLatch latch = new CountDownLatch(3);
32+
Thread t1 = new Thread(new Task("Service1", latch));
33+
Thread t2 = new Thread(new Task("Service2", latch));
34+
Thread t3 = new Thread(new Task("Service3", latch));
35+
36+
t1.start();
37+
t2.start();
38+
t3.start();
39+
40+
try {
41+
latch.await();
42+
System.out.println("All services are up, Starting Main Application now");
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.stacktips.threads;
2+
3+
import java.util.List;
4+
import java.util.concurrent.BrokenBarrierException;
5+
import java.util.concurrent.CopyOnWriteArrayList;
6+
import java.util.concurrent.CyclicBarrier;
7+
8+
class Task1 implements Runnable {
9+
String name;
10+
List<Integer> numbers;
11+
CyclicBarrier barrier;
12+
13+
public Task1(String name, List<Integer> numbers, CyclicBarrier barrier) {
14+
this.name = name;
15+
this.numbers = numbers;
16+
this.barrier = barrier;
17+
}
18+
19+
@Override
20+
public void run() {
21+
System.out.println(name + " is running");
22+
23+
for (int i = 1; i <= 5; i++) {
24+
numbers.add(i);
25+
}
26+
27+
try {
28+
barrier.await();
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
} catch (BrokenBarrierException e) {
32+
e.printStackTrace();
33+
}
34+
System.out.println(name + " has crossed the barrier");
35+
}
36+
}
37+
38+
class Task2 implements Runnable {
39+
String name;
40+
List<Integer> numbers;
41+
CyclicBarrier barrier;
42+
43+
public Task2(String name, List<Integer> numbers, CyclicBarrier barrier) {
44+
this.name = name;
45+
this.numbers = numbers;
46+
this.barrier = barrier;
47+
}
48+
49+
@Override
50+
public void run() {
51+
System.out.println(name + " is running");
52+
53+
for (int i = 6; i <= 10; i++) {
54+
numbers.add(i);
55+
}
56+
57+
try {
58+
barrier.await();
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
} catch (BrokenBarrierException e) {
62+
e.printStackTrace();
63+
}
64+
System.out.println(name + " has crossed the barrier");
65+
}
66+
}
67+
68+
class FinalTask implements Runnable {
69+
70+
String name;
71+
List<Integer> numbers;
72+
73+
public FinalTask(String name, List<Integer> numbers) {
74+
this.name = name;
75+
this.numbers = numbers;
76+
}
77+
78+
@Override
79+
public void run() {
80+
int sum = 0;
81+
for (Integer i : numbers) {
82+
sum = sum + i;
83+
}
84+
System.out.println("Sum of first 10 natural numbers : " + sum);
85+
}
86+
87+
}
88+
89+
public class CyclicBarrierExample {
90+
91+
public static void main(String[] args) {
92+
List<Integer> numbers = new CopyOnWriteArrayList<>();
93+
CyclicBarrier barrier = new CyclicBarrier(2,
94+
new FinalTask("Final Thread", numbers));
95+
96+
Thread t1 = new Thread(new Task1("First Thread", numbers, barrier));
97+
Thread t2 = new Thread(new Task2("Second Thread", numbers, barrier));
98+
99+
t1.start();
100+
t2.start();
101+
}
102+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.stacktips.threads;
2+
3+
class RunnableExample implements Runnable {
4+
public void run() {
5+
System.out.println("Thread runs...");
6+
}
7+
8+
public static void main(String args[]) {
9+
Thread ib = new Thread(new ThreadExample());
10+
ib.start();
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.stacktips.threads;
2+
3+
class ThreadExample extends Thread {
4+
public void run(){
5+
System.out.println("Thread runs...");
6+
}
7+
public static void main(String args[]){
8+
ThreadExample threadExample = new ThreadExample();
9+
threadExample.start();
10+
}
11+
}

0 commit comments

Comments
 (0)