Skip to content

Commit bccbe9d

Browse files
author
shiqifeng
committed
添加TreadTutorial到2.3
1 parent df3ed1d commit bccbe9d

4 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/4.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class DeadLock implements Runnable{
8+
9+
public String username;
10+
public Object lock1 = new Object();
11+
public Object lock2 = new Object();
12+
13+
public void setFlag(String username){
14+
this.username = username;
15+
}
16+
public void run() {
17+
if ("a".equals(username)){
18+
synchronized (lock1){
19+
try{
20+
System.out.println("username = " + username);
21+
Thread.sleep(3000);
22+
}catch (InterruptedException e){
23+
e.printStackTrace();
24+
}
25+
26+
synchronized (lock2){
27+
System.out.println("按照lock1 - > lock2代码顺序执行");
28+
}
29+
}
30+
}
31+
32+
if ("b".equals(username)){
33+
synchronized (lock2){
34+
try{
35+
System.out.println("username = " + username);
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+
}
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/4.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class OutClass {
8+
9+
public static class InnerClass1 {
10+
public void method1(InnerClass2 class2) {
11+
String threadName = Thread.currentThread().getName();
12+
synchronized (class2) {
13+
System.out.println(threadName + "进入InnerClass1类中的method1方法");
14+
for (int i = 1; i < 10; i++) {
15+
System.out.println(" i=" + i);
16+
try {
17+
Thread.sleep(100);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
System.out.println(threadName + "离开InnerClass1类中的method1方法");
23+
}
24+
}
25+
26+
synchronized public void method2() {
27+
String threadName = Thread.currentThread().getName();
28+
System.out.println(threadName + "进入InnerClass1类中的method2方法");
29+
for (int j = 0; j <= 10; j++) {
30+
System.out.println(" j=" + j);
31+
try {
32+
Thread.sleep(100);
33+
} catch (InterruptedException e) {
34+
35+
}
36+
}
37+
System.out.println(threadName +" 离开InnerClass1类中的method2方法");
38+
}
39+
}
40+
41+
public static class InnerClass2 {
42+
synchronized public void method1() {
43+
String threadName = Thread.currentThread().getName();
44+
System.out.println(threadName + "进入InnerClass2类中的method1方法");
45+
for (int k = 1; k < 10; k++) {
46+
System.out.println(" k=" + k);
47+
try {
48+
Thread.sleep(100);
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
53+
}
54+
System.out.println(threadName + " 离开InnerClass2类中的method1方法");
55+
}
56+
}
57+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.byhieg.threadtutorialtest.char02test;
2+
3+
import cn.byhieg.threadtutorial.char02.DeadLock;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2017/1/4.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class DeadLockTest extends TestCase {
11+
public void testRun() throws Exception {
12+
DeadLock deadLock = new DeadLock();
13+
deadLock.setFlag("a");
14+
15+
Thread threadA = new Thread(deadLock);
16+
threadA.start();
17+
Thread.sleep(100);
18+
19+
deadLock.setFlag("b");
20+
Thread threadB = new Thread(deadLock);
21+
threadB.start();
22+
23+
Thread.sleep(1000 * 150);
24+
25+
}
26+
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.byhieg.threadtutorialtest.char02test;
2+
3+
import cn.byhieg.threadtutorial.char02.OutClass;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by shiqifeng on 2017/1/4.
8+
* Mail byhieg@gmail.com
9+
*/
10+
public class InnerClass1Test extends TestCase {
11+
public void testMethod() throws Exception {
12+
final OutClass.InnerClass1 in1 = new OutClass.InnerClass1();
13+
final OutClass.InnerClass2 in2 = new OutClass.InnerClass2();
14+
15+
new Thread(new Runnable() {
16+
public void run() {
17+
in1.method1(in2);
18+
}
19+
},"T1").start();
20+
21+
new Thread(new Runnable() {
22+
public void run() {
23+
in1.method2();
24+
}
25+
},"T2").start();
26+
27+
new Thread(new Runnable() {
28+
public void run() {
29+
in2.method1();
30+
}
31+
},"T3").start();
32+
33+
Thread.sleep(1000 * 10);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)