线程的几个方法:
1. join() 线程的强制执行
当主线程在执行时,子线程执行join方法,那么子线程就会强制执行,直到子线程执行结束,主线程才会继续执行
package thread;
public class TestThreadJoin {
public static void main(String[] args) throws InterruptedException {
MyThread2 th = new MyThread2();
th.start();
for(int i=0; i<50; i++){
System.out.println(Thread.currentThread().getName() + ":" + i);
if(i>20){
th.join();
}
}
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=0; i<50; i++){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
执行结果:
main:0
Thread-0:0
main:1
Thread-0:1
main:2
Thread-0:2
main:3
Thread-0:3
main:4
Thread-0:4
main:5
Thread-0:5
main:6
Thread-0:6
main:7
Thread-0:7
main:8
Thread-0:8
main:9
Thread-0:9
main:10
Thread-0:10
main:11
Thread-0:11
main:12
Thread-0:12
main:13
Thread-0:13
main:14
Thread-0:14
main:15
Thread-0:15
main:16
Thread-0:16
main:17
Thread-0:17
main:18
Thread-0:18
main:19
Thread-0:19
main:20
Thread-0:20
main:21
Thread-0:21
Thread-0:22
Thread-0:23
Thread-0:24
Thread-0:25
Thread-0:26
Thread-0:27
Thread-0:28
Thread-0:29
Thread-0:30
Thread-0:31
Thread-0:32
Thread-0:33
Thread-0:34
Thread-0:35
Thread-0:36
Thread-0:37
Thread-0:38
Thread-0:39
Thread-0:40
Thread-0:41
Thread-0:42
Thread-0:43
Thread-0:44
Thread-0:45
Thread-0:46
Thread-0:47
Thread-0:48
Thread-0:49
main:22
main:23
main:24
main:25
main:26
main:27
main:28
main:29
main:30
main:31
main:32
main:33
main:34
main:35
main:36
main:37
main:38
main:39
main:40
main:41
main:42
main:43
main:44
main:45
main:46
main:47
main:48
main:49
2. sleep() 线程的睡眠
正在执行的线程,可以通过sleep() 方法,让线程沉睡指定的时间
3. yield() 线程的礼让
线程之间的礼让,如A线程执行该方法,那么就会让B线程执行一会;B线程执行该方法,那么就会让A线程执行一会
package thread;
public class TestThreadJoin {
public static void main(String[] args) throws InterruptedException {
MyThread2 th1 = new MyThread2();
MyThread2 th2 = new MyThread2();
th1.start();
th2.start();
}
}
class MyThread2 extends Thread{
public void run(){
for(int i=0; i<50; i++){
System.out.println(Thread.currentThread().getName() + ":" + i);
if(i==10){
System.out.println("礼让");
Thread.yield();
}
}
}
}
执行结果:
Thread-0:0
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9
Thread-1:10
礼让
Thread-0:1
Thread-0:2
Thread-0:3
Thread-1:11
Thread-0:4
Thread-1:12
Thread-0:5
Thread-1:13
Thread-0:6
Thread-1:14
Thread-0:7
Thread-1:15
Thread-0:8
Thread-1:16
Thread-0:9
Thread-1:17
Thread-0:10
礼让
Thread-1:18
Thread-0:11
Thread-1:19
Thread-0:12
Thread-1:20
Thread-0:13
Thread-1:21
Thread-0:14
Thread-1:22
Thread-0:15
Thread-1:23
Thread-0:16
Thread-1:24
Thread-0:17
Thread-1:25
Thread-0:18
Thread-1:26
Thread-0:19
Thread-1:27
Thread-0:20
Thread-1:28
Thread-0:21
Thread-1:29
Thread-0:22
Thread-1:30
Thread-0:23
Thread-1:31
Thread-0:24
Thread-1:32
Thread-0:25
Thread-1:33
Thread-0:26
Thread-1:34
Thread-0:27
Thread-1:35
Thread-0:28
Thread-1:36
Thread-0:29
Thread-1:37
Thread-0:30
Thread-1:38
Thread-0:31
Thread-1:39
Thread-0:32
Thread-1:40
Thread-0:33
Thread-1:41
Thread-0:34
Thread-1:42
Thread-0:35
Thread-1:43
Thread-0:36
Thread-1:44
Thread-0:37
Thread-1:45
Thread-0:38
Thread-1:46
Thread-0:39
Thread-1:47
Thread-0:40
Thread-1:48
Thread-0:41
Thread-1:49
Thread-0:42
Thread-0:43
Thread-0:44
Thread-0:45
Thread-0:46
Thread-0:47
Thread-0:48
Thread-0:49