conditiontester.java

来自「用NETBEANS做的一个关于Java的小小的demo.大家赐教」· Java 代码 · 共 54 行

JAVA
54
字号
/*
 * ConditionTester.java
 *
 * Created on 2007年9月14日, 上午10:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package product_consumer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author Administrator
 */
public class ConditionTester {
    
    public static void main(String[] args) throws InterruptedException{
        final Basket basket = new Basket();
//定义一个producer
        Runnable producer = new Runnable() {
            public void run() {
                try {
                    basket.produce();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        };
//定义一个consumer
        Runnable consumer = new Runnable() {
            public void run() {
                try {
                    basket.consume();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        };
//各产生10个consumer和producer
        ExecutorService service = Executors.newCachedThreadPool();
        for(int i=0; i < 10; i++)
            service.submit(consumer);
        Thread.sleep(2000);
        for(int i=0; i<10; i++)
            service.submit(producer);
        service.shutdown();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?