線程休眠—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()); } }

浙公網安備 33010602011771號