public class ConditionTester {
?????
????? public static void main(String[] args) throws InterruptedException{
final Basket basket = new Basket();
//
定義一個(gè)
producer
??????????? Runnable producer = new Runnable() {
????????????????? public void run() {
??????????????????????? try {
????????????????????????????? basket.produce();
??????
?????????????????} catch (InterruptedException ex) {
????????????????????????????? ex.printStackTrace();
??????????????????????? }
????????????????? }
};
//
定義一個(gè)
consumer
??????????? Runnable consumer = new Runnable() {
????????????????? public void run() {
??????????????????????? try {
????????????????????????????? basket.consume();
??????????????????????? } catch (InterruptedException ex) {
????????????????????????????? ex.printStackTrace();
??????????????????????? }
????????????????? }
};
//
各產(chǎn)生
10
個(gè)
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();
????? }?????
}
|