哪家公司做网站开发做得比较好,佛山商城网站制作,宁波网站建站的公司,上海对外贸易公司sleep#xff08;时间#xff09;线程阻塞毫秒数
sleep存在异常InterruptedException
sleep时间达到后线程进入就绪状态
sleep可以模拟网络延时#xff0c;倒计时
每一个对象都有一个锁#xff0c;sleep不会释放锁
package com.wuming.state;import com.wuming.demo01…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 codeRunnable/code is used* to create a thread, starting the thread causes the objects* coderun/code method to be called in that separately executing* thread.* p* The general contract of the method coderun/code is that it may* take any action whatsoever.** see Thread#run()*///票数private int ticketNums10;Overridepublic void run() {while(true){if (ticketNums0){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票