线程控制基本方法
Static 静态的
Sleep/join/yield方法
#sleep方法
-可以调用Thread的静态方法:public static void
sleep(long millis) throws InterruptedException使得当前线程休眠(暂时停止millis毫秒) 使之时间片用完开始下一进程
-例:Thread/TestInterrupt.java
#join方法
-合并某个线程
#yield方法
-让出cpu,给其他线程执行的机会
-例:TestYield.java
public class TestYield {
public static void main(String[] args) {
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start(); t2.start();
}
}
class MyThread3 extends Thread {
MyThread3(String s){super(s);}
public void run(){
for(int i =1;i<=100;i++){
System.out.println(getName()+": "+i);
if(i%10==0){
yield();
}
}
}
}