Skip to content

Commit dad42e7

Browse files
committed
Print in Order: fix
1 parent 6050580 commit dad42e7

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

LeetCodePrj/Java/leetcode/easy/page7/EasyPage7.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,47 @@
55
public class EasyPage7 {
66
@Test
77
public void testPrintInOrder() throws Exception {
8-
Runnable first = new Runnable() {
8+
Thread first = new Thread(new Runnable() {
99
@Override
1010
public void run() {
1111
System.out.println("first");
1212
}
13-
};
14-
Runnable second = new Runnable() {
13+
});
14+
Thread second = new Thread(new Runnable() {
1515
@Override
1616
public void run() {
1717
System.out.println("second");
1818
}
19-
};
20-
Runnable third = new Runnable() {
19+
});
20+
Thread third = new Thread(new Runnable() {
2121
@Override
2222
public void run() {
2323
System.out.println("third");
2424
}
25-
};
25+
});
2626

2727
PrintInOrder printInOrder = new PrintInOrder();
28-
printInOrder.first(first);
29-
printInOrder.second(second);
30-
printInOrder.third(third);
28+
new Thread(() -> {
29+
try {
30+
printInOrder.first(first);
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
}).start();
35+
new Thread(() -> {
36+
try {
37+
printInOrder.second(second);
38+
} catch (InterruptedException e) {
39+
e.printStackTrace();
40+
}
41+
}).start();
42+
new Thread(() -> {
43+
try {
44+
printInOrder.third(third);
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
}
48+
}).start();
3149

3250
System.in.read();
3351
}

LeetCodePrj/Java/leetcode/easy/page7/PrintInOrder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import java.util.concurrent.locks.ReentrantLock;
77

88
public class PrintInOrder {
9-
private Semaphore semaphoreSecond = new Semaphore(1);
10-
private Semaphore semaphoreThird = new Semaphore(1);
9+
private static Semaphore semaphoreSecond = new Semaphore(1);
10+
private static Semaphore semaphoreThird = new Semaphore(1);
1111

1212
public PrintInOrder() {
1313
try {

0 commit comments

Comments
 (0)