Skip to content

Commit f29f1db

Browse files
committed
添加synchronized关键字的使用
1 parent 924ee71 commit f29f1db

File tree

12 files changed

+334
-0
lines changed

12 files changed

+334
-0
lines changed
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.StringAndSyn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-17 11:41 PM
6+
*/
7+
public class Service {
8+
9+
10+
public static void print(String stringParam) {
11+
try {
12+
synchronized (stringParam) {
13+
while (true) {
14+
System.out.println(Thread.currentThread().getName());
15+
Thread.sleep(1000);
16+
}
17+
}
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
}
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.StringAndSyn;
2+
3+
/**
4+
* 两个线程都持有'aa'字符串对象的锁,所以造成B线程不能执行,这就是String常量池带来的问题.
5+
* 因此在大多数情况下synchronized代码块都不适用String作为锁对象,而要改用其它 比如new Object()
6+
*
7+
* @author chen weijie
8+
* @date 2018-04-17 11:47 PM
9+
*/
10+
public class Test {
11+
12+
public static void main(String[] args) {
13+
14+
ThreadA threadA = new ThreadA("aa");
15+
ThreadB threadB = new ThreadB("aa");
16+
17+
threadA.setName("a");
18+
threadB.setName("b");
19+
20+
threadA.start();
21+
threadB.start();
22+
}
23+
24+
}
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.StringAndSyn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-17 11:43 PM
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
10+
private String param;
11+
12+
public ThreadA(String param) {
13+
super();
14+
this.param = param;
15+
}
16+
17+
@Override
18+
public void run() {
19+
super.run();
20+
Service.print(param);
21+
}
22+
23+
24+
}
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.StringAndSyn;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-17 11:43 PM
6+
*/
7+
public class ThreadB extends Thread {
8+
9+
10+
private String param;
11+
12+
public ThreadB(String param) {
13+
super();
14+
this.param = param;
15+
}
16+
17+
@Override
18+
public void run() {
19+
super.run();
20+
Service.print(param);
21+
}
22+
23+
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.chen.api.util.thread.study.chapter2.deadLockTest;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-18 12:11 AM
6+
*/
7+
public class DealThreadTask implements Runnable {
8+
9+
public String userName;
10+
public Object lock1 = new Object();
11+
public Object lock2 = new Object();
12+
public void setFlag(String userName) {
13+
this.userName = userName;
14+
}
15+
16+
@Override
17+
public void run() {
18+
if (userName.equals("a")) {
19+
synchronized (lock1) {
20+
System.out.println("userName==" + userName);
21+
try {
22+
Thread.sleep(3000);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
synchronized (lock2) {
28+
System.out.println("按lock1--->lock2的代码执行了..");
29+
}
30+
}
31+
32+
if (userName.equals("b")) {
33+
synchronized (lock2) {
34+
System.out.println("userName========" + userName);
35+
try {
36+
Thread.sleep(3000);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
synchronized (lock1) {
42+
System.out.println("按lock2--->lock1的代码执行了..");
43+
}
44+
}
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.chen.api.util.thread.study.chapter2.deadLockTest;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-18 12:18 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
DealThreadTask task = new DealThreadTask();
12+
task.setFlag("a");
13+
Thread thread1 = new Thread(task);
14+
thread1.start();
15+
try {
16+
Thread.sleep(1000);
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
Thread thread2 = new Thread(task);
21+
task.setFlag("b");
22+
thread2.start();
23+
24+
25+
}
26+
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.thread.study.chapter2.twoStop;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-17 11:56 PM
6+
*/
7+
public class Service {
8+
9+
10+
synchronized public void methodA() {
11+
System.out.println("methodA begin..");
12+
13+
boolean isContinueRun = true;
14+
15+
while (isContinueRun) {
16+
17+
}
18+
19+
System.out.println("methodA end");
20+
}
21+
22+
synchronized public void methodB() {
23+
System.out.println("methodB begin");
24+
System.out.println("methodB end");
25+
}
26+
27+
28+
}
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.twoStop;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-18 12:00 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
Service service = new Service();
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+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.thread.study.chapter2.twoStop;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-17 11:58 PM
7+
*/
8+
public class ThreadA extends Thread {
9+
10+
11+
private Service service;
12+
13+
public ThreadA(Service service) {
14+
super();
15+
this.service = service;
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
service.methodA();
22+
}
23+
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.thread.study.chapter2.twoStop;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-17 11:58 PM
7+
*/
8+
public class ThreadB extends Thread {
9+
10+
11+
private Service service;
12+
13+
public ThreadB(Service service) {
14+
super();
15+
this.service = service;
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
service.methodA();
22+
}
23+
24+
25+
}

0 commit comments

Comments
 (0)