1.
package interview.thread; import java.util.LinkedList;
import java.util.Queue;
import org.apache.log4j.Logger; public class InterThreadCommunicationExample { public static void main(String args[]) { final Queue sharedQ = new LinkedList(); Thread producer = new Producer(sharedQ);
Thread consumer = new Consumer(sharedQ); producer.start();
consumer.start(); }
} class Producer extends Thread {
private static final Logger logger = Logger.getLogger(Producer.class);
private final Queue sharedQ; public Producer(Queue sharedQ) {
super("Producer");
this.sharedQ = sharedQ;
} @Override
public void run() { for (int i = 0; i < 4; i++) { synchronized (sharedQ) {
//waiting condition - wait until Queue is not empty
while (sharedQ.size() >= 1) {
try {
logger.debug("Queue is full, waiting");
sharedQ.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
logger.debug("producing : " + i);
sharedQ.add(i);
sharedQ.notify();
}
}
}
} class Consumer extends Thread {
private static final Logger logger = Logger.getLogger(Consumer.class);
private final Queue<Integer> sharedQ; public Consumer(Queue sharedQ) {
super("Consumer");
this.sharedQ = sharedQ;
} @Override
public void run() {
while(true) { synchronized (sharedQ) {
//waiting condition - wait until Queue is not empty
while (sharedQ.size() == 0) {
try {
logger.debug("Queue is empty, waiting");
sharedQ.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
int number = sharedQ.poll();
logger.debug("consuming : " + number );
sharedQ.notify(); //termination condition
if(number == 3){break; }
}
}
}
}
2.用BlockingQueue会自动处理生产者消费者问题
package interview.thread; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger; public class ProducerConsumerPattern { public static void main(String args[]){ //Creating shared object
BlockingQueue sharedQueue = new LinkedBlockingQueue(); //Creating Producer and Consumer Thread
Thread prodThread = new Thread(new Producer(sharedQueue));
Thread consThread = new Thread(new Consumer(sharedQueue)); //Starting producer and Consumer thread
prodThread.start();
consThread.start();
} } //Producer Class in java
class Producer implements Runnable { private final BlockingQueue sharedQueue; public Producer(BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
} @Override
public void run() {
for(int i=0; i<10; i++){
try {
System.out.println("Produced: " + i);
sharedQueue.put(i);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
} } //Consumer Class in Java
class Consumer implements Runnable{ private final BlockingQueue sharedQueue; public Consumer (BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
} @Override
public void run() {
while(true){
try {
System.out.println("Consumed: "+ sharedQueue.take());
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
3.两个线程打印奇偶数
package interview.thread; public class OddEven { public static void main(String[] args) {
Monitor m = new Monitor();
Thread task1 = new Thread(new Task1(m));
Thread task2 = new Thread(new Task2(m));
task1.start();
task2.start(); } } class Monitor {
private boolean flag = true; public boolean isFlag() {
return flag;
} public void setFlag(boolean flag) {
this.flag = flag;
} } class Task1 implements Runnable {
private Monitor monitor; public Task1(Monitor monitor) {
super();
this.monitor = monitor;
} @Override
public void run() {
for (int i = 0; i <= 10; i = i + 2) {
synchronized (monitor) {
while (!monitor.isFlag()) {
try {
monitor.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Printing :" + i);
monitor.setFlag(false);
monitor.notifyAll(); }
} } } class Task2 implements Runnable {
private Monitor monitor; public Task2(Monitor monitor) {
super();
this.monitor = monitor;
} @Override
public void run() {
for (int i = 1; i <= 10; i = i + 2) {
synchronized (monitor) {
while (monitor.isFlag()) {
try {
monitor.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
System.out.println("Printing :" + i);
monitor.setFlag(true);
monitor.notifyAll();
}
} } }
4.