Skip to content

Commit a37eefa

Browse files
committed
添加多线程的学习的例子
1 parent 085870b commit a37eefa

File tree

9 files changed

+141
-28
lines changed

9 files changed

+141
-28
lines changed

src/main/java/com/chen/api/util/thread/ReaderWriterList.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/com/chen/api/util/thread/IncDecThread.java renamed to src/main/java/com/chen/api/util/thread/introduce/IncDecThread.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package com.chen.api.util.thread;
1+
package com.chen.api.util.thread.introduce;
22

33
/**
4+
* 面试题
45
* @Author chenweijie
56
* @Date 2017/9/18 23:45
67
*/
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.chapter1.newRunnalble;
2+
3+
/**
4+
* 直接new Runnable创建多线程
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:43 AM
8+
*/
9+
public class NewRunnable {
10+
11+
12+
public static void main(String[] args) {
13+
14+
Runnable runnable = new Runnable() {
15+
@Override
16+
public void run() {
17+
System.out.println("124");
18+
}
19+
};
20+
new Thread(runnable).start();
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.chen.api.util.thread.study.chapter1.runnableTest;
2+
3+
/**
4+
* 测试实现Runnable接口实现多线程
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:37 AM
8+
*/
9+
public class MyRunnable implements Runnable {
10+
11+
@Override
12+
public void run() {
13+
System.out.println(Thread.currentThread().getName() + "...running!");
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chen.api.util.thread.study.chapter1.runnableTest;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:38 AM
8+
*/
9+
public class MyRunnableTest {
10+
11+
public static void main(String[] args) {
12+
13+
Runnable myRunnable = new MyRunnable();
14+
15+
Thread t = new Thread(myRunnable, "指定threadName的线程");
16+
17+
t.start();
18+
System.out.println("main 线程在运行..");
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.chen.api.util.thread.study.chapter1.threadTest;
2+
3+
/**
4+
* 继承thread类的多线程
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:20 AM
8+
*/
9+
public class MyThread extends Thread {
10+
11+
@Override
12+
public void run() {
13+
Thread.currentThread().setName("MyThread线程..");
14+
System.out.println(Thread.currentThread().getName() + "正在运行..");
15+
}
16+
17+
}
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.chapter1.threadTest;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:21 AM
8+
*/
9+
public class MyThreadTest {
10+
11+
public static void main(String[] args) {
12+
13+
MyThread myThread = new MyThread();
14+
myThread.start();
15+
16+
System.out.println("运行结束!");
17+
}
18+
}
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.chapter1.threadTest2;
2+
3+
/**
4+
* 测试执行start方法的顺序不代表代码的执行顺序
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:29 AM
8+
*/
9+
public class MyThread2 extends Thread {
10+
11+
private int i;
12+
13+
public MyThread2(int i) {
14+
super();
15+
this.i = i;
16+
}
17+
18+
@Override
19+
public void run() {
20+
System.out.println(i);
21+
}
22+
}
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.chapter1.threadTest2;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-08 1:32 AM
8+
*/
9+
public class MyThread2Test {
10+
11+
public static void main(String[] args) {
12+
MyThread2 t1 = new MyThread2(1);
13+
MyThread2 t2 = new MyThread2(2);
14+
MyThread2 t3 = new MyThread2(3);
15+
MyThread2 t4 = new MyThread2(4);
16+
MyThread2 t5 = new MyThread2(5);
17+
MyThread2 t6 = new MyThread2(6);
18+
t1.start();
19+
t2.start();
20+
t3.start();
21+
t4.start();
22+
t5.start();
23+
t6.start();
24+
}
25+
}

0 commit comments

Comments
 (0)