2013年9月20日星期五

Java multi -threaded typical producer and consumer issues, some do not understand , please enlighten.


written buns production and consumption issues , a four entity classes : producers, consumers , buns , baskets , baskets can hold six buns , but the results are not expected in the so , there will be produced after spending seven buns situation , as well as basic first consumer spending will start from the first one , as can be expected, is like a LIFO stack , do not know where the problem is , please enlighten ~ ~ ~
Also, the production and consumption of two classes add delay , that is part of the text commented out , the result looks similar to some of the Council and the expected .
practice writing code , not to develop the habit of writing notes , sorry ~ ~ ~ ~


public class Produce
{
public static void main(String[] args) 
{
Barsket b=new Barsket();
Pro p=new Pro(b);
Cus c=new Cus(b);
new Thread(p).start();
new Thread(c).start();

}
}
//生产者
class Pro implements Runnable
{
Barsket b;
Pro(Barsket b)
{
this.b=b;
}
public void run()
{
for (int i=1;i<=20 ; i++)
{
/*try
{
Thread.sleep((int)(Math.random()*200));
}
catch (InterruptedException e)
{
e.printStackTrace();
}*/

Bao bao=new Bao(i);
b.produce(bao);
System.out.println("生产了 "+bao.toString());
}
}
}
//消费者
class Cus implements Runnable
{
Barsket b;
Cus(Barsket b)
{
this.b=b;
}
public void run()
{
for (int i=1;i<=20 ; i++)
{
/*try
{
Thread.sleep((int)(Math.random()*200));
}
catch (InterruptedException e)
{
e.printStackTrace();
}*/

Bao bao=b.consume();
System.out.println("消费了 "+bao.toString());
}
}
}
//篮子,可以放6个包子
class Barsket 
{
int index =0;
Bao bao[]=new Bao[6];
static int num=0;
public synchronized void produce(Bao baozi)
{
while(index==6)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notifyAll();
bao[index]=baozi;
index++;
//return bao[index];
}
public synchronized Bao consume()
{
while(index==0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.notifyAll();
index--;
return bao[index];
}
}
//包子类
class Bao 
{
int i;
Bao(int i)
{
this.i=i;
}
public String toString()
{
return "包子:"+i;
}
}


The result:




example of the result is this ( for example not the same, with the buns, same meaning )


------ Solution ------------------------------------ --------
output order because multithreaded causes.
------ Solution ---------------------------------------- ----
why not put public synchronized void produce (Bao baozi) inside the logic in this method Pro class run () method inside public synchronized Bao consume ( ) on Cus class inside ? ?
------ Solution ---------------------------------------- ----
production 7 buns is an illusion , is the output problems
------ Solution ---------------------- ----------------------
I used to write a
/**
 * 生产者-消费者问题
 * 
 * @author 
 * 
 */
public class Test {

public static void main(String[] args) {

Stack s = new Stack();
Producer p = new Producer(s);
Consumer c = new Consumer(s);

Thread t1 = new Thread(p);
Thread t2 = new Thread(c);

t1.start();
t2.start();

}

}

class Stack {
int[] elements;
int size;

public Stack() {
elements = new int[5];
size = 0;
}

public synchronized void push(int data) {
while (size < 0 || size >= elements.length) {
try {
System.out.println("producer wait...");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
elements[size] = data;
size++;
System.out.println("push[" + (size - 1) + "]:" + data);
this.notifyAll();
}

public synchronized int pop() {
while (size <= 0) {//为什么用while,假如stack为空,有两个消费者线程同时等待在这里,然后生产一个东西,两个生产者会先后去取,数组越界
try {
System.out.println("consumer wait...");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
size--;
int value = elements[size];
System.out.println("pop[" + size + "]:" + value);
this.notifyAll();
return value;
}
}

class Consumer implements Runnable {
Stack s;

public Consumer(Stack s) {
this.s = s;
}

@Override
public void run() {
for (int i = 0; i < 20; i++)
s.pop();
}
}

class Producer implements Runnable {
Stack s;

public Producer(Stack s) {
this.s = s;
}

@Override
public void run() {
for (int i = 0; i < 20; i++)
s.push(i);
}
}

------ For reference only ----------------------------------- ----

What are the benefits you can talk about ?
I am a novice , just follow the examples go ,
------ For reference only -------------------------- -------------

like Hao ~ ~
put the output into BasketBall produce () method and the Consumer () method which is expected to reach the ~ ~ ~

------ For reference only ---------------------------------- -----

Thank you ! ~ ~

没有评论:

发表评论