Skip to content

Commit cd255d5

Browse files
committed
同步代码块-联系
1 parent 0f44d8a commit cd255d5

30 files changed

+775
-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.doubleSynBlockOneTwo;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 1:36 AM
6+
*/
7+
public class ObjectService {
8+
9+
public void serviceMethodA() {
10+
synchronized (this) {
11+
try {
12+
System.out.println("A begin time =" + System.currentTimeMillis());
13+
Thread.sleep(2000);
14+
System.out.println("A end time =" + System.currentTimeMillis());
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
}
20+
21+
public void serviceMethodB() {
22+
23+
synchronized (this) {
24+
System.out.println("B begin time =" + System.currentTimeMillis());
25+
System.out.println("B end time =" + System.currentTimeMillis());
26+
}
27+
}
28+
29+
30+
}
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.doubleSynBlockOneTwo;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 1:42 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
ObjectService service = new ObjectService();
12+
ThreadA threadA = new ThreadA(service);
13+
ThreadB threadB = new ThreadB(service);
14+
threadA.setName("A");
15+
threadB.setName("B");
16+
threadA.start();
17+
threadB.start();
18+
}
19+
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter2.doubleSynBlockOneTwo;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 1:40 AM
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
10+
private ObjectService service;
11+
12+
public ThreadA(ObjectService service) {
13+
super();
14+
this.service = service;
15+
16+
}
17+
18+
19+
@Override
20+
public void run() {
21+
super.run();
22+
service.serviceMethodA();
23+
}
24+
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter2.doubleSynBlockOneTwo;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 1:40 AM
6+
*/
7+
public class ThreadB extends Thread {
8+
9+
10+
private ObjectService service;
11+
12+
public ThreadB(ObjectService service) {
13+
super();
14+
this.service = service;
15+
16+
}
17+
18+
19+
@Override
20+
public void run() {
21+
super.run();
22+
service.serviceMethodB();
23+
}
24+
25+
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.chen.api.util.thread.study.chapter2.synBlockingString;
2+
3+
import com.chen.api.util.thread.study.chapter2.throwExceptionNoLock.ThreadB;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-04-15 2:26 AM
8+
*/
9+
public class Service {
10+
11+
private String userNameParam;
12+
13+
private String passWordParam;
14+
15+
private String anyString = new String();
16+
17+
public void setUserNamePassword(String userName, String passWord) {
18+
19+
try {
20+
// 使用synchronized(非this对象X)同步代码块格式进行同步时,对象监视器必须是同一个对象
21+
// 如果使用非同一个对象达不到同步的效果
22+
// String anyString = new String();
23+
synchronized (anyString) {
24+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入同步块");
25+
userNameParam = userName;
26+
Thread.sleep(3000);
27+
passWordParam = passWord;
28+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开同步块");
29+
}
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
34+
}
35+
36+
}
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.synBlockingString;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 2:33 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
Service service = new Service();
11+
ThreadA threadA = new ThreadA(service);
12+
ThreadB threadB = new ThreadB(service);
13+
threadA.setName("aaaaaaaaaaaaaaaaaaaa");
14+
threadB.setName("bbbbbbbbbbbbbbbbbbbb");
15+
threadA.start();
16+
threadB.start();
17+
}
18+
19+
}
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.synBlockingString;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 2:31 AM
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
private Service service;
10+
11+
public ThreadA(Service service) {
12+
super();
13+
this.service = service;
14+
}
15+
16+
@Override
17+
public void run() {
18+
super.run();
19+
service.setUserNamePassword("a", "aa");
20+
}
21+
22+
}
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.synBlockingString;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-15 2:31 AM
6+
*/
7+
public class ThreadB extends Thread {
8+
9+
private Service service;
10+
11+
public ThreadB(Service service) {
12+
super();
13+
this.service = service;
14+
}
15+
16+
@Override
17+
public void run() {
18+
super.run();
19+
service.setUserNamePassword("b", "bb");
20+
}
21+
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter2.synchronizedOneThreadIn;
2+
3+
/**
4+
* synchronized同步代码块的使用
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-14 9:54 PM
8+
*/
9+
public class ObjectService {
10+
11+
public void serviceMethod() {
12+
13+
synchronized (this) {
14+
try {
15+
System.out.println("beginTime:" + System.currentTimeMillis() + ",thread:" + Thread.currentThread().getName());
16+
Thread.sleep(10000);
17+
System.out.println("endTime:" + System.currentTimeMillis() + ",thread:" + Thread.currentThread().getName());
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
}
24+
25+
26+
}
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.synchronizedOneThreadIn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-14 9:58 PM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
ObjectService service = new ObjectService();
12+
13+
ThreadA threadA = new ThreadA(service);
14+
ThreadB threadB = new ThreadB(service);
15+
threadA.setName("A");
16+
threadB.setName("B");
17+
threadA.start();
18+
threadB.start();
19+
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)