1.启动三个线程,打印递增的数字,线程1打印1,2,3,4,5.线程2打印6,7,8,9,10,线程3打印11,12,13,14,15.接着再由线程1打印16,17,18,19,20,以此类推,直到打印到75
public class Test1 implements Runnable {
public static int outInt = 1;
public Integer id;
public Test1(Integer id) {
this.id = id;
}
public static Object lock = new Object();
@Override
public void run() {
synchronized (lock) {
while (true) {
while ((outInt - 1) / 5 % 3 + 1 != id) {
try {
lock.wait();
if (outInt > 75) {
lock.notifyAll();
return;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程" + id + ":" + outInt++);
lock.notifyAll();
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Test1(1));
Thread t2 = new Thread(new Test1(2));
Thread t3 = new Thread(new Test1(3));
t1.start();
t2.start();
t3.start();
}
}
2.单一的生产消费模型(多生产消费模型在下面):
public class Test2 {
public static void main(String[] args) {
final BlokingQueue<String> blokingQueue = new BlokingQueue<>();
Thread t1 = new Thread(() -> {
while (true) {
try {
System.out.println(blokingQueue.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(() -> {
int i = 0;
while (true) {
blokingQueue.add("ABC" + i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}
}
class BlokingQueue<T> {
private LinkedList<T> linkedList = new LinkedList<>();
public synchronized T get() throws InterruptedException {
while (linkedList.size() == 0) {
wait();
}
T t = linkedList.getFirst();
linkedList.removeFirst();
return t;
}
public synchronized void add(T t) {
linkedList.add(t);
notifyAll();
}
}
3.多生产者消费者模型
public class Test3 {
public static void main(String[] args) {
ArrayBlokingQueue<String> arrayBlockingQueue = new ArrayBlokingQueue<>(20);
List<Runnable> adds = new ArrayList<>();
List<Runnable> gets = new ArrayList<>();
adds.add(new GetThread(arrayBlockingQueue));
adds.add(new GetThread(arrayBlockingQueue));
adds.add(new GetThread(arrayBlockingQueue));
gets.add(new AddThread(arrayBlockingQueue));
gets.add(new AddThread(arrayBlockingQueue));
gets.add(new AddThread(arrayBlockingQueue));
for (int i = 0; i < adds.size(); i++) {
new Thread(adds.get(i), "" + i).start();
}
for (int i = 0; i < gets.size(); i++) {
new Thread(gets.get(i), "" + i).start();
}
}
}
/**
* 生产消费队列阻塞
* @param <T>
*/
class ArrayBlokingQueue<T> {
private LinkedList<T> linkedList = new LinkedList<>();
private Integer queueSize;
private Lock lock = new ReentrantLock();
private Condition getCon = lock.newCondition();
private Condition addCon = lock.newCondition();
public ArrayBlokingQueue(Integer queueSize) {
this.queueSize = queueSize;
}
public T get() {
lock.lock();
while (linkedList.size() == 0) {
System.out.println("__________________________________________队列空");
try {
getCon.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
T t = linkedList.getFirst();
linkedList.removeFirst();
return t;
} finally {
addCon.signalAll();
lock.unlock();
}
}
public void add(T t) {
lock.lock();
while (linkedList.size() == queueSize) {
System.out.println("-------------------------------------------------------------队列满");
try {
addCon.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
linkedList.add(t);
} finally {
getCon.signalAll();
lock.unlock();
}
}
}
/**
* 消费者
*/
class GetThread implements Runnable {
private ArrayBlokingQueue<String> arrayBlokingQueue;
public GetThread(ArrayBlokingQueue<String> arrayBlokingQueue) {
this.arrayBlokingQueue = arrayBlokingQueue;
}
@Override
public void run() {
while (true) {
System.out.println("消费者" + Thread.currentThread().getName() + ":" + arrayBlokingQueue.get());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 生产者
*/
class AddThread implements Runnable {
private ArrayBlokingQueue<String> arrayBlokingQueue;
public AddThread(ArrayBlokingQueue<String> arrayBlokingQueue) {
this.arrayBlokingQueue = arrayBlokingQueue;
}
@Override
public void run() {
for (int i = 0; ; i++) {
arrayBlokingQueue.add("生产者" + Thread.currentThread().getName() + "的" + i);
System.out.println("生产者" + Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}