java 線程筆記

以消費者,生產者例子舉例。

一、定義線程類

通過繼承Runnable接口: class Producer implement Runnable{...}  。

需實現run方法 : public void run(){...} 。

還可通過extends Thread 方法定義線程類。


二、線程類常用方法,關鍵詞

this.wait() 等待。

notifyall() 喚醒其他所有線程。

Thread.sleep(500) 線程睡眠。

synchronized 同步方法: public synchronized void push(Food fd){}


三、具體使用

Producer p = new Producer();
Thread t = new Thread(p);
t.start();




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章