Skip to content

Commit c05e05f

Browse files
committed
添加对象以及变量的并发访问的第一部分
1 parent f236dd3 commit c05e05f

File tree

24 files changed

+579
-0
lines changed

24 files changed

+579
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum;
2+
3+
/**
4+
* 方法的私有变量
5+
* 非线程安全问题存在于实例变量,如果是方法内部的私有变量则不存在非线程安全问题.
6+
*
7+
* @author chen weijie
8+
* @date 2018-04-12 12:55 AM
9+
*/
10+
public class HasPrivateNum {
11+
12+
13+
public void addI(String userName) {
14+
int num = 0;
15+
try {
16+
if (userName.equals("a")) {
17+
num = 100;
18+
System.out.println("a set over");
19+
Thread.sleep(1000);
20+
} else {
21+
num = 200;
22+
System.out.println("b set over");
23+
}
24+
System.out.println(userName + " ,num=" + num);
25+
} catch (InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-12 1:08 AM
7+
*/
8+
public class Test {
9+
10+
public static void main(String[] args) {
11+
HasPrivateNum hasPrivateNum = new HasPrivateNum();
12+
ThreadA threadA = new ThreadA(hasPrivateNum);
13+
threadA.start();
14+
ThreadB threadB = new ThreadB(hasPrivateNum);
15+
threadB.start();
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-12 12:58 AM
7+
*/
8+
public class ThreadA extends Thread {
9+
10+
11+
private HasPrivateNum numRef;
12+
13+
public ThreadA(HasPrivateNum numRef) {
14+
super();
15+
this.numRef = numRef;
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
numRef.addI("a");
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum;
2+
3+
import com.chen.api.util.thread.study.chapter2.twoObjectTwoLock.HasSelfPrivateNum;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-04-12 1:06 AM
8+
*/
9+
public class ThreadB extends Thread {
10+
11+
private HasPrivateNum numRef;
12+
13+
public ThreadB(HasPrivateNum numRef) {
14+
super();
15+
this.numRef = numRef;
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
numRef.addI("b");
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum2;
2+
3+
/**
4+
* 方法的私有变量
5+
* 多个线程共同访问一个对象中的实例变量,则可能出现非线程安全问题.如果对象仅有一个实例变量,则可能出现覆盖的情况
6+
*
7+
* @author chen weijie
8+
* @date 2018-04-12 12:55 AM
9+
*/
10+
public class HasPrivateNum2 {
11+
12+
int num = 0;
13+
14+
public void addI(String userName) {
15+
try {
16+
if (userName.equals("a")) {
17+
num = 100;
18+
System.out.println("a set over");
19+
Thread.sleep(1000);
20+
} else {
21+
num = 200;
22+
System.out.println("b set over");
23+
}
24+
System.out.println(userName + " ,num=" + num);
25+
} catch (InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum2;
2+
3+
/**
4+
* 对象中的变量是实例变量,但是方法是同步方法
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-12 1:23 AM
8+
*/
9+
public class SynHasPrivateNum {
10+
11+
12+
private int i = 0;
13+
14+
synchronized public void addI(String userName) {
15+
if ("a".equals(userName)) {
16+
i = 100;
17+
System.out.println("a set over");
18+
try {
19+
Thread.sleep(1000);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
} else {
24+
i = 200;
25+
System.out.println("b set over");
26+
}
27+
28+
System.out.println(userName + " ,num=" + i);
29+
}
30+
31+
}
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.chapter2.hasSelfPrivateNum2;
2+
3+
/**
4+
*
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-12 1:08 AM
8+
*/
9+
public class Test2 {
10+
11+
public static void main(String[] args) {
12+
HasPrivateNum2 hasPrivateNum = new HasPrivateNum2();
13+
ThreadA2 threadA = new ThreadA2(hasPrivateNum);
14+
threadA.start();
15+
ThreadB2 threadB = new ThreadB2(hasPrivateNum);
16+
threadB.start();
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.chapter2.hasSelfPrivateNum2;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-12 1:28 AM
6+
*/
7+
public class TestSyn {
8+
9+
public static void main(String[] args) {
10+
11+
SynHasPrivateNum numRef = new SynHasPrivateNum();
12+
ThreadA3 threadA3 = new ThreadA3(numRef);
13+
ThreadB3 threadB3 = new ThreadB3(numRef);
14+
threadA3.start();
15+
threadB3.start();
16+
17+
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum2;
2+
3+
import com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum.HasPrivateNum;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-04-12 12:58 AM
8+
*/
9+
public class ThreadA2 extends Thread {
10+
11+
12+
private HasPrivateNum2 numRef2;
13+
14+
public ThreadA2(HasPrivateNum2 numRef2) {
15+
super();
16+
this.numRef2 = numRef2;
17+
}
18+
19+
@Override
20+
public void run() {
21+
super.run();
22+
numRef2.addI("a");
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chen.api.util.thread.study.chapter2.hasSelfPrivateNum2;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-12 1:29 AM
6+
*/
7+
public class ThreadA3 extends Thread {
8+
9+
private SynHasPrivateNum numRef;
10+
11+
public ThreadA3(SynHasPrivateNum numRef) {
12+
this.numRef = numRef;
13+
}
14+
15+
public void run() {
16+
super.run();
17+
numRef.addI("a");
18+
19+
}
20+
}

0 commit comments

Comments
 (0)