Skip to content

Commit a874c0a

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
Formatted with Google Java Formatter
1 parent b043372 commit a874c0a

9 files changed

+121
-110
lines changed

src/main/java/com/examplehub/basics/thread/DeadLockExample.java

+28-28
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@ public class DeadLockExample {
77
public static void main(String[] args) {
88
DeadLockExample deadLockExample = new DeadLockExample();
99
Runnable runnableA =
10-
() -> {
11-
synchronized (deadLockExample.resourceA) {
12-
System.out.println(Thread.currentThread().getName() + "get resourceA");
13-
try {
14-
Thread.sleep(1000);
15-
} catch (InterruptedException e) {
16-
e.printStackTrace();
17-
}
18-
System.out.println(Thread.currentThread().getName() + " is trying to get resourceB");
19-
synchronized (deadLockExample.resourceB) {
20-
System.out.println(Thread.currentThread().getName() + "get resourceB");
21-
}
22-
}
23-
};
10+
() -> {
11+
synchronized (deadLockExample.resourceA) {
12+
System.out.println(Thread.currentThread().getName() + "get resourceA");
13+
try {
14+
Thread.sleep(1000);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
System.out.println(Thread.currentThread().getName() + " is trying to get resourceB");
19+
synchronized (deadLockExample.resourceB) {
20+
System.out.println(Thread.currentThread().getName() + "get resourceB");
21+
}
22+
}
23+
};
2424

2525
Runnable runnableB =
26-
() -> {
27-
synchronized (deadLockExample.resourceB) {
28-
System.out.println(Thread.currentThread().getName() + "get resourceB");
29-
try {
30-
Thread.sleep(1000);
31-
} catch (InterruptedException e) {
32-
e.printStackTrace();
33-
}
34-
System.out.println(Thread.currentThread().getName() + " is trying to get resourceA");
35-
synchronized (deadLockExample.resourceA) {
36-
System.out.println(Thread.currentThread().getName() + "get resourceA");
37-
}
38-
}
39-
};
26+
() -> {
27+
synchronized (deadLockExample.resourceB) {
28+
System.out.println(Thread.currentThread().getName() + "get resourceB");
29+
try {
30+
Thread.sleep(1000);
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
System.out.println(Thread.currentThread().getName() + " is trying to get resourceA");
35+
synchronized (deadLockExample.resourceA) {
36+
System.out.println(Thread.currentThread().getName() + "get resourceA");
37+
}
38+
}
39+
};
4040

4141
new Thread(runnableA).start();
4242
new Thread(runnableB).start();
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class ReadWriteLockExample {
4-
}
3+
public class ReadWriteLockExample {}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class ReentrantLockConditionExample {
4-
}
3+
public class ReentrantLockConditionExample {}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class ReentrantLockExample {
4-
}
3+
public class ReentrantLockExample {}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class WaitNotifyExample {
4-
}
3+
public class WaitNotifyExample {}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
44

55
import java.util.Arrays;
66
import java.util.concurrent.locks.Lock;
77
import java.util.concurrent.locks.ReadWriteLock;
88
import java.util.concurrent.locks.ReentrantReadWriteLock;
9-
10-
import static org.junit.jupiter.api.Assertions.*;
9+
import org.junit.jupiter.api.Test;
1110

1211
class ReadWriteLockExampleTest {
1312
static class Counter {
@@ -21,7 +20,7 @@ public void inc(int index) {
2120
wLock.lock();
2221
try {
2322
counts[index] += 1;
24-
}finally {
23+
} finally {
2524
wLock.unlock();
2625
}
2726
}
@@ -30,22 +29,25 @@ public int[] get() {
3029
rLock.lock();
3130
try {
3231
return Arrays.copyOf(counts, counts.length);
33-
}finally {
32+
} finally {
3433
rLock.unlock();
3534
}
3635
}
3736
}
37+
3838
@Test
3939
void test() throws InterruptedException {
4040
Counter counter = new Counter();
4141
for (int i = 0; i < 10; i++) {
4242
int finalI = i;
43-
new Thread(()->{
44-
counter.inc(finalI);
45-
System.out.println(Arrays.toString(counter.get()));
46-
}).start();
43+
new Thread(
44+
() -> {
45+
counter.inc(finalI);
46+
System.out.println(Arrays.toString(counter.get()));
47+
})
48+
.start();
4749
}
4850
Thread.sleep(100);
4951
assertEquals("[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]", Arrays.toString(counter.get()));
5052
}
51-
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import java.util.ArrayList;
64
import java.util.LinkedList;
75
import java.util.Queue;
86
import java.util.concurrent.locks.Condition;
97
import java.util.concurrent.locks.ReentrantLock;
8+
import org.junit.jupiter.api.Test;
109

1110
class ReentrantLockConditionExampleTest {
1211
static class TaskQueue {
1312
Queue<String> queue = new LinkedList<>();
1413
ReentrantLock lock = new ReentrantLock();
1514
Condition condition = lock.newCondition();
15+
1616
public void addTask(String task) {
1717
lock.lock();
1818
try {
1919
queue.add(task);
2020
condition.signalAll();
21-
}finally {
21+
} finally {
2222
lock.unlock();
2323
}
2424
}
@@ -30,47 +30,53 @@ public String getTask() throws InterruptedException {
3030
condition.await();
3131
}
3232
return queue.remove();
33-
}finally {
34-
lock.unlock();
33+
} finally {
34+
lock.unlock();
3535
}
3636
}
3737
}
38+
3839
@Test
3940
void test() throws InterruptedException {
4041
var tasks = new TaskQueue();
4142
var threads = new ArrayList<Thread>();
4243
for (int i = 0; i < 5; i++) {
43-
Thread thread = new Thread(()->{
44-
while (true) {
45-
try {
46-
String task = tasks.getTask();
47-
System.out.println("execute: " + task);
48-
} catch (InterruptedException e) {
49-
System.out.println("thread " + Thread.currentThread().getName() + " interrupted" );
50-
return;
51-
}
52-
}
53-
});
44+
Thread thread =
45+
new Thread(
46+
() -> {
47+
while (true) {
48+
try {
49+
String task = tasks.getTask();
50+
System.out.println("execute: " + task);
51+
} catch (InterruptedException e) {
52+
System.out.println(
53+
"thread " + Thread.currentThread().getName() + " interrupted");
54+
return;
55+
}
56+
}
57+
});
5458
thread.start();
5559
threads.add(thread);
5660
}
57-
var addThread = new Thread(()->{
58-
for (int i = 0; i < 10; ++i) {
59-
String task = "task - " + Math.random();
60-
System.out.println("added: " + task);
61-
tasks.addTask(task);
62-
try {
63-
Thread.sleep(100);
64-
} catch (InterruptedException e) {
65-
e.printStackTrace();
66-
}
67-
}
68-
});
61+
var addThread =
62+
new Thread(
63+
() -> {
64+
for (int i = 0; i < 10; ++i) {
65+
String task = "task - " + Math.random();
66+
System.out.println("added: " + task);
67+
tasks.addTask(task);
68+
try {
69+
Thread.sleep(100);
70+
} catch (InterruptedException e) {
71+
e.printStackTrace();
72+
}
73+
}
74+
});
6975
addThread.start();
7076
addThread.join();
7177
Thread.sleep(100);
7278
for (var thread : threads) {
7379
thread.interrupt();
7480
}
7581
}
76-
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
44

55
import java.util.concurrent.locks.Lock;
66
import java.util.concurrent.locks.ReentrantLock;
7-
8-
import static org.junit.jupiter.api.Assertions.*;
7+
import org.junit.jupiter.api.Test;
98

109
class ReentrantLockExampleTest {
1110
static class Counter {
1211
private final Lock lock = new ReentrantLock();
1312
private int count;
13+
1414
public void add(int step) {
1515
lock.lock();
1616
try {
1717
count += step;
18-
}finally {
18+
} finally {
1919
lock.unlock();
2020
}
2121
}
@@ -24,16 +24,18 @@ public int getCount() {
2424
return count;
2525
}
2626
}
27+
2728
@Test
2829
void test() throws InterruptedException {
2930
Counter counter = new Counter();
3031
for (int i = 0; i < 10; i++) {
31-
new Thread(()->{
32-
counter.add(1);
33-
}).start();
34-
32+
new Thread(
33+
() -> {
34+
counter.add(1);
35+
})
36+
.start();
3537
}
3638
Thread.sleep(100);
3739
assertEquals(10, counter.getCount());
3840
}
39-
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import java.util.ArrayList;
64
import java.util.LinkedList;
75
import java.util.Queue;
6+
import org.junit.jupiter.api.Test;
87

98
class WaitNotifyExampleTest {
109
static class TaskQueue {
@@ -22,42 +21,48 @@ public synchronized String getTask() throws InterruptedException {
2221
return queue.remove();
2322
}
2423
}
24+
2525
@Test
2626
void test() throws InterruptedException {
2727
var tasks = new TaskQueue();
2828
var threads = new ArrayList<Thread>();
2929
for (int i = 0; i < 5; i++) {
30-
Thread thread = new Thread(()->{
31-
while (true) {
32-
try {
33-
String task = tasks.getTask();
34-
System.out.println("execute: " + task);
35-
} catch (InterruptedException e) {
36-
System.out.println("thread " + Thread.currentThread().getName() + " interrupted" );
37-
return;
38-
}
39-
}
40-
});
30+
Thread thread =
31+
new Thread(
32+
() -> {
33+
while (true) {
34+
try {
35+
String task = tasks.getTask();
36+
System.out.println("execute: " + task);
37+
} catch (InterruptedException e) {
38+
System.out.println(
39+
"thread " + Thread.currentThread().getName() + " interrupted");
40+
return;
41+
}
42+
}
43+
});
4144
thread.start();
4245
threads.add(thread);
4346
}
44-
var addThread = new Thread(()->{
45-
for (int i = 0; i < 10; ++i) {
46-
String task = "task - " + Math.random();
47-
System.out.println("added: " + task);
48-
tasks.addTask(task);
49-
try {
50-
Thread.sleep(100);
51-
} catch (InterruptedException e) {
52-
e.printStackTrace();
53-
}
54-
}
55-
});
47+
var addThread =
48+
new Thread(
49+
() -> {
50+
for (int i = 0; i < 10; ++i) {
51+
String task = "task - " + Math.random();
52+
System.out.println("added: " + task);
53+
tasks.addTask(task);
54+
try {
55+
Thread.sleep(100);
56+
} catch (InterruptedException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
});
5661
addThread.start();
5762
addThread.join();
5863
Thread.sleep(100);
5964
for (var thread : threads) {
6065
thread.interrupt();
6166
}
6267
}
63-
}
68+
}

0 commit comments

Comments
 (0)