线程休眠—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