package com.ange.demo.thread;
public class ThreadTest {
static Thread t1,t2=null;
public static void main(String[] args){
System.out.print("hello");
t1=new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while(i<100){
i++;
System.out.println("t1:"+i);
if(i==10){
//这里join 在线程1中执行,使线程2先执行完再执行线程1
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
t2=new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while(i<10){
i++;
System.out.println("t2:"+i);
}
}
});
t1.start();
}
}
结果如下:
hellot1:1
t1:2
t1:3
t1:4
t1:5
t1:6
t1:7
t1:8
t1:9
t1:10
t2:1
t2:2
t2:3
t2:4
t2:5
t2:6
t2:7
t2:8
t2:9
t2:10
t1:11
t1:12
t1:13
t1:14
t1:15
t1:16
t1:17
t1:18
t1:19
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
t1:100
Process finished with exit code 0