生产者,消费者
lock
public class PC02 {
public static void main(String[] args) {
datas da=new datas();
new Thread(()->{
for(int i=1;i<=10;i++){
da.producter();
}
},"p1").start();
new Thread(()->{
for(int i=1;i<=10;i++){
da.consumer();
}
},"c1").start();
}
}
class datas{
private int num=0;
Lock lock=new ReentrantLock();
Condition cond= lock.newCondition();
public void producter() {
lock.lock();
try {
while(num!=0){
cond.await();
}
System.out.println(Thread.currentThread().getName()+"生产产品"+(++num));
cond.signalAll();
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void consumer() {
lock.lock();
try {
while(num==0){
cond.await();
}
System.out.println(Thread.currentThread().getName()+"消费产品"+(num--)+"剩余产品"+num);
cond.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
synchronized
public class PC01 {
public static void main(String[] args) {
data da=new data();
new Thread(()->{
for(int i=1;i<=10;i++){
da.producter();
}
},"p1").start();
new Thread(()->{
for(int i=1;i<=10;i++){
da.consumer();
}
},"c1").start();
}
}
class data{
private int num=10;
public synchronized void producter() {
while(num>0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"生产产品"+(++num));
notifyAll();
}
public synchronized void consumer() {
while(num==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"消费产品"+(num--)+"剩余产品"+num);
notifyAll();
}
}
管程法
public class PCThread {
public static void main(String[] args) {
Buffers bf = new Buffers();
new Thread(new Productor(bf)).start();
new Thread(new Consumer(bf)).start();
}
}
class Productor implements Runnable {
public Buffers bf;
public Productor(Buffers bf) {
this.bf = bf;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
bf.push(new Product(i));
System.out.println("生产第" + i + "个产品");
}
}
}
class Consumer implements Runnable {
public Buffers bf;
public Consumer(Buffers bf) {
this.bf = bf;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println("消费第" + bf.pop().id + "个产品");
}
}
}
class Product {
public int id;
public Product(int id) {
this.id = id;
}
}
class Buffers {
Product[] products = new Product[10];
int count = 0;
public synchronized void push(Product product) {
if (count == products.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
products[count] = product;
count++;
this.notifyAll();
}
public synchronized Product pop() {
if (count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Product pt = products[count];
this.notifyAll();
return pt;
}
}
BlockingQueue 阻塞队列法
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class PC_bq {
public static void main(String[] args) {
BlockingQueue<String> block=new LinkedBlockingQueue<>(2);
Producter p=new Producter(block);
consumer c=new consumer(block);
for(int i=0;i<5;i++){
new Thread(p,"producet"+(i+1)).start();
new Thread(c,"consume"+(i+1)).start();
}
}
}
class Producter implements Runnable{
BlockingQueue<String> bq;
public Producter(BlockingQueue<String> bq) {
this.bq = bq;
}
@Override
public void run() {
try {
String msg="消费一个产品,生产线程是"+Thread.currentThread().getName();
System.out.println( Thread.currentThread().getName()+"生产了一个产品,before put");
bq.put(msg);
System.out.println( Thread.currentThread().getName()+"生产了一个产品,afterd put" );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class consumer implements Runnable{
BlockingQueue<String> bq;
public consumer(BlockingQueue<String> bq) {
this.bq = bq;
}
@Override
public void run() {
try {
String msg= bq.take();
System.out.println(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)