?? producer.java
字號:
package book.thread.product;
/**
* 生產者類,采用線程,模擬生產者的行為
*/
class Producer extends Thread {
// 生產者存儲產品的倉庫
private Warehouse warehouse;
// 產品的名字
private static int produceName = 0;
//是否需要結束線程的標志位
private boolean running = false;
public Producer(Warehouse warehouse, String name) {
super(name);
this.warehouse = warehouse;
}
public void start(){
this.running = true;
super.start();
}
public void run() {
Product product;
try {
while (running) {
//生產并存儲產品
product = new Product((++produceName) + "");
this.warehouse.storageProduct(product);
sleep(300);
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
/**
* 停止生產者線程
*/
public void stopProducer(){
synchronized (warehouse){
this.running = false;
//通知等待倉庫的線程
warehouse.notifyAll();
}
}
//生產者線程是否在運行
public boolean isRunning() {
return running;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -