Sleep():在指定的毫秒数内让当前正在执行的线程休眠
调用sleep方法时使当前的线程进入休眠状态(阻塞状态)设定休眠xxxx毫秒数后进入运行状态
同步块中的Sleep方法调用 不会释放对象锁但调用wait()方法会释放对象的同步锁
package com.test.thread;
public class TestRunnable implements Runnable {
@Override
public synchronized void run() {
for (int i = 0; i < 6; i++) {
if (i % 2 == 0) {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.test.thread;
public class TestMain {
public static void main(String[] args) {
TestRunnable runnable_01=new TestRunnable();
Thread thread1=new Thread(runnable_01, "runnable_01");
thread1.start();
}
}