Skip to content

Commit 628e663

Browse files
committed
添加多线程的学习代码
1 parent a37eefa commit 628e663

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chen.api.util.thread.study.chapter1.notShareVariable;
2+
3+
/**
4+
* 不共享变量
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-09 3:02 AM
8+
*/
9+
public class MyThread extends Thread {
10+
11+
private int i = 5;
12+
13+
public MyThread(String name) {
14+
super();
15+
this.setName(name);
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
while (i > 0) {
22+
i--;
23+
System.out.println(Thread.currentThread().getName() + "计算count==" + i);
24+
}
25+
26+
}
27+
28+
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.thread.study.chapter1.notShareVariable;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-09 3:08 AM
8+
*/
9+
public class TestNoShareVariable {
10+
11+
public static void main(String[] args) {
12+
MyThread thread1 = new MyThread("线程1");
13+
MyThread thread2 = new MyThread("线程2");
14+
MyThread thread3 = new MyThread("线程3");
15+
thread1.start();
16+
try {
17+
Thread.sleep(1000);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
thread2.start();
22+
thread3.start();
23+
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chen.api.util.thread.study.chapter1.shareVariable;
2+
3+
/**
4+
* 共享变量
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-09 3:16 AM
8+
*/
9+
public class ShareVariableThread extends Thread {
10+
11+
private int i = 10;
12+
13+
@Override
14+
public void run() {
15+
i--;
16+
System.out.println(Thread.currentThread().getName() + "计算,count=" + i);
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.chen.api.util.thread.study.chapter1.shareVariable;
2+
3+
/**
4+
* 共享变量(优化类,加同步锁)
5+
* 多个线程之间进行同步,按顺序排队的方式进行减1操作
6+
*
7+
* @author chen weijie
8+
* @date 2018-04-09 3:16 AM
9+
*/
10+
public class ShareVariableThread2 extends Thread {
11+
12+
private int i = 10;
13+
14+
@Override
15+
synchronized public void run() {
16+
i--;
17+
System.out.println(Thread.currentThread().getName() + "计算,count=" + i);
18+
}
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.chen.api.util.thread.study.chapter1.shareVariable;
2+
3+
import com.chen.api.util.thread.study.chapter1.notShareVariable.MyThread;
4+
5+
/**
6+
* 测试类
7+
*
8+
* @author chen weijie
9+
* @date 2018-04-09 3:18 AM
10+
*/
11+
public class Test {
12+
13+
public static void main(String[] args) {
14+
15+
//thread实例其实和Runnable代码块一样,可以使用new Thread(thread) 创建线程;
16+
ShareVariableThread thread = new ShareVariableThread();
17+
Thread t1 = new Thread(thread, "t1");
18+
Thread t2 = new Thread(thread, "t2");
19+
Thread t3 = new Thread(thread, "t3");
20+
Thread t4 = new Thread(thread, "t4");
21+
22+
t1.start();
23+
t2.start();
24+
t3.start();
25+
t4.start();
26+
27+
// 打印的结果其实时重复的,是非线程安全的
28+
// t3计算,count=9
29+
// t2计算,count=8
30+
// t1计算,count=9
31+
// t4计算,count=7
32+
33+
//在代码块中加synchronize同步锁,使得代码按照顺序执行
34+
ShareVariableThread2 thread2 =new ShareVariableThread2();
35+
Thread t5 =new Thread(thread2,"t5");
36+
Thread t6 =new Thread(thread2,"t6");
37+
Thread t7 =new Thread(thread2,"t7");
38+
Thread t8 =new Thread(thread2,"t8");
39+
t5.start();
40+
t6.start();
41+
t7.start();
42+
t8.start();
43+
44+
//在执行run方法前,先判断方法是否加synchronize锁,如果上锁,说明其它线程在调用run方法,必须等待其它线程对run方法调用结束后才可以执行run方法
45+
46+
47+
}
48+
}

0 commit comments

Comments
 (0)