黑马程序员技术交流社区
标题:
Java多线程,多生产者多消费者模式
[打印本页]
作者:
大漠孤烟
时间:
2014-5-1 23:11
标题:
Java多线程,多生产者多消费者模式
package com.itheima.thread.demo;
public class ProductDemo {
/**
* @param args
*/
public static void main(String[] args) {
Resource res=new Resource();
Producter pd=new Producter(res);
Consumer cos=new Consumer(res);
Thread t1=new Thread(pd);
Thread t2=new Thread(pd);
Thread t3=new Thread(cos);
Thread t4=new Thread(cos);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count=1;
private static boolean flag=false;
//生产
public synchronized void set(String name){
while(flag){
try {
this.wait();
} catch (Exception e) {
// TODO: handle exception
}
this.name=name+"..."+count++; //烤鸭1、烤鸭2、烤鸭3
System.out.print(Thread.currentThread().getName()+"......生产者.."+this.name);
flag=true;
notifyAll(); //唤醒所有等待的线程
}
}
//消费
public synchronized void Out (){
if(!flag)
try {
wait();
} catch (Exception e) {
// TODO: handle exception
}
System.out.print(Thread.currentThread().getName()+"......消费者..."+this.name);
flag=false;
notifyAll();
}
}
class Producter implements Runnable{
private Resource res;
Producter(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.set("北京烤鸭");
}
}
}
class Consumer implements Runnable{
private Resource res;
Consumer(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.Out();
}
}
}
各位大侠麻烦帮忙看一下,我看着视频敲的,也不知道哪里出了问题,多线程执行控制台没有反应,也不报错??谢谢!
作者:
Up↑Lee↗
时间:
2014-5-2 08:54
public class ProductDemo {
/**
* @param args
*/
public static void main(String[] args) {
Resource res=new Resource();
Producter pd=new Producter(res);
Consumer cos=new Consumer(res);
Thread t1=new Thread(pd);
Thread t2=new Thread(pd);
Thread t3=new Thread(cos);
Thread t4=new Thread(cos);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count=1;
private static boolean flag=false;
//生产
public synchronized void set(String name){
while(flag) //-<span style="background-color: yellow;">----------------------你把while的{}去掉就可以了 加上{}没有再次判断条件</span>
try {
this.wait();
} catch (Exception e) {
// TODO: handle exception
}
this.name=name+"..."+count++; //烤鸭1、烤鸭2、烤鸭3
System.out.println(Thread.currentThread().getName()+"......生产者.."+this.name);/<span style="background-color: yellow;">/------记得换行</span>
flag=true;
notifyAll(); //唤醒所有等待的线程
}
//消费
public synchronized void Out (){
if(!flag)
try {
wait();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(Thread.currentThread().getName()+"......消费者..."+this.name);
flag=false;
notifyAll();
}
}
class Producter implements Runnable{
private Resource res;
Producter(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.set("北京烤鸭");
}
}
}
class Consumer implements Runnable{
private Resource res;
Consumer(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.Out();
}
}
}
复制代码
改正在源代码,呵呵 记得要细心啊
作者:
Lin0411
时间:
2014-5-2 10:01
修改后的正确代码如下:
public class test {
/**
* @param args
*/
public static void main(String[] args) {
Resource res=new Resource();
Producter pd=new Producter(res);
Consumer cos=new Consumer(res);
Thread t1=new Thread(pd);
Thread t2=new Thread(pd);
Thread t3=new Thread(cos);
Thread t4=new Thread(cos);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count=1;
private static boolean flag=false;
//生产
public synchronized void set(String name){
while(flag){
try {
this.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
this.name=name+"..."+count++; //烤鸭1、烤鸭2、烤鸭3
System.out.println(Thread.currentThread().getName()+"......生产者.."+this.name);
flag=true;
notifyAll(); //唤醒所有等待的线程
}
//消费
public synchronized void Out (){
while(!flag)
{try {
wait();
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println(Thread.currentThread().getName()+"......消费者..."+this.name);
flag=false;
notifyAll();
}
}
class Producter implements Runnable{
private Resource res;
Producter(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.set("北京烤鸭");
}
}
}
class Consumer implements Runnable{
private Resource res;
Consumer(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.Out();
}
}
}
if语句都应该改成while语句来判断!!!!,另外你的while语句括号的范围错了。
复制代码
作者:
大漠孤烟
时间:
2014-5-2 23:47
多谢楼主啦,:handshake原来这里的while循环不用带{ }括号,编程习惯都加了,去掉果然可以打印,不过后面的if()判断也可以打印出来,只不过数据乱的,改成while确实正常啦。。。。。
作者:
邵景伦
时间:
2014-5-3 16:20
修改后的正确代码如下:
public class test {
/**
* @param args
*/
public static void main(String[] args) {
Resource res=new Resource();
Producter pd=new Producter(res);
Consumer cos=new Consumer(res);
Thread t1=new Thread(pd);
Thread t2=new Thread(pd);
Thread t3=new Thread(cos);
Thread t4=new Thread(cos);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count=1;
private static boolean flag=false;
//生产
public synchronized void set(String name){
while(flag){//范围错误
try {
this.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
this.name=name+"..."+count++; //烤鸭1、烤鸭2、烤鸭3
System.out.println(Thread.currentThread().getName()+"......生产者.."+this.name);
flag=true;
notifyAll(); //唤醒所有等待的线程
}
//消费
public synchronized void Out (){
while(!flag)
{try {
wait();
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println(Thread.currentThread().getName()+"......消费者..."+this.name);
flag=false;
notifyAll();
}
}
class Producter implements Runnable{
private Resource res;
Producter(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){//修改后if换成while
res.set("北京烤鸭");
}
}
}
class Consumer implements Runnable{
private Resource res;
Consumer(Resource res){
this.res=res;
}
@Override
public void run() {
while(true){
res.Out();
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2