Skip to content

Commit 8b6aedc

Browse files
volatile
1 parent 8804562 commit 8b6aedc

File tree

9 files changed

+95
-9
lines changed

9 files changed

+95
-9
lines changed
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3-
org.eclipse.jdt.core.compiler.compliance=1.5
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.8
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
412
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5-
org.eclipse.jdt.core.compiler.source=1.5
13+
org.eclipse.jdt.core.compiler.source=1.8

JavaConcurrent/src/main/java/com/FlammulinaBlog/Java/Concurrent/BoundedHashSet.java renamed to JavaConcurrent/src/main/java/com/FlammulinaBlog/Java/Concurrent/Semaphore/SemaphoreTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.FlammulinaBlog.Java.Concurrent;
1+
package com.FlammulinaBlog.Java.Concurrent.Semaphore;
22

33
import java.util.Collections;
44
import java.util.HashSet;
@@ -11,12 +11,12 @@
1111
* <p/>
1212
* @author flammulinaBlog
1313
*/
14-
public class BoundedHashSet<T> {
14+
public class SemaphoreTest<T> {
1515

1616
private final Set<T> set;
1717
private final Semaphore sem;
1818

19-
public BoundedHashSet(int bound) {
19+
public SemaphoreTest(int bound) {
2020
this.set = Collections.synchronizedSet(new HashSet<T>());
2121
this.sem = new Semaphore(bound);
2222
}
@@ -48,7 +48,7 @@ public boolean remove(T o) {
4848
@SuppressWarnings({ "unchecked", "rawtypes" })
4949
public static void main(String[] args) {
5050

51-
BoundedHashSet test=new BoundedHashSet(1);
51+
SemaphoreTest test=new SemaphoreTest(1);
5252
try {
5353
if(test.add("new")) {
5454
test.remove("new");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.FlammulinaBlog.Java.Concurrent.Volatile;
2+
3+
/**
4+
* volatile 保持一致
5+
* @author flammulinaBlog
6+
*/
7+
public class VolatileTest implements Runnable {
8+
private volatile int firstVal;
9+
10+
private volatile int secondVal;
11+
12+
@Override
13+
public void run() {
14+
15+
}
16+
17+
public boolean valEqual() {
18+
return firstVal == secondVal;
19+
}
20+
21+
@SuppressWarnings("unused")
22+
private void workMethod() {
23+
int val = 1;
24+
while (true) {
25+
stepOne(val);
26+
stepTwo(val);
27+
val++;
28+
try {
29+
Thread.sleep(200);
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
}
36+
37+
38+
//赋值后,休眠300毫秒,从而使线程有机会在stepOne操作和stepTwo操作之间被挂起
39+
private void stepOne(int newVal) {
40+
firstVal = newVal;
41+
try {
42+
//模拟长时间运行的情况
43+
Thread.sleep(300);
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
49+
private void stepTwo(int newVal) {
50+
secondVal = newVal;
51+
}
52+
53+
public static void main(String[] args) {
54+
VolatileTest dsr = new VolatileTest();
55+
Thread t = new Thread(dsr);
56+
t.start();
57+
58+
//休眠1秒,让其他线程有机会获得执行
59+
try {
60+
Thread.sleep(1000);
61+
} catch (InterruptedException x) {
62+
}
63+
for (int i = 0; i < 10; i++) {
64+
//挂起线程
65+
// t.suspend();
66+
System.out.println("dsr.valEqual()=" + dsr.valEqual());
67+
//恢复线程
68+
// t.resume();
69+
try {
70+
//线程随机休眠0~2秒
71+
Thread.sleep((long) (Math.random() * 2000.0));
72+
} catch (InterruptedException x) {
73+
//略
74+
}
75+
}
76+
System.exit(0); //中断应用程序
77+
}
78+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Generated by Maven Integration for Eclipse
2-
#Tue Dec 26 15:32:42 CST 2017
2+
#Tue Dec 26 16:02:53 CST 2017
33
version=0.0.1
44
groupId=com.FlammulinaBlog
55
m2e.projectName=JavaConcurrent
6-
m2e.projectLocation=G\:\\workspace_4.7\\JavaConcurrent
6+
m2e.projectLocation=G\:\\workspace_4.7\\Java-Concurrent\\JavaConcurrent
77
artifactId=JavaConcurrent
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)