Java生产者消费者问题
1.
1 package interview.thread; 2 3 import java.util.LinkedList; 4 import java.util.Queue; 5 import org.apache.log4j.Logger; 6 7 public class InterThreadCommunicationExample { 8 9 public static void main(String args[]) { 10 11 final Queue sharedQ = new LinkedList(); 12 13 Thread producer = new Producer(sharedQ); 14 Thread consumer = new Consumer(sharedQ); 15 16 producer.start(); 17 consumer.start(); 18 19 } 20 } 21 22 class Producer extends Thread { 23 private static final Logger logger = Logger.getLogger(Producer.class); 24 private final Queue sharedQ; 25 26 public Producer(Queue sharedQ) { 27 super("Producer"); 28 this.sharedQ = sharedQ; 29 } 30 31 @Override 32 public void run() { 33 34 for (int i = 0; i < 4; i++) { 35 36 synchronized (sharedQ) { 37 //waiting condition - wait until Queue is not empty 38 while (sharedQ.size() >= 1) { 39 try { 40 logger.debug("Queue is full, waiting"); 41 sharedQ.wait(); 42 } catch (InterruptedException ex) { 43 ex.printStackTrace(); 44 } 45 } 46 logger.debug("producing : " + i); 47 sharedQ.add(i); 48 sharedQ.notify(); 49 } 50 } 51 } 52 } 53 54 class Consumer extends Thread { 55 private static final Logger logger = Logger.getLogger(Consumer.class); 56 private final Queue<Integer> sharedQ; 57 58 public Consumer(Queue sharedQ) { 59 super("Consumer"); 60 this.sharedQ = sharedQ; 61 } 62 63 @Override 64 public void run() { 65 while(true) { 66 67 synchronized (sharedQ) { 68 //waiting condition - wait until Queue is not empty 69 while (sharedQ.size() == 0) { 70 try { 71 logger.debug("Queue is empty, waiting"); 72 sharedQ.wait(); 73 } catch (InterruptedException ex) { 74 ex.printStackTrace(); 75 } 76 } 77 int number = sharedQ.poll(); 78 logger.debug("consuming : " + number ); 79 sharedQ.notify(); 80 81 //termination condition 82 if(number == 3){break; } 83 } 84 } 85 } 86 }
2.用BlockingQueue会自动处理生产者消费者问题
1 package interview.thread; 2 3 import java.util.concurrent.BlockingQueue; 4 import java.util.concurrent.LinkedBlockingQueue; 5 import java.util.logging.Level; 6 import java.util.logging.Logger; 7 8 public class ProducerConsumerPattern { 9 10 public static void main(String args[]){ 11 12 //Creating shared object 13 BlockingQueue sharedQueue = new LinkedBlockingQueue(); 14 15 //Creating Producer and Consumer Thread 16 Thread prodThread = new Thread(new Producer(sharedQueue)); 17 Thread consThread = new Thread(new Consumer(sharedQueue)); 18 19 //Starting producer and Consumer thread 20 prodThread.start(); 21 consThread.start(); 22 } 23 24 } 25 26 //Producer Class in java 27 class Producer implements Runnable { 28 29 private final BlockingQueue sharedQueue; 30 31 public Producer(BlockingQueue sharedQueue) { 32 this.sharedQueue = sharedQueue; 33 } 34 35 @Override 36 public void run() { 37 for(int i=0; i<10; i++){ 38 try { 39 System.out.println("Produced: " + i); 40 sharedQueue.put(i); 41 } catch (InterruptedException ex) { 42 Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); 43 } 44 } 45 } 46 47 } 48 49 //Consumer Class in Java 50 class Consumer implements Runnable{ 51 52 private final BlockingQueue sharedQueue; 53 54 public Consumer (BlockingQueue sharedQueue) { 55 this.sharedQueue = sharedQueue; 56 } 57 58 @Override 59 public void run() { 60 while(true){ 61 try { 62 System.out.println("Consumed: "+ sharedQueue.take()); 63 } catch (InterruptedException ex) { 64 Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); 65 } 66 } 67 } 68 69 70 }
3.两个线程打印奇偶数
1 package interview.thread; 2 3 public class OddEven { 4 5 public static void main(String[] args) { 6 Monitor m = new Monitor(); 7 Thread task1 = new Thread(new Task1(m)); 8 Thread task2 = new Thread(new Task2(m)); 9 task1.start(); 10 task2.start(); 11 12 } 13 14 } 15 16 class Monitor { 17 private boolean flag = true; 18 19 public boolean isFlag() { 20 return flag; 21 } 22 23 public void setFlag(boolean flag) { 24 this.flag = flag; 25 } 26 27 } 28 29 class Task1 implements Runnable { 30 private Monitor monitor; 31 32 public Task1(Monitor monitor) { 33 super(); 34 this.monitor = monitor; 35 } 36 37 @Override 38 public void run() { 39 for (int i = 0; i <= 10; i = i + 2) { 40 synchronized (monitor) { 41 while (!monitor.isFlag()) { 42 try { 43 monitor.wait(); 44 } catch (InterruptedException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 } 49 System.out.println("Printing :" + i); 50 monitor.setFlag(false); 51 monitor.notifyAll(); 52 53 } 54 } 55 56 } 57 58 } 59 60 class Task2 implements Runnable { 61 private Monitor monitor; 62 63 public Task2(Monitor monitor) { 64 super(); 65 this.monitor = monitor; 66 } 67 68 @Override 69 public void run() { 70 for (int i = 1; i <= 10; i = i + 2) { 71 synchronized (monitor) { 72 while (monitor.isFlag()) { 73 try { 74 monitor.wait(); 75 } catch (InterruptedException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 80 } 81 System.out.println("Printing :" + i); 82 monitor.setFlag(true); 83 monitor.notifyAll(); 84 } 85 } 86 87 } 88 89 }
4.
You can do anything you set your mind to, man!