Skip to content

Commit f236dd3

Browse files
committed
添加多线程的基本API
1 parent 79823e0 commit f236dd3

File tree

17 files changed

+443
-0
lines changed

17 files changed

+443
-0
lines changed
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.chapter1.daemonThread;
2+
3+
/**
4+
* 守护线程
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-11 1:59 AM
8+
*/
9+
public class MyThread extends Thread {
10+
11+
private int i = 0;
12+
13+
@Override
14+
public void run() {
15+
while (true) {
16+
try {
17+
i++;
18+
System.out.println("i=" + i);
19+
Thread.sleep(1000);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
25+
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.thread.study.chapter1.daemonThread;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-11 2:01 AM
8+
*/
9+
public class Test {
10+
11+
12+
public static void main(String[] args) {
13+
14+
try {
15+
MyThread myThread = new MyThread();
16+
myThread.setDaemon(true);
17+
myThread.start();
18+
Thread.sleep(10000);
19+
20+
System.out.println("我离开daemon对象也不在打印了,也就是停止了");
21+
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
26+
27+
}
28+
}

src/main/java/com/chen/api/util/thread/study/chapter1/newRunnalble/NewRunnable.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ public class NewRunnable {
1111

1212
public static void main(String[] args) {
1313

14+
//创建线程的一种方式
1415
Runnable runnable = new Runnable() {
1516
@Override
1617
public void run() {
1718
System.out.println("124");
1819
}
1920
};
2021
new Thread(runnable).start();
22+
23+
//创建线程的另一种方式
24+
Thread thread = new Thread() {
25+
26+
@Override
27+
public void run() {
28+
System.out.println("Thread 直接创建线程 ");
29+
}
30+
};
31+
thread.start();
32+
2133
}
2234
}
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.priorityThread;
2+
3+
/**
4+
* 线程优先级 线程优先级的继承性: 比如A线程启动B线程,则B线程的优先级与A是一样的.
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-11 1:08 AM
8+
*/
9+
public class MyThread extends Thread {
10+
11+
@Override
12+
public void run() {
13+
System.out.println("my thread1 priority is " + this.getPriority());
14+
MyThread2 thread2 = new MyThread2();
15+
thread2.start();
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.chen.api.util.thread.study.chapter1.priorityThread;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-11 1:10 AM
6+
*/
7+
public class MyThread2 extends Thread {
8+
9+
@Override
10+
public void run() {
11+
System.out.println("MyThread2 run priority is " + this.getPriority());
12+
}
13+
14+
}
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.chapter1.priorityThread;
2+
3+
/**
4+
* 测试类
5+
*
6+
* @author chen weijie
7+
* @date 2018-04-11 1:14 AM
8+
*/
9+
public class Test {
10+
11+
public static void main(String[] args) {
12+
13+
System.out.println("main thread priority begin=" + Thread.currentThread().getPriority());
14+
Thread.currentThread().setPriority(6);
15+
System.out.println("main thread priority end=" + Thread.currentThread().getPriority());
16+
17+
//main线程把thread1启动,此后 线程1的线程优先级为6 此后线程1把线程2启动,线程2的优先级为6
18+
MyThread thread1 = new MyThread();
19+
thread1.start();
20+
21+
22+
}
23+
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chen.api.util.thread.study.chapter1.priorityThread2;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* cpu会优先执行优先级比较高的线程对象中的任务.所以优先级高的线程会比优先级低的线程获取更多的cpu时间
7+
*
8+
* @author chen weijie
9+
* @date 2018-04-11 1:23 AM
10+
*/
11+
public class MyThread extends Thread {
12+
13+
@Override
14+
public void run() {
15+
16+
long beginTime = System.currentTimeMillis();
17+
long result = 0;
18+
for (int j = 0; j < 10; j++) {
19+
for (int i = 0; i < 50000; i++) {
20+
Random random = new Random();
21+
random.nextInt();
22+
result = result + i;
23+
}
24+
long endTime = System.currentTimeMillis();
25+
System.out.println("time====================A===" + (endTime - beginTime));
26+
}
27+
28+
}
29+
}
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.chapter1.priorityThread2;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-04-11 1:23 AM
8+
*/
9+
public class MyThread2 extends Thread {
10+
11+
@Override
12+
public void run() {
13+
14+
long beginTime = System.currentTimeMillis();
15+
long result = 0;
16+
for (int j = 0; j < 10; j++) {
17+
for (int i = 0; i < 50000; i++) {
18+
Random random = new Random();
19+
random.nextInt();
20+
result = result + i;
21+
}
22+
long endTime = System.currentTimeMillis();
23+
System.out.println("time====================B==" + (endTime - beginTime));
24+
}
25+
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.api.util.thread.study.chapter1.priorityThread2;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-11 1:27 AM
6+
*/
7+
public class Test {
8+
9+
10+
public static void main(String[] args) {
11+
12+
MyThread thread = new MyThread();
13+
thread.setPriority(10);
14+
thread.start();
15+
MyThread2 thread2 = new MyThread2();
16+
thread.setPriority(1);
17+
thread2.start();
18+
}
19+
20+
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.chen.api.util.thread.study.chapter1.priorityThread3;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-04-11 1:42 AM
6+
*/
7+
public class TestRun {
8+
9+
10+
public static void main(String[] args) {
11+
12+
try {
13+
ThreadA a = new ThreadA();
14+
ThreadB b = new ThreadB();
15+
a.setPriority(Thread.NORM_PRIORITY - 2);
16+
a.start();
17+
b.setPriority(Thread.NORM_PRIORITY + 2);
18+
b.start();
19+
Thread.sleep(20000);
20+
21+
a.stop();
22+
b.stop();
23+
System.out.println("a=============" + a.getCount());
24+
System.out.println("b=============" + b.getCount());
25+
} catch (InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)