Java多线程详解
程序员文章站
2022-05-04 16:25:17
...
线程基本概念
线程是一个程序内部的顺序控制流
也就是说线程是一个程序里不同的执行路径,理解这个概念就足够了。
线程和进程的区别
- 每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销。
- 线程可以看成轻量级的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换开销小。
- 多进程:操作系统能同时运行多个任务(程序)。
多线程:在同一个应用程序中有多个顺序流同时执行。
一个时间点,单核单CPU只能有一个线程执行,由于CPU运行能力强,CPU将自己分为很多个时间片,一个时间片执行一会这个线程,另一个时间片又执行一会另一个线程。
- Java线程是通过Java.lang.Thread类来实现的。
- VM启动时会有一个由主方法(public static void main(){})所定义的线程。
- 可以通过创建Thread的实例来创建新的线程。
- 每个线程都是通过特定的Thread对象所对应的方法run()来完成其操作的,方法run()称为线程体。
-
通过调用Thread类的start()方法来启动一个线程。
注意:Java代码里面创建线程可以通过继承Thread类,或者实现Runnable接口两种方式,启动都需要Thread对象的start()方法来执行,推荐使用实现runnable接口来创建线程,因为继承只能继承一次,实现接口可以多次,更加灵活。
线程状态转换
线程控制基本方法
public class MyThread extends Thread {
@Override
public void run() {
while (true) {
System.out.println("--->>>" + new Date() + "<<<---");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}
public class TestSleep {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
//当线程在睡眠中调用此方法,将会导致抛出InterruptedException异常
thread.interrupt();
}
}
上面代码输出结果:
--->>>Tue Mar 13 22:38:56 CST 2018<<<---
--->>>Tue Mar 13 22:38:57 CST 2018<<<---
--->>>Tue Mar 13 22:38:58 CST 2018<<<---
--->>>Tue Mar 13 22:38:59 CST 2018<<<---
--->>>Tue Mar 13 22:39:00 CST 2018<<<---
--->>>Tue Mar 13 22:39:01 CST 2018<<<---
--->>>Tue Mar 13 22:39:02 CST 2018<<<---
--->>>Tue Mar 13 22:39:03 CST 2018<<<---
--->>>Tue Mar 13 22:39:04 CST 2018<<<---
--->>>Tue Mar 13 22:39:05 CST 2018<<<---
Join方法测试:
public class MyThread2 extends Thread {
public MyThread2(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("I am " + getName());
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}
public class TestJoin {
public static void main(String[] args) {
MyThread2 mt = new MyThread2("子线程");
mt.start();
try {
mt.join();
} catch (InterruptedException e) {}
for (int i = 0; i < 10; i++) {
System.out.println("I am main Thread");
}
}
}
输出结果是:
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am 子线程
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
I am main Thread
yield方法测试:
public class MyThread3 extends Thread {
public MyThread3(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
System.out.println(getName() + ": " + i);
if (i % 10 == 0) {
yield();
}
}
}
}
public class TestYield {
public static void main(String[] args) {
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start();
t2.start();
}
}
输出结果:
t1: 0
t1: 1
t1: 2
t1: 3
t1: 4
t2: 0
t2: 1
t2: 2
t2: 3
t1: 5
t1: 6
t1: 7
t1: 8
t2: 4
t1: 9
t2: 5
t2: 6
t2: 7
t2: 8
t2: 9
t2: 10
t1: 10
t2: 11
t1: 11
t2: 12
t2: 13
t2: 14
t2: 15
t2: 16
t2: 17
t2: 18
t2: 19
t2: 20
t1: 12
线程优先级别,优先级越高的线程,得到的CPU执行时间越长。
public class Thread1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("T1: " + i);
}
}
}
public class Thread2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("T2: " + i);
}
}
}
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new Thread1());
Thread t2 = new Thread(new Thread2());
// t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
上面注释掉t1.setPriority方法将输出:
T1: 9
T1: 10
T1: 11
T1: 12
T2: 0
T2: 1
T2: 2
T2: 3
T2: 4
T1: 13
T1: 14
T1: 15
T1: 16
T1: 17
T1: 18
T1: 19
T1: 20
T1: 21
T1: 22
T1: 23
T2: 5
T2: 6
T2: 7
T2: 8
T2: 9
T1: 24
T1: 25
T1: 26
T1: 27
T1: 28
T1: 29
T1: 30
T1: 31
T1: 32
T1: 33
T1: 34
T1: 35
T1: 36
T1: 37
T1: 38
T1: 39
T1: 40
T1: 41
T1: 42
T1: 43
T2: 10
T2: 11
T1: 44
T1: 45
T1: 46
T2: 12
T2: 13
T2: 14
T2: 15
T2: 16
T2: 17
T2: 18
可以看出为设置优先级前,两个子线程各执行一会
注意:用更大的数字执行才明显
设置优先级后:
T1: 0
T1: 1
T2: 0
T1: 2
T1: 3
T1: 4
T1: 5
T1: 6
T1: 7
T1: 8
T2: 1
T1: 9
T2: 2
T2: 3
T2: 4
T2: 5
T2: 6
T2: 7
T2: 8
T2: 9
T2: 10
T2: 11
T2: 12
T1: 1
T2: 13
T2: 14
T2: 15
T1: 11
T1: 12
T1: 13
T1: 14
T1: 15
T1: 16
T1: 17
T1: 18
T1: 19
T2: 16
T2: 17
T2: 18
T2: 19
T2: 20
T1: 20
T1: 21
T1: 22
T1: 23
T1: 24
T1: 25
T1: 26
T1: 27
T1: 28
T1: 29
T1: 30
T1: 31
T1: 32
T1: 33
T1: 34
T1: 35
T1: 36
T1: 37
T1: 38
T1: 39
T1: 40
T1: 41
T1: 42
T1: 43
T1: 44
T1: 45
T1: 46
T1: 47
T1: 48
T1: 49
T1: 50
T1: 51
T1: 52
T1: 53
T1: 54
T1: 55
T1: 56
T1: 57
T1: 58
T1: 59
T1: 60
T1: 61
T1: 62
T1: 63
T1: 64
T1: 65
T1: 66
T1: 67
T1: 68
T1: 69
T1: 70
T1: 71
T1: 72
T1: 73
T1: 74
T1: 75
T1: 76
T1: 77
T1: 78
T1: 79
T1: 80
T1: 81
T1: 82
T1: 83
T1: 84
T1: 85
T1: 86
T1: 87
T1: 88
T1: 89
T1: 90
T1: 91
T1: 92
T1: 93
T1: 94
T1: 95
T1: 96
T1: 97
T1: 98
T1: 99
T2: 21
T2: 22
T2: 23
T2: 24
T2: 25
T2: 26
T2: 27
T2: 28
T2: 29
T2: 30
T2: 31
T2: 32
T2: 33
T2: 34
T2: 35
T2: 36
T2: 37
T2: 38
T2: 39
T2: 40
T2: 41
T2: 42
T2: 43
T2: 44
T2: 45
T2: 46
T2: 47
T2: 48
T2: 49
T2: 50
T2: 51
T2: 52
T2: 53
T2: 54
T2: 55
T2: 56
T2: 57
T2: 58
T2: 59
T2: 60
T2: 61
T2: 62
T2: 63
T2: 64
T2: 65
T2: 66
T2: 67
T2: 68
T2: 69
T2: 70
T2: 71
T2: 72
T2: 73
T2: 74
T2: 75
T2: 76
T2: 77
T2: 78
T2: 79
T2: 80
T2: 81
T2: 82
T2: 83
T2: 84
T2: 85
T2: 86
T2: 87
T2: 88
T2: 89
T2: 90
T2: 91
T2: 92
T2: 93
T2: 94
T2: 95
T2: 96
T2: 97
T2: 98
T2: 99
T1执行的更加频繁,且先执行完
下一篇: Web后端开发知识点整理