介绍
这篇文章介绍线程的一些基本方法
- join
- sleep
- yield
join方法
- 如果在正在运行的线程t1上调用t2的join()方法,那么t1会进行等待状态,直到t2完成它的执行。
- 如果join方法传递一个时间,那么表示t1只会等待该时间,就会往下继续执行。
我们以例子来说明:
public class ThreadExample implements Runnable {
public static void main(String[] args) throws InterruptedException {
System.out.println("master thread: " + Thread.currentThread().getName());
Thread t = new Thread(new ThreadExample());
t.start();
t.join(5000);
System.out.println("end");
}
@Override
public void run() {
System.out.println("runnable thread:" + Thread.currentThread().getName());
int i = 0;
while(i < 100) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (Interrupt