Sun提供的:
/**/
/* * Producer.java
*
* Created on 2008年2月24日, 下午2:11
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package
threadPC;
/** */
/** *
*@authorrulinma
*/
public
class
Producer
extends
Thread
...
{
privateCubbyHole cubbyhole;
privateintnumber;
privateProducer()
...{
}
publicProducer(CubbyHole c,intnumber)
...{
cubbyhole=c;
this.number=number;
}
publicvoidrun()
...{
for(inti=0; i<10; i++)
...{
System.out.println("Producer #"+this.number+"put:"+i);
cubbyhole.put(i);
//System.out.println("Producer #" + this.number + " put: " + i);
try
...{
sleep((int)(Math.random()*100));
}catch(InterruptedException e)
...{
} } }}
/**/
/* * Consumer.java
*
* Created on 2008年2月24日, 下午2:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package
threadPC;
/** */
/** *
*@authorrulinma
*/
public
class
Consumer
extends
Thread
...
{
privateCubbyHole cubbyhole;
privateintnumber;
/** *//**Creates a new instance of Consumer*/
publicConsumer()...{
}
publicConsumer(CubbyHole c,intnumber)
...{
cubbyhole=c;
this.number=number;
}
publicvoidrun()
...{
intvalue=0;
for(inti=0; i<10; i++)
...{
value=cubbyhole.get();
System.out.println("Consumer #"+this.number+"got:"+value);
} }
}
/**/
/* * CubbyHole.java
*
* Created on 2008年2月24日, 下午2:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package
threadPC;
/** */
/** *
*@authorrulinma
*/
public
class
CubbyHole
...
{
privateintcontents;
privatebooleanavailable=false;
publicCubbyHole()
...{
}
/**//* * synchronized同步提供单一线程访问
*/publicsynchronizedintget()
...{
//System.out.println("aviaialbe " + available);while(available==false)
...{
try
...{
//System.out.println("get waiting...");wait();
//available = false;}catch(InterruptedException e)
...{
} } notifyAll();
available=false;
//System.out.println("contents:"+contents);returncontents;
}
publicsynchronizedvoidput(intvalue)
...{
//System.out.println("aviaialbe " + available);while(available==true)
...{
try
...{
//System.out.println("put waiting...");wait();
//available = true;}catch(InterruptedException e)
...{
} } contents=value;
notifyAll();
available=true;
//System.out.println("contents:"+contents); }}
/**/
/* * ProducerConsumerTest.java
*
* Created on 2008年2月24日, 下午2:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package
threadPC;
/** */
/** *
*@authorrulinma
*/
public
class
ProducerConsumerTest
...
{
publicProducerConsumerTest()...{
}
publicstaticvoidmain(String[] args)
...{
CubbyHole c=newCubbyHole();
Producer p1=newProducer(c,1);
Consumer c1=newConsumer(c,1);
p1.start();
c1.start();
}
}
运行结果:
init:
deps-jar:
compile-single:
run-single:
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
生成成功(总时间:1 秒)
运行过程如果使用上述代码中的红色代码而不是蓝色的 运行结果会有所不同。