Skip to content

Commit 31c43f3

Browse files
committed
java-multihread 更新至 example16,synchronized包暂时结束
1 parent 176fdcd commit 31c43f3

File tree

6 files changed

+379
-0
lines changed

6 files changed

+379
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.brianway.learning.java.multithread.synchronize.example15;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
7+
/**
8+
* P130
9+
* sychronized代码块有volatile同步的功能
10+
*/
11+
public class Run15_synchronized {
12+
public static void main(String[] args) {
13+
try {
14+
RunThread2 t = new RunThread2();
15+
t.start();
16+
Thread.sleep(1000);
17+
t.setRunning(false);
18+
System.out.println("已经赋值为false");
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
}
24+
25+
26+
/*
27+
输出:
28+
进入run了
29+
线程被停止了
30+
已经赋值为false
31+
32+
-----------------
33+
注释掉RunThread2类中的synchronized ("any thing"){}
34+
输出:
35+
进入run了
36+
已经赋值为false
37+
38+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.brianway.learning.java.multithread.synchronize.example15;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
7+
/**
8+
* P121
9+
* 不使用volatile关键字
10+
* JVM配置 -server(IDEA15.02,jdk1.8,WIN7,64bit不加效果也是的)
11+
*/
12+
public class Run15_volatile {
13+
public static void main(String[] args) {
14+
try {
15+
RunThread t = new RunThread();
16+
t.start();
17+
Thread.sleep(1000);
18+
t.setRunning(false);
19+
System.out.println("已经赋值为false");
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
25+
26+
27+
/*
28+
输出:
29+
进入run了
30+
已经赋值为false
31+
32+
--------------
33+
加上volatile关键字后
34+
输出:
35+
进入run了
36+
已经赋值为false
37+
线程被停止了
38+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.brianway.learning.java.multithread.synchronize.example15;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
public class RunThread extends Thread{
7+
private boolean isRunning = true;
8+
//volatile private boolean isRunning = true;
9+
10+
public boolean isRunning(){
11+
return isRunning;
12+
}
13+
14+
public void setRunning(boolean running) {
15+
isRunning = running;
16+
}
17+
18+
@Override
19+
public void run() {
20+
System.out.println("进入run了");
21+
while(isRunning == true ){
22+
23+
}
24+
System.out.println("线程被停止了");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.brianway.learning.java.multithread.synchronize.example15;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
public class RunThread2 extends Thread{
7+
private boolean isRunning = true;
8+
9+
public boolean isRunning(){
10+
return isRunning;
11+
}
12+
13+
public void setRunning(boolean running) {
14+
isRunning = running;
15+
}
16+
17+
@Override
18+
public void run() {
19+
System.out.println("进入run了");
20+
while(isRunning == true ){
21+
synchronized ("any thing"){}
22+
}
23+
System.out.println("线程被停止了");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.synchronize.example16;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
public class MyThread extends Thread {
7+
volatile public static int count;
8+
//synchronized
9+
private static void addCount(){
10+
for(int i=0;i<100;i++) {
11+
count++;
12+
}
13+
System.out.println("count="+count);
14+
}
15+
16+
@Override
17+
public void run() {
18+
addCount();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
package com.brianway.learning.java.multithread.synchronize.example16;
2+
3+
/**
4+
* Created by Brian on 2016/4/13.
5+
*/
6+
7+
/**
8+
* P124
9+
* volatile非原子的特性
10+
*/
11+
public class Run16_volatile {
12+
public static void main(String[] args) {
13+
MyThread[] myThreads = new MyThread[100];
14+
for(int i=0;i<100;i++){
15+
myThreads[i]= new MyThread();
16+
}
17+
for(int i=0;i<100;i++){
18+
myThreads[i].start();
19+
}
20+
21+
}
22+
}
23+
24+
/*
25+
addCount不加synchronized
26+
输出:
27+
count=200
28+
count=200
29+
count=300
30+
count=400
31+
count=500
32+
count=600
33+
count=700
34+
count=800
35+
count=900
36+
count=1000
37+
count=1100
38+
count=1200
39+
count=1300
40+
count=1400
41+
count=1500
42+
count=1600
43+
count=1700
44+
count=1800
45+
count=1900
46+
count=2000
47+
count=2100
48+
count=2200
49+
count=2300
50+
count=2400
51+
count=2500
52+
count=2600
53+
count=2700
54+
count=2800
55+
count=2900
56+
count=3000
57+
count=3100
58+
count=3200
59+
count=3300
60+
count=3400
61+
count=3500
62+
count=3600
63+
count=3700
64+
count=3800
65+
count=3900
66+
count=4000
67+
count=4100
68+
count=4200
69+
count=4300
70+
count=4400
71+
count=4500
72+
count=9456
73+
count=9456
74+
count=9456
75+
count=9456
76+
count=9456
77+
count=9456
78+
count=9456
79+
count=9456
80+
count=9456
81+
count=9456
82+
count=9456
83+
count=9456
84+
count=9456
85+
count=9456
86+
count=9456
87+
count=9556
88+
count=9456
89+
count=9656
90+
count=9756
91+
count=9756
92+
count=9756
93+
count=9856
94+
count=9456
95+
count=9456
96+
count=9456
97+
count=9456
98+
count=9856
99+
count=9856
100+
count=9856
101+
count=9856
102+
count=9856
103+
count=9856
104+
count=9856
105+
count=9856
106+
count=9856
107+
count=9856
108+
count=9856
109+
count=9856
110+
count=9856
111+
count=9756
112+
count=9756
113+
count=9656
114+
count=9556
115+
count=9556
116+
count=9956
117+
count=9856
118+
count=9856
119+
count=9856
120+
count=9856
121+
count=9856
122+
count=9856
123+
count=9856
124+
count=9856
125+
count=9856
126+
count=9856
127+
128+
129+
-----------------------------
130+
addCount()加synchronized
131+
输出:
132+
count=100
133+
count=200
134+
count=300
135+
count=400
136+
count=500
137+
count=600
138+
count=700
139+
count=800
140+
count=900
141+
count=1000
142+
count=1100
143+
count=1200
144+
count=1300
145+
count=1400
146+
count=1500
147+
count=1600
148+
count=1700
149+
count=1800
150+
count=1900
151+
count=2000
152+
count=2100
153+
count=2200
154+
count=2300
155+
count=2400
156+
count=2500
157+
count=2600
158+
count=2700
159+
count=2800
160+
count=2900
161+
count=3000
162+
count=3100
163+
count=3200
164+
count=3300
165+
count=3400
166+
count=3500
167+
count=3600
168+
count=3700
169+
count=3800
170+
count=3900
171+
count=4000
172+
count=4100
173+
count=4200
174+
count=4300
175+
count=4400
176+
count=4500
177+
count=4600
178+
count=4700
179+
count=4800
180+
count=4900
181+
count=5000
182+
count=5100
183+
count=5200
184+
count=5300
185+
count=5400
186+
count=5500
187+
count=5600
188+
count=5700
189+
count=5800
190+
count=5900
191+
count=6000
192+
count=6100
193+
count=6200
194+
count=6300
195+
count=6400
196+
count=6500
197+
count=6600
198+
count=6700
199+
count=6800
200+
count=6900
201+
count=7000
202+
count=7100
203+
count=7200
204+
count=7300
205+
count=7400
206+
count=7500
207+
count=7600
208+
count=7700
209+
count=7800
210+
count=7900
211+
count=8000
212+
count=8100
213+
count=8200
214+
count=8300
215+
count=8400
216+
count=8500
217+
count=8600
218+
count=8700
219+
count=8800
220+
count=8900
221+
count=9000
222+
count=9100
223+
count=9200
224+
count=9300
225+
count=9400
226+
count=9500
227+
count=9600
228+
count=9700
229+
count=9800
230+
count=9900
231+
count=10000
232+
*/

0 commit comments

Comments
 (0)