Skip to content

Commit 0f44d8a

Browse files
committed
添加synchronized方法的学习
1 parent c05e05f commit 0f44d8a

File tree

15 files changed

+339
-0
lines changed

15 files changed

+339
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.api.util.thread.study.chapter2.dirtyRead;
2+
3+
/**
4+
* 公共变量
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-12 11:55 PM
8+
*/
9+
public class PublicVar {
10+
11+
public String userName;
12+
13+
public String passWord;
14+
15+
synchronized public void setValue(String userName, String passWord) {
16+
17+
try {
18+
this.userName = userName;
19+
Thread.sleep(5000);
20+
this.passWord = passWord;
21+
System.out.println("setValue method thread name=" +
22+
Thread.currentThread().getName() + ",userName:===" + userName + ",passWord===" + passWord);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
synchronized public void getValue() {
29+
System.out.println("getValue method thread name=" +
30+
Thread.currentThread().getName() + ",userName:===" + userName + ",passWord===" + passWord);
31+
32+
}
33+
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter2.dirtyRead;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 12:05 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
PublicVar var = new PublicVar();
12+
ThreadA threadA = new ThreadA(var);
13+
threadA.start();
14+
try {
15+
Thread.sleep(1000);//打印结果受值大小影响此影响(如果getValue方法不加synchronize关键字)
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
var.getValue();
20+
}
21+
22+
}
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.dirtyRead;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 12:03 AM
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
private PublicVar var;
10+
11+
public ThreadA(PublicVar var) {
12+
super();
13+
this.var = var;
14+
}
15+
16+
@Override
17+
public void run() {
18+
super.run();
19+
var.setValue("B", "BBBBBB");
20+
}
21+
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter2.synLockIn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 12:45 AM
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
Service service = new Service();
12+
service.service1();
13+
}
14+
}
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.synLockIn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 12:43 AM
6+
*/
7+
public class Service {
8+
9+
10+
synchronized public void service1() {
11+
System.out.println("service1");
12+
service2();
13+
}
14+
15+
synchronized public void service2() {
16+
System.out.println("service2");
17+
service3();
18+
}
19+
20+
synchronized public void service3() {
21+
System.out.println("service3");
22+
}
23+
}
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.synLockIn;
2+
3+
/**
4+
*
5+
* 关键字synchronized拥有锁重入的功能,当一个线程得到一个对象锁之后,再次请求此对象锁时,是可以再次获得该对象的锁的.
6+
* @author chen weijie
7+
* @date 2018-04-13 12:46 AM
8+
*/
9+
public class Test {
10+
11+
public static void main(String[] args) {
12+
13+
MyThread myThread = new MyThread();
14+
myThread.start();
15+
}
16+
17+
}
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.synNotExtends;
2+
3+
import com.chen.api.util.thread.study.chapter2.throwExceptionNoLock.ThreadB;
4+
5+
/**
6+
* 同步不可以继承
7+
*
8+
* @author chen weijie
9+
* @date 2018-04-13 1:07 AM
10+
*/
11+
public class Main {
12+
13+
14+
synchronized public void serviceMethod() {
15+
System.out.println("int main 下一步 sleep begin threadName=" + Thread.currentThread().getName() + ",time===" + System.currentTimeMillis());
16+
try {
17+
Thread.sleep(5000);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
System.out.println("int main 下一步 sleep end threadName=" + Thread.currentThread().getName() + ",time===" + System.currentTimeMillis());
22+
}
23+
24+
}
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.synNotExtends;
2+
3+
import com.chen.api.util.thread.threadPool.MyThread;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-04-13 1:11 AM
8+
*/
9+
public class MyThreadA extends Thread {
10+
11+
private Sub sub;
12+
13+
public MyThreadA(Sub sub) {
14+
this.sub = sub;
15+
}
16+
17+
@Override
18+
public void run() {
19+
sub.serviceMethod();
20+
}
21+
22+
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.api.util.thread.study.chapter2.synNotExtends;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 1:11 AM
6+
*/
7+
public class MyThreadB extends Thread {
8+
9+
private Sub sub;
10+
11+
public MyThreadB(Sub sub) {
12+
this.sub = sub;
13+
}
14+
15+
@Override
16+
public void run() {
17+
sub.serviceMethod();
18+
}
19+
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.api.util.thread.study.chapter2.synNotExtends;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-13 1:10 AM
6+
*/
7+
public class Sub extends Main {
8+
9+
10+
@Override
11+
public void serviceMethod() {
12+
System.out.println("int sub 下一步 sleep begin threadName=" + Thread.currentThread().getName() + ",time===" + System.currentTimeMillis());
13+
try {
14+
Thread.sleep(5000);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
System.out.println("int sub 下一步 sleep end threadName=" + Thread.currentThread().getName() + ",time===" + System.currentTimeMillis());
19+
super.serviceMethod();
20+
}
21+
}

0 commit comments

Comments
 (0)