package producer_customer;
public class ProducerCustomer1 {
public static int limit = 10;
public static int have = 0;
public static int start = 0;
Task[] tasklist = new Task[limit];
class Producer implements Runnable {
@Override
public void run() {
while (true) {
synchronized (tasklist) {
if (ProducerCustomer1.have < ProducerCustomer1.limit) {
Task temp = new Task(
"mytest"
+ (ProducerCustomer1.start + ProducerCustomer1.have)
% ProducerCustomer1.limit+"\t"+Thread.currentThread().getId());
tasklist[(ProducerCustomer1.start + ProducerCustomer1.have)
% ProducerCustomer1.limit] = temp;
ProducerCustomer1.have++;
tasklist.notifyAll();
} else {
try {
tasklist.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class Customer implements Runnable {
@Override
public void run() {
while (true) {
Task temp = null;
synchronized (tasklist) {
if (ProducerCustomer1.have > 0) {
temp = tasklist[ProducerCustomer1.start];
ProducerCustomer1.start = (ProducerCustomer1.start + 1)
% ProducerCustomer1.limit;
ProducerCustomer1.have--;
System.out.println(temp.taskname + ":"
+ Thread.currentThread().getId());
tasklist.notifyAll();
} else {
try {
tasklist.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class Task {
public String taskname;
public Task(String taskname) {
this.taskname = taskname;
}
}
public static void main(String[] args) {
ProducerCustomer1 p = new ProducerCustomer1();
Producer producer = p.new Producer();
Customer customer = p.new Customer();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(customer).start();
new Thread(customer).start();
new Thread(customer).start();
}
}
[java] view plaincopy
package producer_customer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerCustomer2 {
public static int limit = 10;
public static int have = 0;
public static int start = 0;
Task[] tasklist = new Task[limit];
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
class Producer implements Runnable {
@Override
public void run() {
while (true) {
lock.lock();
if (ProducerCustomer2.have < ProducerCustomer2.limit) {
Task temp = new Task(
"mytest"
+ (ProducerCustomer2.start + ProducerCustomer2.have)
% ProducerCustomer2.limit + "\t"
+ Thread.currentThread().getId());
tasklist[(ProducerCustomer2.start + ProducerCustomer2.have)
% ProducerCustomer2.limit] = temp;
ProducerCustomer2.have++;
empty.signalAll();
} else {
try {
full.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}
}
}
class Customer implements Runnable {
@Override
public void run() {
while (true) {
Task temp = null;
lock.lock();
if (ProducerCustomer2.have > 0) {
temp = tasklist[ProducerCustomer2.start];
ProducerCustomer2.start = (ProducerCustomer2.start + 1)
% ProducerCustomer2.limit;
ProducerCustomer2.have--;
System.out.println(temp.taskname + ":"
+ Thread.currentThread().getId());
full.signalAll();
} else {
try {
empty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}
}
}
class Task {
public String taskname;
public Task(String taskname) {
this.taskname = taskname;
}
}
public static void main(String[] args) {
ProducerCustomer2 p = new ProducerCustomer2();
Producer producer = p.new Producer();
Customer customer = p.new Customer();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(customer).start();
new Thread(customer).start();
new Thread(customer).start();
}
}
[java] view plaincopy
package producer_customer;
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerCustomer3 {
public static int limit = 10;
public static int have = 0;
public static int start = 0;
LinkedBlockingQueue queue = new LinkedBlockingQueue(limit);
class Customer implements Runnable {
@Override
public void run() {
while (true) {
try {
Tasks ss=queue.take();
System.out.println(ss.taskname+":"+Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable {
@Override
public void run() {
while (true) {
Tasks o = new Tasks("test"+Thread.currentThread().getId());
try {
queue.put(o);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Tasks {
public String taskname;
public Tasks(String taskname) {
this.taskname = taskname;
}
}
public static void main(String[] args) {
ProducerCustomer3 p = new ProducerCustomer3();
Producer producer = p.new Producer();
Customer customer = p.new Customer();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(customer).start();
new Thread(customer).start();
new Thread(customer).start();
}
}
[java] view plaincopy
package producer_customer;
import java.util.concurrent.Semaphore;
public class ProducerCustomer4 {
Semaphore mutex = new Semaphore(1);// 互斥量
Semaphore notfull = new Semaphore(10);
Semaphore notempty = new Semaphore(0);
String[] array = new String[10];
int putptr, takeptr, count = 0;
class Producer implements Runnable {
@Override
public void run() {
while (true) {
try {
notfull.acquire();
mutex.acquire();
if (putptr == array.length)
putptr = 0;
String temp = new String("temp" + ":" + putptr + ":"
+ Thread.currentThread().getId());
array[putptr] = temp;
putptr++;
count++;
mutex.release();
notempty.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
while (true) {
try {
notempty.acquire();
mutex.acquire();
if (takeptr == array.length)
takeptr = 0;
System.out.println(array[takeptr] + ":" + takeptr + ":"
+ Thread.currentThread().getId());
takeptr++;
count--;
mutex.release();
notfull.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerCustomer4 s = new ProducerCustomer4();
Producer producer = s.new Producer();
Consumer consumer = s.new Consumer();
new Thread(producer).start();
new Thread(producer).start();
new Thread(producer).start();
new Thread(consumer).start();
new Thread(consumer).start();
}
}