Skip to content

Commit 15bc850

Browse files
committed
format multithreading code with google plugin
1 parent 278e073 commit 15bc850

File tree

30 files changed

+1367
-1440
lines changed

30 files changed

+1367
-1440
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.yen;
22

33
public class Main {
4-
public static void main(String[] args) {
4+
public static void main(String[] args) {
55

6-
System.out.println("Hello world!");
7-
}
8-
9-
}
6+
System.out.println("Hello world!");
7+
}
8+
}

dev_projects/multithreading/Multithreading/src/main/java/com/yen/atguigu/Callable/course23_24_25/Demo1.java

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,81 @@
55
import java.util.concurrent.FutureTask;
66

77
/**
8-
* Comparison : Callable VS Runnable
9-
*
10-
* - FutureTask example :
11-
* - 1) create a new (not main thread), do sth,
12-
* NOT wait it complete, (Not affect main thread)
13-
* and can get result via "get()" method
14-
*
15-
* - 2) have 3 tasks (run by FutureTask),
16-
* (task 1 completed, task 2 running, task 3 running ..)
17-
* (task 1 completed, task 2 completed, task 3 running ..)
18-
* cam wait till ALL tasks completed
19-
*
8+
* Comparison : Callable VS Runnable
209
*
10+
* <p>- FutureTask example : - 1) create a new (not main thread), do sth, NOT wait it complete, (Not
11+
* affect main thread) and can get result via "get()" method
2112
*
13+
* <p>- 2) have 3 tasks (run by FutureTask), (task 1 completed, task 2 running, task 3 running ..)
14+
* (task 1 completed, task 2 completed, task 3 running ..) cam wait till ALL tasks completed
2215
*/
2316

2417
// course 23 : https://youtu.be/1rOoUmzjPFw?si=ZakPKztzlqy5-9OS&t=314
2518
// course 24 : https://youtu.be/eqHmPDqgXDI?si=oAAHAdNAb0udhnGv
2619
// course 25 : https://youtu.be/1uG5vwYPj1w?si=jQR3fv8WmMYncB_F
2720
// course 26 : https://youtu.be/1uG5vwYPj1w?si=GhSgMys5hV0o1Xb5
2821

