Skip to content

Commit f9667cf

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

File tree

7 files changed

+56
-44
lines changed

7 files changed

+56
-44
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class InterruptedThread {
4-
}
3+
public class InterruptedThread {}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package com.examplehub.basics.thread;
22

3-
public class VolatileExample {
4-
}
3+
public class VolatileExample {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class YieldThread implements Runnable {
44

55
@Override
66
public void run() {
7-
// System.out.println(Thread.currentThread().getName() + " is running");
7+
// System.out.println(Thread.currentThread().getName() + " is running");
88
Thread.yield();
99
System.out.println(Thread.currentThread().getName() + " is stopped");
1010
}

src/test/java/com/examplehub/basics/thread/InterruptedThreadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class InterruptedThreadTest {
77
@Test
88
@Disabled
99
void test() throws InterruptedException {
10-
//TODO
10+
// TODO
1111
class MyThread extends Thread {
1212
@Override
1313
public void run() {
@@ -25,4 +25,4 @@ public void run() {
2525
thread.join();
2626
System.out.println(Thread.currentThread().getName() + " stopped");
2727
}
28-
}
28+
}

src/test/java/com/examplehub/basics/thread/JoinThreadTest.java

Lines changed: 11 additions & 10 deletions
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 static org.junit.jupiter.api.Assertions.*;
64

75
import java.util.concurrent.atomic.AtomicInteger;
6+
import org.junit.jupiter.api.Test;
87

98
class JoinThreadTest {
109
@Test
@@ -22,14 +21,16 @@ void test() throws InterruptedException {
2221
@Test
2322
void testJoinWithTime() throws InterruptedException {
2423
AtomicInteger number = new AtomicInteger();
25-
Thread thread = new Thread(()->{
26-
try {
27-
Thread.sleep(100);
28-
number.set(1);
29-
} catch (InterruptedException e) {
30-
e.printStackTrace();
31-
}
32-
});
24+
Thread thread =
25+
new Thread(
26+
() -> {
27+
try {
28+
Thread.sleep(100);
29+
number.set(1);
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
});
3334
thread.start();
3435
thread.join(50);
3536
assertEquals(0, number.get());

src/test/java/com/examplehub/basics/thread/SynchronizedThreadTest.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import static org.junit.jupiter.api.Assertions.*;
64

5+
import org.junit.jupiter.api.Test;
6+
77
class Counter {
88
public static final Object lock = new Object();
99
public static int count = 0;
@@ -56,11 +56,13 @@ public void run() {
5656
void testSynchronizedThis() throws InterruptedException {
5757
class Counter {
5858
private int count = 0;
59+
5960
public void dec() {
6061
synchronized (this) {
6162
count = count - 1;
6263
}
6364
}
65+
6466
public void add() {
6567
synchronized (this) {
6668
count = count + 1;
@@ -72,16 +74,20 @@ public int getCount() {
7274
}
7375
}
7476
Counter counter = new Counter();
75-
var firstThread = new Thread(() -> {
76-
for (int i = 0; i < 100; i++) {
77-
counter.add();
78-
}
79-
});
80-
var secondThread = new Thread(() -> {
81-
for (int i = 0; i < 100; i++) {
82-
counter.dec();
83-
}
84-
});
77+
var firstThread =
78+
new Thread(
79+
() -> {
80+
for (int i = 0; i < 100; i++) {
81+
counter.add();
82+
}
83+
});
84+
var secondThread =
85+
new Thread(
86+
() -> {
87+
for (int i = 0; i < 100; i++) {
88+
counter.dec();
89+
}
90+
});
8591
firstThread.start();
8692
secondThread.start();
8793
firstThread.join();
@@ -93,28 +99,34 @@ public int getCount() {
9399
void testSynchronizedFunction() throws InterruptedException {
94100
class Counter {
95101
private int count = 0;
102+
96103
public synchronized void dec() {
97-
count = count - 1;
104+
count = count - 1;
98105
}
106+
99107
public synchronized void add() {
100-
count = count + 1;
108+
count = count + 1;
101109
}
102110

103111
public int getCount() {
104112
return count;
105113
}
106114
}
107115
Counter counter = new Counter();
108-
var firstThread = new Thread(() -> {
109-
for (int i = 0; i < 100; i++) {
110-
counter.add();
111-
}
112-
});
113-
var secondThread = new Thread(() -> {
114-
for (int i = 0; i < 100; i++) {
115-
counter.dec();
116-
}
117-
});
116+
var firstThread =
117+
new Thread(
118+
() -> {
119+
for (int i = 0; i < 100; i++) {
120+
counter.add();
121+
}
122+
});
123+
var secondThread =
124+
new Thread(
125+
() -> {
126+
for (int i = 0; i < 100; i++) {
127+
counter.dec();
128+
}
129+
});
118130
firstThread.start();
119131
secondThread.start();
120132
firstThread.join();

src/test/java/com/examplehub/basics/thread/VolatileExampleTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.examplehub.basics.thread;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import static org.junit.jupiter.api.Assertions.*;
64

5+
import org.junit.jupiter.api.Test;
6+
77
class VolatileExampleTest {
88
int number = 0;
9+
910
@Test
1011
void test() throws InterruptedException {
1112
class MyThread extends Thread {
@@ -27,4 +28,4 @@ public void run() {
2728
thirdThread.join();
2829
assertEquals(30, number);
2930
}
30-
}
31+
}

0 commit comments

Comments
 (0)