File tree Expand file tree Collapse file tree 9 files changed +95
-9
lines changed
src/main/java/com/FlammulinaBlog/Java/Concurrent
META-INF/maven/com.FlammulinaBlog/JavaConcurrent
test-classes/com/FlammulinaBlog/JavaConcurrent Expand file tree Collapse file tree 9 files changed +95
-9
lines changed Original file line number Diff line number Diff line change 11eclipse.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
412org.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
Original file line number Diff line number Diff line change 1- package com .FlammulinaBlog .Java .Concurrent ;
1+ package com .FlammulinaBlog .Java .Concurrent . Semaphore ;
22
33import java .util .Collections ;
44import java .util .HashSet ;
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" );
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
33version =0.0.1
44groupId =com.FlammulinaBlog
55m2e.projectName =JavaConcurrent
6- m2e.projectLocation =G\:\\ workspace_4.7\\ JavaConcurrent
6+ m2e.projectLocation =G\:\\ workspace_4.7\\ Java-Concurrent \\ JavaConcurrent
77artifactId =JavaConcurrent
You can’t perform that action at this time.
0 commit comments