Skip to content

Commit e55d8b4

Browse files
zhuzhu
authored andcommitted
线程间通讯完毕
1 parent 3fdf547 commit e55d8b4

File tree

9 files changed

+207
-0
lines changed

9 files changed

+207
-0
lines changed
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.chapter3.joinLong;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-15 23:53
6+
*/
7+
public class MyThread extends Thread {
8+
9+
10+
@Override
11+
public void run() {
12+
13+
try {
14+
System.out.println("begin time:" + System.currentTimeMillis());
15+
Thread.sleep(5000);
16+
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
21+
22+
}
23+
24+
25+
}
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.chapter3.joinLong;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-15 23:59
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
11+
try {
12+
MyThread myThread = new MyThread();
13+
myThread.start();
14+
myThread.join(2000);
15+
// Thread.sleep(2000);
16+
System.out.println("end time:" + System.currentTimeMillis());
17+
18+
} catch (Exception 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.chapter3.threadLocal11;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 00:35
6+
*/
7+
public class Run {
8+
9+
10+
public static final ThreadLocal threadLocal = new ThreadLocal();
11+
12+
public static void main(String[] args) {
13+
14+
if (threadLocal.get() == null) {
15+
System.out.println("从未放过值。。");
16+
threadLocal.set("我的值");
17+
}
18+
System.out.println(threadLocal.get());
19+
System.out.println(threadLocal.get());
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.chapter3.threadLocalExt;
2+
3+
/**
4+
* 解决初始值为null的问题。
5+
*
6+
* @author : chen weijie
7+
* @Date: 2018-05-16 01:06
8+
*/
9+
public class Run {
10+
11+
public static ThreadLocalExt threadLocalExt = new ThreadLocalExt();
12+
13+
public static void main(String[] args) {
14+
15+
if (threadLocalExt.get() == null) {
16+
threadLocalExt.set("我的值");
17+
}
18+
System.out.println(threadLocalExt.get());
19+
System.out.println(threadLocalExt.get());
20+
21+
}
22+
23+
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chen.api.util.thread.study.chapter3.threadLocalExt;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 01:04
6+
*/
7+
public class ThreadLocalExt extends ThreadLocal {
8+
9+
10+
11+
@Override
12+
public Object initialValue(){
13+
14+
return "我是init value";
15+
16+
}
17+
18+
}
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.chapter3.threadLocalTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 00:50
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
ThreadA threadA = new ThreadA();
12+
ThreadB threadB = new ThreadB();
13+
threadA.start();
14+
threadB.start();
15+
16+
for (int i = 0; i < 200; i++) {
17+
18+
Tools.t1.set("main thread :" + (i + 1));
19+
System.out.println("main thread get:" + Tools.t1.get());
20+
21+
}
22+
23+
24+
}
25+
26+
}
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.chapter3.threadLocalTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 00:47
6+
*/
7+
public class ThreadA extends Thread {
8+
9+
10+
11+
@Override
12+
public void run(){
13+
try {
14+
for (int i = 0; i < 100; i++) {
15+
Tools.t1.set("ThradA :" + (i + 1));
16+
System.out.println("ThreadA get value:" + Tools.t1.get());
17+
Thread.sleep(200);
18+
}
19+
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
24+
25+
}
26+
27+
}
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.chapter3.threadLocalTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 00:47
6+
*/
7+
public class ThreadB extends Thread {
8+
9+
10+
11+
@Override
12+
public void run(){
13+
try {
14+
for (int i = 0; i < 100; i++) {
15+
Tools.t1.set("ThradB :" + (i + 1));
16+
System.out.println("ThradB get value:" + Tools.t1.get());
17+
Thread.sleep(200);
18+
}
19+
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
24+
25+
}
26+
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.chen.api.util.thread.study.chapter3.threadLocalTest;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-16 00:45
6+
*/
7+
public class Tools {
8+
9+
public static ThreadLocal t1 =new ThreadLocal();
10+
11+
}

0 commit comments

Comments
 (0)