Skip to content

Commit cb33aa9

Browse files
committed
♻️ Refactoring code.
1 parent fdf4a71 commit cb33aa9

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

src/main/java/com/crossoverjie/actual/TwoThread.java

+41-41
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212
*/
1313
public class TwoThread {
1414

15-
private int start = 1 ;
15+
private int start = 1;
1616

1717
/**
1818
* 保证内存可见性
1919
* 其实用锁了之后也可以保证可见性 这里用不用 volatile 都一样
2020
*/
21-
private boolean flag = false ;
21+
private boolean flag = false;
2222

2323
/**
2424
* 重入锁
2525
*/
26-
private final static Lock LOCK = new ReentrantLock() ;
26+
private final static Lock LOCK = new ReentrantLock();
2727

2828
public static void main(String[] args) {
29-
TwoThread twoThread = new TwoThread() ;
29+
TwoThread twoThread = new TwoThread();
3030

3131
Thread t1 = new Thread(new OuNum(twoThread));
3232
t1.setName("t1");
3333

3434

35-
Thread t2 = new Thread(new JiNum(twoThread)) ;
35+
Thread t2 = new Thread(new JiNum(twoThread));
3636
t2.setName("t2");
3737

3838
t1.start();
@@ -42,73 +42,73 @@ public static void main(String[] args) {
4242
/**
4343
* 偶数线程
4444
*/
45-
public static class OuNum implements Runnable{
45+
public static class OuNum implements Runnable {
4646

47-
private TwoThread number ;
47+
private TwoThread number;
4848

4949
public OuNum(TwoThread number) {
5050
this.number = number;
5151
}
5252

5353
@Override
5454
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();
7473
}
74+
}
7575
}
7676
}
7777
}
7878

7979
/**
8080
* 奇数线程
8181
*/
82-
public static class JiNum implements Runnable{
82+
public static class JiNum implements Runnable {
8383

84-
private TwoThread number ;
84+
private TwoThread number;
8585

8686
public JiNum(TwoThread number) {
8787
this.number = number;
8888
}
8989

9090
@Override
9191
public void run() {
92-
while (number.start <= 100){
92+
while (number.start <= 100) {
9393

94-
if (!number.flag){
95-
try {
94+
if (!number.flag) {
95+
try {
9696
LOCK.lock();
97-
System.out.println( Thread.currentThread().getName() + "+-+" + number.start);
97+
System.out.println(Thread.currentThread().getName() + "+-+" + number.start);
9898
number.start++;
9999
number.flag = true;
100100

101101

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();
111110
}
111+
}
112112
}
113113
}
114114
}

0 commit comments

Comments
 (0)