Skip to content

Commit bca772d

Browse files
author
shiqifeng
committed
添加ThreadTutorial2.2.4
1 parent bc2a2cb commit bca772d

15 files changed

Lines changed: 338 additions & 0 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class CommonUtils {
8+
9+
public static long beginTime1;
10+
public static long endTime1;
11+
12+
public static long beginTime2;
13+
public static long endTime2;
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class FatherSynService {
8+
9+
public int i = 10;
10+
synchronized public void operateIMainMethod(){
11+
try{
12+
i--;
13+
System.out.println("main print i=" +i);
14+
Thread.sleep(100);
15+
}catch (InterruptedException e){
16+
e.printStackTrace();
17+
}
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class HalfSynTask {
8+
9+
public void doLongTimeTask(){
10+
for(int i = 0 ;i < 4;i++){
11+
System.out.println("nosynchronized threadName=" + Thread.currentThread().getName() + " i=" + (i + 1));
12+
try {
13+
Thread.sleep(1000);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
18+
}
19+
System.out.println("");
20+
21+
synchronized (this){
22+
for (int i = 0 ; i < 4;i++){
23+
System.out.println("synchronized threadName=" +Thread.currentThread().getName() + " i=" + (i + 1));
24+
}
25+
}
26+
27+
for (int i = 1000 ; i < 1005;i ++){
28+
System.out.println("after synchronize =" + Thread.currentThread().getName() + " i=" + (i + 1));
29+
try {
30+
Thread.sleep(1000);
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class HalfTaskThread extends Thread{
8+
9+
private HalfSynTask task;
10+
11+
public HalfTaskThread(HalfSynTask task){
12+
this.task = task;
13+
}
14+
15+
@Override
16+
public void run() {
17+
super.run();
18+
task.doLongTimeTask();
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class LongTimeServiceThreadA extends Thread{
8+
9+
private LongTimeTask task;
10+
public LongTimeServiceThreadA(LongTimeTask task){
11+
super();
12+
this.task = task;
13+
}
14+
15+
@Override
16+
public void run() {
17+
super.run();
18+
CommonUtils.beginTime1 = System.currentTimeMillis();
19+
task.doLongTimeTask();
20+
CommonUtils.endTime1 = System.currentTimeMillis();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class LongTimeServiceThreadB extends Thread{
8+
9+
private LongTimeTask task;
10+
public LongTimeServiceThreadB(LongTimeTask task){
11+
super();
12+
this.task = task;
13+
}
14+
15+
@Override
16+
public void run() {
17+
super.run();
18+
CommonUtils.beginTime2 = System.currentTimeMillis();
19+
task.doLongTimeTask();
20+
CommonUtils.endTime2 = System.currentTimeMillis();
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class LongTimeTask {
8+
private String getData1;
9+
private String getData2;
10+
11+
public void doLongTimeTask(){
12+
try{
13+
System.out.println("begin task");
14+
Thread.sleep(3000);
15+
String privateGetData1 = "长时间处理任务后从远程返回的值 1 threadName=" + Thread.currentThread().getName();
16+
String privateGetData2 = "长时间处理任务后从远程返回的值 2 threadName=" + Thread.currentThread().getName();
17+
18+
synchronized (this){
19+
getData1 = privateGetData1;
20+
getData2 = privateGetData2;
21+
}
22+
23+
System.out.println(getData1);
24+
System.out.println(getData2);
25+
System.out.println("end task");
26+
}catch (InterruptedException e){
27+
e.printStackTrace();
28+
}
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class SonSynService extends FatherSynService{
8+
9+
synchronized public void operateISubMethod(){
10+
try{
11+
while (i > 0){
12+
i--;
13+
System.out.println("sub print i=" + i);
14+
Thread.sleep(1000);
15+
this.operateIMainMethod();
16+
}
17+
}catch (InterruptedException e){
18+
e.printStackTrace();
19+
}
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class SonSynTread extends Thread{
8+
@Override
9+
public void run() {
10+
super.run();
11+
SonSynService son = new SonSynService();
12+
son.operateISubMethod();
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by shiqifeng on 2017/1/3.
5+
* Mail byhieg@gmail.com
6+
*/
7+
public class SynchronizedService {
8+
9+
synchronized public void service1(){
10+
System.out.println("service1");
11+
service2();
12+
}
13+
14+
synchronized public void service2(){
15+
System.out.println("service2");
16+
service3();
17+
}
18+
19+
synchronized public void service3(){
20+
System.out.println("service3");
21+
}
22+
}

0 commit comments

Comments
 (0)