JAVA高级编程(一)

JAVA高级编程——多线程(生产者与消费者)


1、概念

      生产者和消费者指的是两个不同的线程类对象,两个进程公用一个公共的固定大小缓冲区。其中之一的生产者,用于将消息传入缓冲区,另外一个是消费者,用于从缓冲区取    出数据。当缓冲区满了,而此时生产者还想向其中放入新的数据,其解决方法是让生产者此时进行休眠,等待消费者从缓冲区取走数据再去唤醒它,反之。

●  生产者负责生产数据,消费者负责取走数据;

●  生产者每生产完一组数据之后,消费者就要取走一组数据;

2、代码分析

(1)生产者与消费者

class Info {

private String title;
private String content;
private boolean flag = true;
//flag = true:表示可以生产,但不可以取走
//flag = false:表示可以取走,但是不可以生产

public synchronized void set(String title,String content) {   //生产
if(this.flag == false) {
try {
super.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
this.title = title;
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
this.content = content;
this.flag = false;
super.notify();
}
public synchronized void get() { //消费
if(this.flag == true) {
try {
super.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.title + "--" + this.content );
this.flag = true;
super.notify();
}
}
class Productor implements Runnable{    //生产者
private Info info ;
public Productor(Info info) {
this.info = info;
}
@Override
public void run() {
for(int x= 0;x<100;x++) {
if(x%2==0) {
this.info.set("电子", "");
}else {
this.info.set("生产完","请取出");
}
}

}
}
class Customer implements Runnable{  //消费者
private Info info ;
public Customer(Info info) {  
this.info = info;
}
@Override
public void run() {
for(int x=0; x<100; x++) {
this.info.get();
}
}
}
public class test {


public static void main(String[] args) {
Info info = new Info();
new Thread(new Productor(info)).start();
new Thread(new Customer(info)).start();
}

}

(2)生产者与消费者

class Resource{
//可用资源
private int num = 0;
//缓冲区中可存放的资源
private int size = 10;

public synchronized void remove() {
if(num > 0) {
num --;
System.out.println("消费者"+Thread.currentThread().getName()+"正在消费num="+num);
super.notifyAll();
}else {
try {
super.wait();
System.out.println("消费者"+Thread.currentThread().getName()+"进入等待状态");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void add() {
if(num < size) {
num ++;
System.out.println("生产者"+Thread.currentThread().getName()+"正在生产num="+num);
super.notifyAll();s
}else {
try {
super.wait();
System.out.println("生产者"+Thread.currentThread().getName()+"进入等待状态");
} catch (Exception e) {
e.printStackTrace();
}
}
}

}


class Consumer extends Thread{
Resource resource ;
public Consumer(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
while(true) {
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
resource.remove();
}
}
}


class Producer extends Thread{
Resource resource ;
public Producer(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
while(true) {
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
resource.add();
}
}
}
public class JUCTestDemo {
public static void main(String[] args) {
Resource resource = new Resource();
Producer p1 = new Producer(resource);
Producer p2 = new Producer(resource);
Producer p3 = new Producer(resource);
Consumer c1 = new Consumer(resource);
p1.start();
p2.start();
p3.start();
c1.start();
}

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值