让两个线程有先后执行顺序
public class Join1_One extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName() + " thread_one start" + new Date());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_one end "+ new Date());
}
}
public class Join2_Two extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_two start" + new Date());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_two end "+ new Date());
}
}
ThreadOne one = new ThreadOne();
ThreadTwo two = new ThreadTwo();
one.start();
two.start();
//结果
Thread-1 thread_two startMon Aug 16 17:51:28 GMT+08:00 2021
Thread-0 thread_one startMon Aug 16 17:51:28 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 17:51:33 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 17:51:33 GMT+08:00 2021
如果这样写的话线程one和线程two是交替执行的,但是不一定谁先启动
public class Join3_Two_In_One extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_one start" + new Date());
Join2_Two two = new Join2_Two();
two.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_one end "+ new Date());
}
}
public static void main(String[] args) {
Join3_Two_In_One one = new Join3_Two_In_One();
one.start();
}
//结果
Thread-0 thread_one startMon Aug 16 17:55:31 GMT+08:00 2021
Thread-1 thread_two startMon Aug 16 17:55:31 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 17:55:36 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 17:55:36 GMT+08:00 2021
或者
Thread-0 thread_one startMon Aug 16 17:57:37 GMT+08:00 2021
Thread-1 thread_two startMon Aug 16 17:57:37 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 17:57:42 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 17:57:42 GMT+08:00 2021
以上这样肯定保证two在one后面执行,但是谁先结束不一定
两个线程合并成一个线程
public class Join4_Two_Join_One extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_one start" + new Date());
Join2_Two two = new Join2_Two();
two.start();
try {
two.join();//线程2加入到线程1里直到线程2先执行完毕
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_one end "+ new Date());
}
}
public class Join6_Two_Join_One_MainTest {
public static void main(String[] args) {
Join4_Two_Join_One one = new Join4_Two_Join_One();
one.start();
}
}
//结果
Thread-0 thread_one start Mon Aug 16 16:26:56 GMT+08:00 2021
Thread-1 thread_two startMon Aug 16 16:26:56 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 16:27:01 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 16:27:01 GMT+08:00 2021
线程2加入到线程1里直到线程2先执行完毕再执行线程1
public class Join4_Two_Join_One extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_one start" + new Date());
Join2_Two two = new Join2_Two();
two.start();
try {
two.join(2000);//就等线程2 2000毫秒 超过就不等了
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_one end "+ new Date());
}
}
public class Join6_Two_Join_One_MainTest {
public static void main(String[] args) {
Join4_Two_Join_One one = new Join4_Two_Join_One();
one.start();
}
}
//结果
Thread-0 thread_one start Mon Aug 16 16:31:21 GMT+08:00 2021
Thread-1 thread_two startMon Aug 16 16:31:21 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 16:31:23 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 16:31:26 GMT+08:00 2021
线程2 join进来之后,只给2000ms 让他先执行,过时随机争抢
public class Join4_Two_Join_One extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_one start " + new Date());
Join2_Two two = new Join2_Two();
two.start();
try {
two.join();//线程2加入到线程1里
// two.join(2000);//就等线程2 2000毫秒 超过就不等了
two.join(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_one end "+ new Date());
}
}
public static void main(String[] args) {
Join4_Two_Join_One one = new Join4_Two_Join_One();
one.start();
}
//结果
Thread-0 thread_one start Mon Aug 16 18:01:08 GMT+08:00 2021
Thread-1 thread_two startMon Aug 16 18:01:08 GMT+08:00 2021
Thread-1 thread_two end Mon Aug 16 18:01:13 GMT+08:00 2021
Thread-0 thread_one end Mon Aug 16 18:01:13 GMT+08:00 2021
虽然等6秒但是如果线程2执行完毕的话就不需要等够6秒了,就可以继续执行线程1的内容了
看一下被锁定的情况
public class Join_Lock1_ThreadOne extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName() +" thread_one start: "+ new Date());
Join_Lock2_ThreadTwo two = new Join_Lock2_ThreadTwo();
two.start();
try {
two.join(2000);//线程2加入到线程1里, 线程2先执行。但是线程1只会等线程2 2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +" thread_one end: "+ new Date());
}
}
public class Join_Lock2_ThreadTwo extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_two start: "+ new Date());
Join_Lock3_ThreadThree three = new Join_Lock3_ThreadThree(this);
three.start();
try {
Thread.sleep(5000);//线程2 5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_two end: "+ new Date());
}
}
public class Join_Lock3_ThreadThree extends Thread{
private Join_Lock2_ThreadTwo two;
public Join_Lock3_ThreadThree(Join_Lock2_ThreadTwo two) {
this.two = two;
}
public void run(){
//在two执行过程中 one等待的过程中 three将two对象锁定
System.out.println(Thread.currentThread().getName() + " thread_three start: "+ new Date());
synchronized (two){
System.out.println(Thread.currentThread().getName() + " two is locked: "+ new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " two is free: "+ new Date());
}
System.out.println(Thread.currentThread().getName() + " thread_three end: "+ new Date());
}
}
public class Join_Lock_TestMain {
public static void main(String[] args) {
Join_Lock1_ThreadOne one = new Join_Lock1_ThreadOne();
one.start();
}
}
2000ms之后one想把two从自己的线程内删除two对象不在自己手里,发现对象被three线程锁定10000ms,one只能等着three将two对象释放后才能踢掉
//结果
Thread-0 thread_one start: Mon Aug 16 18:28:17 GMT+08:00 2021
Thread-1 thread_two start: Mon Aug 16 18:28:17 GMT+08:00 2021
Thread-2 thread_three start: Mon Aug 16 18:28:17 GMT+08:00 2021
Thread-2 two is locked: Mon Aug 16 18:28:17 GMT+08:00 2021
Thread-1 thread_two end: Mon Aug 16 18:28:22 GMT+08:00 2021
Thread-2 two is free: Mon Aug 16 18:28:27 GMT+08:00 2021
Thread-0 thread_one end: Mon Aug 16 18:28:27 GMT+08:00 2021
Thread-2 thread_three end: Mon Aug 16 18:28:27 GMT+08:00 2021
public class Join_Lock2_ThreadTwo extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() + " thread_two start: "+ new Date());
Join_Lock3_ThreadThree three = new Join_Lock3_ThreadThree(this);
three.start();
try {
three.join(5000);//线程3加入到线程2里, 线程3先执行。但是线程2只会等线程3 5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " thread_two end: "+ new Date());
}
}
//结果
Thread-0 thread_one start: Mon Aug 16 18:35:33 GMT+08:00 2021
Thread-1 thread_two start: Mon Aug 16 18:35:33 GMT+08:00 2021
Thread-2 thread_three start: Mon Aug 16 18:35:33 GMT+08:00 2021
Thread-2 two is locked: Mon Aug 16 18:35:33 GMT+08:00 2021
Thread-1 thread_two end: Mon Aug 16 18:35:38 GMT+08:00 2021
Thread-2 two is free: Mon Aug 16 18:35:43 GMT+08:00 2021
Thread-2 thread_three end: Mon Aug 16 18:35:43 GMT+08:00 2021
Thread-0 thread_one end: Mon Aug 16 18:35:43 GMT+08:00 2021
其中
Thread.sleep(5000);//线程2 sleep5秒
three.join(5000);//线程3加入到线程2里, 线程3先执行。但是线程2只会等线程3 5秒
都是说线程2 在5秒后会和线程3 去争夺资源