線程休眠—sleep方法


Thread.sleep(ms):當前線程進入Time—Wait狀態,並等待指定時間。

與wait的區別:wait只能用於同步塊中,wait釋放鎖。

class MyThread implements Runnable {
    public void run() {
        int i = 0;
        while(i < 6) {
            System.out.println(Thread.currentThread() + " a = " + i);
            i++;
            Thread.yield();
        }
    }
}
public class Demo1 {

    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(new MyThread(),"t1");
        Thread t2 = new Thread(new MyThread(),"t2");
        t1.start();
        t2.start();
        Thread.sleep(1);
        System.out.println(Thread.currentThread());
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM