多线程休眠
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票
總結
 
                            
                        - 上一篇: C语言 指针数组 - C语言零基础入门教
- 下一篇: C语言 二维数组遍历 - C语言零基础入
