Skip to content

Commit 4645887

Browse files
committed
refactor(demo-thread): rename demo classes for consistency and clarity
1 parent 0bf2999 commit 4645887

9 files changed

Lines changed: 51 additions & 34 deletions

File tree

demo-thread/src/main/java/com/helltractor/demo/WaitAndNotifyDemo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) throws InterruptedException {
2626

2727
Thread add = new Thread(() -> {
2828
for (int i = 0; i < 10; i++) {
29-
String s = "t-" + Math.random();
29+
String s = "Worker-" + i;
3030
System.out.println("add task: " + s);
3131
q.addTask(s);
3232
try {
@@ -39,12 +39,13 @@ public static void main(String[] args) throws InterruptedException {
3939
add.start();
4040
add.join();
4141
Thread.sleep(100);
42+
4243
for (var t : ts) {
4344
t.interrupt();
4445
}
4546
}
4647

47-
private static class TaskQueue {
48+
static class TaskQueue {
4849

4950
private Queue<String> queue = new LinkedList<>();
5051

@@ -54,6 +55,7 @@ public synchronized void addTask(String s) {
5455
}
5556

5657
public synchronized String getTask() throws InterruptedException {
58+
// avoid spurious wakeup
5759
while (queue.isEmpty()) {
5860
this.wait();
5961
}

demo-thread/src/main/java/com/helltractor/demo/reentrantlock/ReentrantLockDemo.java renamed to demo-thread/src/main/java/com/helltractor/demo/reentrantlock/ReentrantLockSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.concurrent.TimeUnit;
44
import java.util.concurrent.locks.ReentrantLock;
55

6-
public class ReentrantLockDemo {
6+
public class ReentrantLockSample {
77

88
private static void getReentrantLockContext(ReentrantLock lock) {
99
try {

demo-thread/src/main/java/com/helltractor/demo/reentrantlock/ReentrantReadWriteLockDemo.java renamed to demo-thread/src/main/java/com/helltractor/demo/reentrantlock/ReentrantReadWriteLockSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import java.util.Random;
44

5-
public class ReentrantReadWriteLockDemo {
5+
public class ReentrantReadWriteLockSample {
66

77
public static UnboundedCache<Integer, Integer> cache = new UnboundedCache<>();
88

9-
public class MyThread implements Runnable {
9+
public static class ReentrantReadWriteLockThread implements Runnable {
1010

1111
@Override
1212
public void run() {

demo-thread/src/main/java/com/helltractor/demo/reentrantlock/Task.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
@FunctionalInterface
44
public interface Task {
5-
5+
66
void execute();
7+
78
}

demo-thread/src/main/java/com/helltractor/demo/reentrantlock/MyThread.java renamed to demo-thread/src/main/java/com/helltractor/demo/reentrantlock/TaskThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.helltractor.demo.reentrantlock;
22

3-
class MyThread extends Thread {
3+
class TaskThread extends Thread {
44

55
private final Task task;
6-
7-
public MyThread(String name, Task task) {
6+
7+
public TaskThread(String name, Task task) {
88
super(name);
99
this.task = task;
1010
}

demo-thread/src/main/java/com/helltractor/demo/threadlocal/ThreadLocalTask.java renamed to demo-thread/src/main/java/com/helltractor/demo/threadlocal/TaskThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.helltractor.demo.threadlocal;
22

3-
class ThreadLocalTask implements Runnable {
3+
class TaskThread implements Runnable {
44

5-
final String username;
5+
private final String username;
66

7-
public ThreadLocalTask(String username) {
7+
public TaskThread(String username) {
88
this.username = username;
99
}
1010

demo-thread/src/main/java/com/helltractor/demo/threadlocal/ThreadLocalDemo.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
import java.util.concurrent.TimeUnit;
66

77
public class ThreadLocalDemo {
8-
8+
99
public static void main(String[] args) throws Exception {
1010
ExecutorService es = Executors.newFixedThreadPool(3);
1111
String[] users = new String[]{"Bob", "Alice", "Tim", "Mike", "Lily", "Jack", "Bush"};
1212
for (String user : users) {
13-
es.submit(new ThreadLocalTask(user));
13+
es.submit(new TaskThread(user));
1414
}
1515
es.awaitTermination(3, TimeUnit.SECONDS);
1616
es.shutdown();
17+
1718
InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
19+
inheritableThreadLocal.set("Hello from parent thread!");
20+
21+
Thread childThread = new Thread(() -> {
22+
System.out.println("Child thread: " + inheritableThreadLocal.get());
23+
inheritableThreadLocal.set("Hello from child thread!");
24+
System.out.println("Child thread after set: " + inheritableThreadLocal.get());
25+
});
26+
27+
childThread.start();
28+
childThread.join();
29+
30+
System.out.println("Parent thread after child thread: " + inheritableThreadLocal.get());
31+
inheritableThreadLocal.remove();
1832
}
19-
33+
2034
}

demo-thread/src/test/java/com/helltractor/demo/reentrantlock/ReentrantLockTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.helltractor.demo.reentrantlock;
22

3-
import com.helltractor.demo.reentrantlock.ReentrantLockDemo.LockInterruptiblyTask;
4-
import com.helltractor.demo.reentrantlock.ReentrantLockDemo.LockTask;
5-
import com.helltractor.demo.reentrantlock.ReentrantLockDemo.TryLockTask;
6-
import com.helltractor.demo.reentrantlock.ReentrantLockDemo.TryLockTaskTwo;
3+
import com.helltractor.demo.reentrantlock.ReentrantLockSample.LockInterruptiblyTask;
4+
import com.helltractor.demo.reentrantlock.ReentrantLockSample.LockTask;
5+
import com.helltractor.demo.reentrantlock.ReentrantLockSample.TryLockTask;
6+
import com.helltractor.demo.reentrantlock.ReentrantLockSample.TryLockTaskTwo;
77
import org.junit.jupiter.api.Test;
88

99
public class ReentrantLockTest {
1010

1111
@Test
1212
public void testReentrantLock() {
1313
LockTask lockTask = new LockTask();
14-
MyThread thread1 = new MyThread("Thread 1", lockTask);
15-
MyThread thread2 = new MyThread("Thread 2", lockTask);
16-
MyThread thread3 = new MyThread("Thread 3", lockTask);
14+
TaskThread thread1 = new TaskThread("Thread 1", lockTask);
15+
TaskThread thread2 = new TaskThread("Thread 2", lockTask);
16+
TaskThread thread3 = new TaskThread("Thread 3", lockTask);
1717

1818
thread1.start();
1919
thread2.start();
@@ -23,9 +23,9 @@ public void testReentrantLock() {
2323
@Test
2424
public void testTryLock() {
2525
TryLockTask tryLockTask = new TryLockTask();
26-
MyThread thread1 = new MyThread("Thread 1", tryLockTask);
27-
MyThread thread2 = new MyThread("Thread 2", tryLockTask);
28-
MyThread thread3 = new MyThread("Thread 3", tryLockTask);
26+
TaskThread thread1 = new TaskThread("Thread 1", tryLockTask);
27+
TaskThread thread2 = new TaskThread("Thread 2", tryLockTask);
28+
TaskThread thread3 = new TaskThread("Thread 3", tryLockTask);
2929

3030
thread1.start();
3131
thread2.start();
@@ -35,9 +35,9 @@ public void testTryLock() {
3535
@Test
3636
public void testTryLockWithTimeout() {
3737
TryLockTaskTwo tryLockTask = new TryLockTaskTwo();
38-
MyThread thread1 = new MyThread("Thread 1", tryLockTask);
39-
MyThread thread2 = new MyThread("Thread 2", tryLockTask);
40-
MyThread thread3 = new MyThread("Thread 3", tryLockTask);
38+
TaskThread thread1 = new TaskThread("Thread 1", tryLockTask);
39+
TaskThread thread2 = new TaskThread("Thread 2", tryLockTask);
40+
TaskThread thread3 = new TaskThread("Thread 3", tryLockTask);
4141

4242
thread1.start();
4343
thread2.start();
@@ -47,9 +47,9 @@ public void testTryLockWithTimeout() {
4747
@Test
4848
public void testTryLockWithTimeoutAndNanos() {
4949
LockInterruptiblyTask lockInterruptiblyTask = new LockInterruptiblyTask();
50-
MyThread thread1 = new MyThread("Thread 1", lockInterruptiblyTask);
51-
MyThread thread2 = new MyThread("Thread 2", lockInterruptiblyTask);
52-
MyThread thread3 = new MyThread("Thread 3", lockInterruptiblyTask);
50+
TaskThread thread1 = new TaskThread("Thread 1", lockInterruptiblyTask);
51+
TaskThread thread2 = new TaskThread("Thread 2", lockInterruptiblyTask);
52+
TaskThread thread3 = new TaskThread("Thread 3", lockInterruptiblyTask);
5353

5454
thread1.start();
5555
thread2.start();

demo-thread/src/test/java/com/helltractor/demo/reentrantlock/ReentrantReadWriteLockTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class ReentrantReadWriteLockTest {
1212
public void testReentrantReadWriteLock() {
1313

1414
ExecutorService executorService = Executors.newCachedThreadPool();
15-
ReentrantReadWriteLockDemo demo = new ReentrantReadWriteLockDemo();
15+
ReentrantReadWriteLockSample sample = new ReentrantReadWriteLockSample();
1616
for (int i = 0; i < 20; i++) {
17-
executorService.execute(demo.new MyThread());
18-
demo.cache.get(0);
17+
executorService.execute(new ReentrantReadWriteLockSample.ReentrantReadWriteLockThread());
18+
sample.cache.get(0);
1919
}
2020
executorService.shutdown();
2121
}

0 commit comments

Comments
 (0)