下面的程序有点小小问题,先放出问题 求解,下面附上代码
[quote]
期望运行结果:
....//省略了
46
生产者gen03馒头 编号:3
47
生产者gen02馒头 编号:4
48
生产者gen01馒头 编号:5
49
--消费者cus01馒头 编号:5
50
生产者gen03馒头 编号:5
51
生产者gen01馒头 编号:6
生产者gen02馒头 编号:7
53
53
生产者gen03馒头 编号:8
54
生产者gen01馒头 编号:9
55
生产者gen02馒头 编号:10
56
问题点:(注掉了//--------------包围的代码产生的)
....//省略了
48
--消费者cus01馒头 编号:12
49
--消费者cus02馒头 编号:11
50
--消费者cus01馒头 编号:10
51
生产者gen03馒头 编号:10
52
生产者gen02馒头 编号:11
53
生产者gen01馒头 编号:12
54
生产者gen02馒头 编号:13
55
生产者gen01馒头 编号:14
56
生产者gen03馒头 编号:15
57
生产者gen02馒头 编号:16
58
生产者gen03馒头 编号:17
59
生产者gen01馒头 编号:18
60
生产者gen03馒头 编号:19
61
生产者gen02馒头 编号:20
生产者gen01馒头 编号:21
63
63
[/quote]
上代码
[code="java"]
package com.ibm.TestSynchronized;
import java.util.ArrayList;
public class Generant_Customer {
public static void main(String[] args) {
Collection c = new Collection();
Generant gen = new Generant(c);
Customer cus = new Customer(c);
new Thread(gen,"gen01").start();
new Thread(gen,"gen02").start();
new Thread(gen,"gen03").start();
new Thread(cus,"cus01").start();
new Thread(cus,"cus02").start();
}
}
/**
-
资源类 , 消费者和生产者共同操作的资源
*/
class Collection {
ArrayList collection = new ArrayList();
static final int length = 10;
int index = 1 ;
int operateCount = 0;/* 生产者生产 */
public synchronized void add(){
if(collection.size()>=length){
try {
wait();
} catch (InterruptedException e) {
System.out.println("add() has been interrupted !");
System.exit(1);
}
}
notifyAll();//唤醒在此对象监视器上等待的单个线程
collection.add("馒头 编号:"+index);
System.out.println("生产者" +Thread.currentThread().getName() + collection.get(index-1));
index ++ ;
operateCount++;
}/* 消费者消费 */
public synchronized void pop(){
if(collection.size() == 0){
try {
wait();
} catch (InterruptedException e) {
System.out.println("pop() has been interrupted !");
System.exit(1);
}
}
notifyAll(); //唤醒在此对象监视器上等待的单个线程
System.out.println("--消费者" +Thread.currentThread().getName() + collection.get(collection.size()-1));
collection.remove(collection.size()-1);
index -- ;
operateCount++;
}
}
class Generant implements Runnable{
Collection coll = null ;
public Generant(Collection collection ){
this.coll = collection ;
}
/* 生产者开始执行 */
public void run() {
while(true){
coll.add();
System.out.println(coll.operateCount);
//------------------------------------------------------------------
//注掉后运行就有问题
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("Generant.run() has been interrupted !\\n" +
" System continue...");
}
//------------------------------------------------------------------
}
}
}
class Customer implements Runnable{
Collection coll = null ;
/* 线程开关 */
boolean doRun = true ;
public Customer(Collection collection ){
this.coll = collection ;
}
/* 消费者开始执行 */
public void run() {
while(doRun){
coll.pop();
System.out.println(coll.operateCount);
//------------------------------------------------------------------
//注掉后运行就有问题
try {
Thread.sleep(150);
} catch (InterruptedException e) {
System.out.println("Customer.run() has been interrupted !\\n" +
" System continue...");
}
//------------------------------------------------------------------
if(coll.operateCount == 50){
doRun = false;
}
}
}
}
[/code]