join() 方法
- 当在主线程中执行 t1.join() 方法时,就认为主线程应该把执行权让给子线程 t1。
public class ThreadJoinDemo01 {
public static void main(String[] args) throws InterruptedException {
System.out.println("====程序开始====");
ThreadJoinDemo001 demo001 = new ThreadJoinDemo001();
Thread t1 = new Thread(demo001);
t1.start();
t1.join();
System.out.println("主线程开始");
for (int i = 0; i < 10; i++) {
System.out.println("主线程:" + i);
}
System.out.println("主线程结束");
}
}
class ThreadJoinDemo001 implements Runnable{
@Override
public void run() {
System.out.println("子线程- " + Thread.currentThread().getName() + " -开始");
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getId() + " : " + i);
}
System.out.println("子线程- " + Thread.currentThread().getName() + " -结束");
}
}
====程序开始====
子线程- Thread-0 -开始
12 : 0
12 : 1
12 : 2
12 : 3
12 : 4
12 : 5
12 : 6
12 : 7
12 : 8
12 : 9
子线程- Thread-0 -结束
主线程开始
主线程:0
主线程:1
主线程:2
主线程:3
主线程:4
主线程:5
主线程:6
主线程:7
主线程:8
主线程:9
主线程结束
Process finished with exit code 0
练习
- 现在有 T1、T2、T3 三个线程,你怎样保证 T2 在 T1 执行完后执行,T3 在 T2 执行完后执行。
public class ThreadJoinDemo02 {
public static void main(String[] args) {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("子线程 1 ");
}
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();
} catch (Exception e) {
throw new RuntimeException(e);
}
for (int i = 0; i < 3; i++) {
System.out.println("子线程 2 ");
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
t2.join();
} catch (Exception e) {
throw new RuntimeException(e);
}
for (int i = 0; i < 3; i++) {
System.out.println("子线程 3 ");
}
}
});
t1.start();
t2.start();
t3.start();
try {
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.println("主线程结束!");
}
}
}
子线程 1
子线程 1
子线程 1
子线程 2
子线程 2
子线程 2
子线程 3
子线程 3
子线程 3
主线程结束!
主线程结束!
主线程结束!
Process finished with exit code 0
线程的优先级
- 现代操作系统基本采用时分的形式调度运行的线程,线程分配得到的时间片的多少决定了线程使用处理器资源的多少,也对应了线程优先级这个概念。在 JAVA 线程中,通过一个 int priority 来控制优先级,范围为 1-10,其中 10 最高,默认值为 5。下面是源码(基于 1.8)中关于 priority 的一些量和方法。
public class ThreadDemo0001 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new ThreadDemo00001("子线程一");
Thread t2 = new ThreadDemo00001("子线程二");
t1.start();
t2.start();
t1.setPriority(10);
t2.setPriority(10);
System.out.println("主线程开始!!!");
for (int i = 0; i < 10; i++) {
System.out.println("主线程:" + i);
}
System.out.println("主线程结束!!!");
}
}
class ThreadDemo00001 extends Thread{
public ThreadDemo00001(String name){
super.setName(name);
}
@Override
public void run() {
System.out.println("子线程:" + Thread.currentThread().getName() + " 开始");
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": " + i);
}
System.out.println("子线程:" + Thread.currentThread().getName() + " 结束");
}
}
主线程开始!!!
主线程:0
子线程:子线程二 开始
子线程:子线程一 开始
主线程:1
主线程:2
主线程:3
主线程:4
主线程:5
主线程:6
主线程:7
主线程:8
主线程:9
主线程结束!!!
子线程二: 0
子线程一: 0
子线程二: 1
子线程一: 1
子线程二: 2
子线程一: 2
子线程一: 3
子线程二: 3
子线程二: 4
子线程一: 4
子线程一: 5
子线程二: 5
子线程二: 6
子线程一: 6
子线程一: 7
子线程二: 7
子线程一: 8
子线程二: 8
子线程一: 9
子线程二: 9
子线程:子线程二 结束
子线程:子线程一 结束
Process finished with exit code 0
END…