File tree Expand file tree Collapse file tree 2 files changed +29
-11
lines changed
LeetCodePrj/Java/leetcode/easy/page7 Expand file tree Collapse file tree 2 files changed +29
-11
lines changed Original file line number Diff line number Diff line change 55public 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 }
Original file line number Diff line number Diff line change 66import java .util .concurrent .locks .ReentrantLock ;
77
88public 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 {
You can’t perform that action at this time.
0 commit comments