package com.jin;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestMultipleThread {
public static void main(String[] args) {
Production shareList = new Production();
ExecutorService executorService = Executors.newFixedThreadPool(6);
Consume consume1 = new Consume(shareList);
Consume consume2 = new Consume(shareList);
Consume consume3 = new Consume(shareList);
Consume consume4 = new Consume(shareList);
ProviderP p1 = new ProviderP(shareList);
ProviderP p2 = new ProviderP(shareList);
executorService.execute(p1);
executorService.execute(p2);
executorService.execute(consume2);
executorService.execute(consume1);
executorService.execute(consume3);
//executorService.execute(consume4);
}
static class Production {
LinkedList<String> shareList = new LinkedList<String>();
public synchronized void take() {
if (this.shareList.size() > 0) {
System.out.println("fetch data:" + this.shareList.pollFirst() + ",thread name:" + Thread.currentThread().getName());
if (this.shareList.size() <40)
notify();
} else {
try {
System.out.println("take等待");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public synchronized void put() {
if (this.shareList.size() ==100) {
try {
System.out.println("put等待");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
String data = "" + Math.random();
System.out.println("put:size:" + this.shareList.size() + "data:" +data +",thread name:" + Thread.currentThread().getName());
this.shareList.addLast(data);
notify();
}
}
}
static class Consume implements Runnable {
private Production shareList;
Consume (Production shareList) {
this.shareList = shareList;
}
public void run() {
while (true) {
this.shareList.take();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static class ProviderP implements Runnable{
private Production shareList;
ProviderP (Production shareList) {
this.shareList = shareList;
}
public void run() {
while (true) {
this.shareList.put();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
第二种方式:
package com.jin;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class TakeMultipleThread {
public static void main(String[] a) {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
ExecutorService executorService = Executors.newFixedThreadPool(6);
Consumer consume1 = new Consumer(queue);
Consumer consume2 = new Consumer(queue);
Consumer consume3 = new Consumer(queue);
Consumer consume4 = new Consumer(queue);
Provider p1 = new Provider(queue);
Provider p2 = new Provider(queue);
executorService.execute(p1);
executorService.execute(p2);
executorService.execute(consume2);
executorService.execute(consume1);
//executorService.execute(consume3);
//executorService.execute(consume4);
}
private static class Provider implements Runnable{
private BlockingQueue<String> blockingQueue;
Provider (BlockingQueue<String> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void run() {
while (true) {
String random = "" + Math.random();
this.blockingQueue.offer(random);
System.out.println("put:size:" + this.blockingQueue.size() + "data:" +random +",thread name:" + Thread.currentThread().getName());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static class Consumer implements Runnable{
private BlockingQueue<String> blockingQueue;
Consumer (BlockingQueue<String> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void run() {
while (true) {
String random;
try {
random = this.blockingQueue.take();
System.out.println("fetch:size:" + this.blockingQueue.size() + "data:" +random +",thread name:" + Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(230);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}