以消費者,生產者例子舉例。
一、定義線程類
通過繼承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();