Skip to content

Commit 924ee71

Browse files
committed
添加synchronized类和静态方法
1 parent cd255d5 commit 924ee71

File tree

32 files changed

+801
-0
lines changed

32 files changed

+801
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.chen.api.util.thread.study.chapter2.synBlockMoreObjectOneLock;
2+
3+
/**
4+
* 静态同步
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-16 1:54 AM
8+
*/
9+
public class Service {
10+
11+
12+
public static void printA() {
13+
synchronized (Service.class) {
14+
try {
15+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printA()");
16+
Thread.sleep(3000);
17+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "退出printA()");
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
24+
}
25+
26+
public static void printB() {
27+
synchronized (Service.class) {
28+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printB()");
29+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "退出printB()");
30+
}
31+
}
32+
33+
34+
}
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.synBlockMoreObjectOneLock;
2+
3+
/**
4+
* synchronized(class)代码块和synchronized static方法的作用一样.
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-16 3:29 AM
8+
*/
9+
public class Test {
10+
11+
public static void main(String[] args) {
12+
13+
Service service1 = new Service();
14+
Service service2 = new Service();
15+
ThreadA threadA = new ThreadA(service1);
16+
ThreadB threadB = new ThreadB(service2);
17+
threadA.setName("a");
18+
threadB.setName("b");
19+
threadA.start();
20+
threadB.start();
21+
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.synBlockMoreObjectOneLock;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-16 2:52 AM
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+
service.printA();
21+
}
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.synBlockMoreObjectOneLock;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-04-16 2:52 AM
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+
service.printB();
21+
}
22+
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chen.api.util.thread.study.chapter2.synBlockingString2;
2+
3+
/**
4+
* 对象监视器不同,所以运行结果就是异步的.
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-15 2:26 AM
8+
*/
9+
public class Service {
10+
11+
private String anyString = new String();
12+
13+
public void a() {
14+
15+
try {
16+
synchronized (anyString) {
17+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入同步块");
18+
Thread.sleep(3000);
19+
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开同步块");
20+
}
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
synchronized public void b() {
27+
28+
System.out.println("b begin");
29+
System.out.println("b end");
30+
}
31+
32+
33+
}
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.synBlockingString2;
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.synBlockingString2;
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.a();
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.synBlockingString2;
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.b();
20+
}
21+
22+
}
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.syn_out_asyn;
2+
3+
import com.chen.api.util.thread.study.chapter2.synBlockingString2.ThreadA;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author chen weijie
10+
* @date 2018-04-16 12:27 AM
11+
*/
12+
public class MyList {
13+
14+
private List list = new ArrayList();
15+
16+
synchronized public void add(String userName) {
17+
System.out.println("ThreadName = " + Thread.currentThread().getName() + "执行了add方法!");
18+
list.add(userName);
19+
System.out.println("ThreadName = " + Thread.currentThread().getName() + "退出了add方法!");
20+
}
21+
22+
synchronized public int getSize() {
23+
System.out.println("ThreadName = " + Thread.currentThread().getName() + "执行了getSize方法!");
24+
int sizeValue = list.size();
25+
System.out.println("ThreadName = " + Thread.currentThread().getName() + "退出了getSize方法!");
26+
return sizeValue;
27+
}
28+
29+
30+
}
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.syn_out_asyn;
2+
3+
/**
4+
* 结果是成对出现的,所以方法是同步执行的
5+
* <p>
6+
* 同步快中的代码是同步打印的,当前线程执行是成对出现的.但线程A和线程B的执行却是异步的,这样可能出现脏读的环境.
7+
*
8+
* @author chen weijie
9+
* @date 2018-04-16 12:35 AM
10+
*/
11+
public class Test {
12+
13+
public static void main(String[] args) {
14+
15+
MyList list = new MyList();
16+
ThreadA threadA = new ThreadA(list);
17+
ThreadB threadB = new ThreadB(list);
18+
threadA.setName("A");
19+
threadB.setName("B");
20+
threadA.start();
21+
threadB.start();
22+
}
23+
}

0 commit comments

Comments
 (0)