sleep(时间)线程阻塞毫秒数
sleep存在异常InterruptedException
sleep时间达到后线程进入就绪状态
sleep可以模拟网络延时,倒计时
每一个对象都有一个锁,sleep不会释放锁
package com.wuming.state;import com.wuming.demo01.TestThread4;//模拟网络延时:放大问题的发生性。 public class TestSleep implements Runnable{/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.** @see Thread#run()*///票数private int ticketNums=10;@Overridepublic void run() {while(true){if (ticketNums<=0){break;}//模拟延时try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");}}public static void main(String[] args) {TestSleep ticket = new TestSleep();new Thread(ticket,"小明").start();new Thread(ticket,"老师").start();new Thread(ticket,"黄牛党").start();} }
黄牛党-->拿到了第10票
老师-->拿到了第9票
小明-->拿到了第8票
老师-->拿到了第7票
黄牛党-->拿到了第5票
小明-->拿到了第6票
小明-->拿到了第4票
黄牛党-->拿到了第3票
老师-->拿到了第2票
小明-->拿到了第0票
老师-->拿到了第1票
黄牛党-->拿到了第-1票