12
12
*/
13
13
public class TwoThread {
14
14
15
- private int start = 1 ;
15
+ private int start = 1 ;
16
16
17
17
/**
18
18
* 保证内存可见性
19
19
* 其实用锁了之后也可以保证可见性 这里用不用 volatile 都一样
20
20
*/
21
- private boolean flag = false ;
21
+ private boolean flag = false ;
22
22
23
23
/**
24
24
* 重入锁
25
25
*/
26
- private final static Lock LOCK = new ReentrantLock () ;
26
+ private final static Lock LOCK = new ReentrantLock ();
27
27
28
28
public static void main (String [] args ) {
29
- TwoThread twoThread = new TwoThread () ;
29
+ TwoThread twoThread = new TwoThread ();
30
30
31
31
Thread t1 = new Thread (new OuNum (twoThread ));
32
32
t1 .setName ("t1" );
33
33
34
34
35
- Thread t2 = new Thread (new JiNum (twoThread )) ;
35
+ Thread t2 = new Thread (new JiNum (twoThread ));
36
36
t2 .setName ("t2" );
37
37
38
38
t1 .start ();
@@ -42,73 +42,73 @@ public static void main(String[] args) {
42
42
/**
43
43
* 偶数线程
44
44
*/
45
- public static class OuNum implements Runnable {
45
+ public static class OuNum implements Runnable {
46
46
47
- private TwoThread number ;
47
+ private TwoThread number ;
48
48
49
49
public OuNum (TwoThread number ) {
50
50
this .number = number ;
51
51
}
52
52
53
53
@ Override
54
54
public void run () {
55
- while (number .start <= 100 ){
56
-
57
- if (number .flag ){
58
- try {
59
- LOCK .lock ();
60
- System .out .println (Thread .currentThread ().getName () + "+-+" + number .start );
61
- number .start ++;
62
- number .flag = false ;
63
-
64
-
65
- }finally {
66
- LOCK .unlock ();
67
- }
68
- }else {
69
- try {
70
- Thread .sleep (10 );
71
- } catch (InterruptedException e ) {
72
- e .printStackTrace ();
73
- }
55
+ while (number .start <= 100 ) {
56
+
57
+ if (number .flag ) {
58
+ try {
59
+ LOCK .lock ();
60
+ System .out .println (Thread .currentThread ().getName () + "+-+" + number .start );
61
+ number .start ++;
62
+ number .flag = false ;
63
+
64
+
65
+ } finally {
66
+ LOCK .unlock ();
67
+ }
68
+ } else {
69
+ try {
70
+ Thread .sleep (10 );
71
+ } catch (InterruptedException e ) {
72
+ e .printStackTrace ();
74
73
}
74
+ }
75
75
}
76
76
}
77
77
}
78
78
79
79
/**
80
80
* 奇数线程
81
81
*/
82
- public static class JiNum implements Runnable {
82
+ public static class JiNum implements Runnable {
83
83
84
- private TwoThread number ;
84
+ private TwoThread number ;
85
85
86
86
public JiNum (TwoThread number ) {
87
87
this .number = number ;
88
88
}
89
89
90
90
@ Override
91
91
public void run () {
92
- while (number .start <= 100 ){
92
+ while (number .start <= 100 ) {
93
93
94
- if (!number .flag ){
95
- try {
94
+ if (!number .flag ) {
95
+ try {
96
96
LOCK .lock ();
97
- System .out .println ( Thread .currentThread ().getName () + "+-+" + number .start );
97
+ System .out .println (Thread .currentThread ().getName () + "+-+" + number .start );
98
98
number .start ++;
99
99
number .flag = true ;
100
100
101
101
102
- }finally {
103
- LOCK .unlock ();
104
- }
105
- }else {
106
- try {
107
- Thread .sleep (10 );
108
- } catch (InterruptedException e ) {
109
- e .printStackTrace ();
110
- }
102
+ } finally {
103
+ LOCK .unlock ();
104
+ }
105
+ } else {
106
+ try {
107
+ Thread .sleep (10 );
108
+ } catch (InterruptedException e ) {
109
+ e .printStackTrace ();
111
110
}
111
+ }
112
112
}
113
113
}
114
114
}
0 commit comments