zoukankan      html  css  js  c++  java
  • 多线程学习——消费者生产者(1)

    package com.daicy.ProducerConsumer;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ProducerConsumer {
    	public static void main(String[] args) {
    		executorThreads();
    		// baseThreads();
    	}
    
    	public static void executorThreads() {
    		IDStack stack = new DLinkedBlockingQueue<Mantou>();
    		ExecutorService exec = Executors.newFixedThreadPool(3);
    		exec.execute(new Consumer(stack));
    		exec.execute(new Consumer(stack));
    		exec.execute(new Producer(stack));
    		exec.execute(new Consumer(stack));
    		exec.execute(new Producer(stack));
    		// shutdownAndAwaitTermination(exec);
    		try {
    			TimeUnit.MILLISECONDS.sleep(10);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} // Run for a while...
    		exec.shutdownNow(); // Interrupt all tasks
    		// new Thread(new Consumer(stack)).start();
    		// new Thread(new Producer(stack)).start();
    		//
    		// new Thread(new Producer(stack)).start();
    		// new Thread(new Consumer(stack)).start();
    	}
    
    	public static void shutdownAndAwaitTermination(ExecutorService pool) {
    		pool.shutdown(); // Disable new tasks from being submitted
    		try {
    			// Wait a while for existing tasks to terminate
    			if (!pool.awaitTermination(60, TimeUnit.MILLISECONDS)) {
    				pool.shutdownNow(); // Cancel currently executing tasks
    				// Wait a while for tasks to respond to being cancelled
    				if (!pool.awaitTermination(60, TimeUnit.MILLISECONDS))
    					System.err.println("Pool did not terminate");
    			}
    		} catch (InterruptedException ie) {
    			// (Re-)Cancel if current thread also interrupted
    			pool.shutdownNow();
    			// Preserve interrupt status
    			Thread.currentThread().interrupt();
    		}
    	}
    
    	public static void baseThreads() {
    		IDStack stack = new DLinkedBlockingQueue<Mantou>();
    		List<Thread> list = new ArrayList();
    		list.add(new Thread(new Consumer(stack)));
    		list.add(new Thread(new Producer(stack)));
    		list.add(new Thread(new Producer(stack)));
    		list.add(new Thread(new Consumer(stack)));
    		for (int i = 0; i < list.size(); i++) {
    			list.get(i).start();
    		}
    
    		try {
    			TimeUnit.MILLISECONDS.sleep(10);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} // Run for a while...
    
    		for (int i = 0; i < list.size(); i++) {
    			list.get(i).interrupt();
    		}
    
    		// for (int i = 0; i < list.size(); i++) {
    		// list.get(i).stop();
    		// }
    	}
    }
    
    package com.daicy.ProducerConsumer;
    
    public class Consumer implements Runnable {
    	private IDStack<Mantou> stack;
    
    	private static int taskCount;
    
    	private final int id = taskCount++;
    
    	public Consumer(IDStack<Mantou> stack) {
    		this.stack = stack;
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		// while (true) {
    		while (!Thread.interrupted()) {
    			try {
    				Mantou mantou = stack.pop();
    				System.out.println("C" + this.id + " 消费了 mantou" + mantou);
    				// Thread.sleep((long) (Math.random() * 1000));
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				System.out.println("C" + this.id + " 消费了ttttt mantou");
    				break;
    			}
    		}
    
    	}
    }
    

    package com.daicy.ProducerConsumer;
    
    import com.daicy.sequence.SafeSequence;
    
    public class Producer implements Runnable {
    
    	private IDStack<Mantou> stack;
    
    	private static int taskCount;
    
    	private final int id = taskCount++;
    
    	public Producer(IDStack<Mantou> stack) {
    		this.stack = stack;
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		// while (true) {
    		while (!Thread.interrupted()) {
    			try {
    				int id = SafeSequence.getNext();
    				Mantou mantou = new Mantou(id);
    				stack.push(mantou);
    				System.out.println("P" + this.id + " 生产了 mantou" + mantou);
    				// Thread.sleep((long) (Math.random() * 1000));
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				System.out.println("P" + this.id + " 生产了 tttmantou");
    				break;
    			}
    		}
    	}
    }
    


  • 相关阅读:
    ios之单例模式
    ios之申请后台延时执行和做一个假后台的方法
    ios之gcd浅析
    ios之归档demo
    ios之快速领会VFL的demo
    ios之网络异常与正常视图的切换
    POJ 2280 Amphiphilic Carbon Molecules 极角排序 + 扫描线
    linux pthread
    directdraw显示yuv422(yuy2)
    nginx高效学习方法
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959315.html
Copyright © 2011-2022 走看看