29-
class MyThread1 implements Runnable{
30-
31-
@Override
32-
public void run() {
33-
}
22+
class MyThread1 implements Runnable {
3423

24+
@Override
25+
public void run() {}
3526
}
3627

37-
class MyThread2 implements Callable{
38-
39-
// NOTE : call() method has return value
40-
@Override
41-
public Integer call() throws Exception {
42-
System.out.println(Thread.currentThread().getName() + " (callable) ");
43-
return 100;
44-
}
28+
class MyThread2 implements Callable {
4529

30+
// NOTE : call() method has return value
31+
@Override
32+
public Integer call() throws Exception {
33+
System.out.println(Thread.currentThread().getName() + " (callable) ");
34+
return 100;
35+
}
4636
}
4737

4838
public class Demo1 {
4939

50-
public static void main(String[] args) throws ExecutionException, InterruptedException {
40+
public static void main(String[] args) throws ExecutionException, InterruptedException {
5141

52-
/** Runnable */
53-
new Thread(new MyThread1(), "AA").start();
42+
/** Runnable */
43+
new Thread(new MyThread1(), "AA").start();
5444

55-
/** Callable */
56-
// https://youtu.be/1rOoUmzjPFw?si=IlIH_QJuKPStR361&t=513
57-
//new Thread(new MyThread2(), "BB").run(); // No such construct way
45+
/** Callable */
46+
// https://youtu.be/1rOoUmzjPFw?si=IlIH_QJuKPStR361&t=513
47+
// new Thread(new MyThread2(), "BB").run(); // No such construct way
5848

59-
// https://youtu.be/1rOoUmzjPFw?si=MCc2qFwhHsaPv4Pw&t=739
60-
// FutureTask implement Runnable
49+
// https://youtu.be/1rOoUmzjPFw?si=MCc2qFwhHsaPv4Pw&t=739
50+
// FutureTask implement Runnable
6151

62-
// FutureTask can be constructed with Callable :
63-
// V1
64-
FutureTask<Integer> futureTask1 = new FutureTask<>(new MyThread2());
52+
// FutureTask can be constructed with Callable :
53+
// V1
54+
FutureTask<Integer> futureTask1 = new FutureTask<>(new MyThread2());
6555

66-
// V2
67-
// or, can via Lambda expression
68-
FutureTask<Integer> futureTask2 = new FutureTask<>(new Callable<Integer>() {
69-
@Override
70-
public Integer call() throws Exception {
71-
System.out.println(Thread.currentThread().getName() + " futureTask2 come in callable ");
56+
// V2
57+
// or, can via Lambda expression
58+
FutureTask<Integer> futureTask2 =
59+
new FutureTask<>(
60+
new Callable<Integer>() {
61+
@Override
62+
public Integer call() throws Exception {
63+
System.out.println(
64+
Thread.currentThread().getName() + " futureTask2 come in callable ");
7265
return 100;
73-
}
74-
});
75-
76-
// create a thread (with Callable)
77-
new Thread(futureTask2, "c_2").start();
78-
new Thread(futureTask1, "c_1").start();
66+
}
67+
});
7968

80-
// while(!futureTask2.isDone()){
81-
// System.out.println("wait ....");
82-
// }
69+
// create a thread (with Callable)
70+
new Thread(futureTask2, "c_2").start();
71+
new Thread(futureTask1, "c_1").start();
8372

84-
// get() method
85-
System.out.println(futureTask2.get()); // 100
86-
System.out.println(futureTask2.get());
73+
// while(!futureTask2.isDone()){
74+
// System.out.println("wait ....");
75+
// }
8776

88-
System.out.println(futureTask1.get());
77+
// get() method
78+
System.out.println(futureTask2.get()); // 100
79+
System.out.println(futureTask2.get());
8980

90-
System.out.println(Thread.currentThread().getName() + " done !!!");
91-
}
81+
System.out.println(futureTask1.get());
9282

83+
System.out.println(Thread.currentThread().getName() + " done !!!");
84+
}
9385
}

dev_projects/multithreading/Multithreading/src/main/java/com/yen/atguigu/CountDownLatch/course26/Demo1.java

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,63 @@
66

77
// course 26 : https://youtu.be/7kVpV-UxJ8U?si=XsuLW8EwTX0JqDGw
88

9-
109
public class Demo1 {
1110

12-
public static void main(String[] args) throws InterruptedException {
13-
11+
public static void main(String[] args) throws InterruptedException {
1412

15-
/** V1 : without CountDownLatch */
13+
/** V1 : without CountDownLatch */
1614

17-
// all 6 students left classroom -> lock classroom
18-
// https://youtu.be/7kVpV-UxJ8U?si=-ycGvDYMEZUYVk4L&t=372
19-
// for (int i = 0; i < 6; i++){
20-
// new Thread(new Runnable() {
21-
// @Override
22-
// public void run() {
23-
// System.out.println(Thread.currentThread().getName() + " student leave classroom");
24-
// }
25-
// }, String.valueOf(i)).start();
26-
// }
27-
//
28-
// System.out.println(Thread.currentThread().getName() + " LOCK classroom !!!");
15+
// all 6 students left classroom -> lock classroom
16+
// https://youtu.be/7kVpV-UxJ8U?si=-ycGvDYMEZUYVk4L&t=372
17+
// for (int i = 0; i < 6; i++){
18+
// new Thread(new Runnable() {
19+
// @Override
20+
// public void run() {
21+
// System.out.println(Thread.currentThread().getName() + " student leave
22+
// classroom");
23+
// }
24+
// }, String.valueOf(i)).start();
25+
// }
26+
//
27+
// System.out.println(Thread.currentThread().getName() + " LOCK classroom !!!");
2928

30-
/**
31-
* V1 is NOT what we want
32-
* -> Lock classroom is NOT already executed as final step
33-
*
34-
* example log :
35-
* 0 student leave classroom
36-
* 5 student leave classroom
37-
* 2 student leave classroom
38-
* main LOCK classroom !!!
39-
* 4 student leave classroom
40-
* 1 student leave classroom
41-
* 3 student leave classroom
42-
*
43-
*/
29+
/**
30+
* V1 is NOT what we want -> Lock classroom is NOT already executed as final step
31+
*
32+
* <p>example log : 0 student leave classroom 5 student leave classroom 2 student leave
33+
* classroom main LOCK classroom !!! 4 student leave classroom 1 student leave classroom 3
34+
* student leave classroom
35+
*/
4436

45-
/** V2 : with CountDownLatch */
46-
/**
47-
* Steps :
48-
* step 1 : create CountDownLatch instance, and setup init value
49-
* step 2 : update value
50-
* step 3 : wait (block) op
51-
*
52-
* example log:
53-
*
54-
* 5 student leave classroom
55-
* 3 student leave classroom
56-
* 1 student leave classroom
57-
* 2 student leave classroom
58-
* 0 student leave classroom
59-
* 4 student leave classroom
60-
* main LOCK classroom !!!
61-
*
62-
*
63-
*/
64-
// step 1
65-
CountDownLatch countDownLatch = new CountDownLatch(6);
66-
for (int i = 0; i < 6; i++){
67-
new Thread(new Runnable() {
37+
/** V2 : with CountDownLatch */
38+
/**
39+
* Steps : step 1 : create CountDownLatch instance, and setup init value step 2 : update value
40+
* step 3 : wait (block) op
41+
*
42+
* <p>example log:
43+
*
44+
* <p>5 student leave classroom 3 student leave classroom 1 student leave classroom 2 student
45+
* leave classroom 0 student leave classroom 4 student leave classroom main LOCK classroom !!!
46+
*/
47+
// step 1
48+
CountDownLatch countDownLatch = new CountDownLatch(6);
49+
for (int i = 0; i < 6; i++) {
50+
new Thread(
51+
new Runnable() {
6852
@Override
6953
public void run() {
70-
System.out.println(Thread.currentThread().getName() + " student leave classroom");
71-
// step 2
72-
countDownLatch.countDown();
54+
System.out.println(Thread.currentThread().getName() + " student leave classroom");
55+
// step 2
56+
countDownLatch.countDown();
7357
}
74-
}, String.valueOf(i)).start();
75-
}
76-
77-
// step 3
78-
countDownLatch.await();
79-
80-
System.out.println(Thread.currentThread().getName() + " LOCK classroom !!!");
81-
58+
},
59+
String.valueOf(i))
60+
.start();
8261
}
8362

63+
// step 3
64+
countDownLatch.await();
65+
66+
System.out.println(Thread.currentThread().getName() + " LOCK classroom !!!");
67+
}
8468
}

dev_projects/multithreading/Multithreading/src/main/java/com/yen/atguigu/CyclicBarrier/course27/CyclicBarrierDemo.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,43 @@
44

55
// call dragon when collect 7 dragon balls
66

7-
87
import java.util.concurrent.BrokenBarrierException;
98
import java.util.concurrent.CyclicBarrier;
109

1110
public class CyclicBarrierDemo {
1211

13-
private static final int NUMBER = 7;
12+
private static final int NUMBER = 7;
1413

15-
public static void main(String[] args) {
14+
public static void main(String[] args) {
1615

17-
// create CyclicBarrier instance
18-
CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, new Runnable() {
19-
@Override
20-
public void run() {
16+
// create CyclicBarrier instance
17+
CyclicBarrier cyclicBarrier =
18+
new CyclicBarrier(
19+
NUMBER,
20+
new Runnable() {
21+
@Override
22+
public void run() {
2123
System.out.println("--> call dragon!!! (collect 7 dragon balls OK)");
22-
}
23-
});
24+
}
25+
});
2426

25-
// collect dragon ball op
26-
for (int i = 0; i < 7; i ++){
27-
new Thread(() -> {
27+
// collect dragon ball op
28+
for (int i = 0; i < 7; i++) {
29+
new Thread(
30+
() -> {
2831
try {
29-
System.out.println(Thread.currentThread().getName() + " dragon ball is collected");
30-
// await (if not yet collect 7 balls)
31-
cyclicBarrier.await();
32+
System.out.println(
33+
Thread.currentThread().getName() + " dragon ball is collected");
34+
// await (if not yet collect 7 balls)
35+
cyclicBarrier.await();
3236
} catch (InterruptedException e) {
33-
throw new RuntimeException(e);
37+
throw new RuntimeException(e);
3438
} catch (BrokenBarrierException e) {
35-
throw new RuntimeException(e);
39+
throw new RuntimeException(e);
3640
}
37-
}, String.valueOf(i)).start();
38-
}
39-
41+
},
42+
String.valueOf(i))
43+
.start();
4044
}
41-
45+
}
4246
}

0 commit comments

Comments
 (0)