• 多线程学习——消费者生产者(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;
    			}
    		}
    	}
    }
    


  • 相关阅读:
    CSS布局简史
    行块布局(如何理解Display:None,Block,Inline,Inline-Block)
    JQuery的基础学习
    在网页中添加一个可以收藏的功能
    PHPcms需要用到
    TP框架里面当访问不存在的操作方法时让其不显示错误页面(空控制器空操作)
    验证码上传文件
    关于API
    webapp的学习
    在ThinkPHP里面进行表单验证
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959315.html
Copyright © 2020-2023  润新知