62,628
社区成员
发帖
与我相关
我的任务
分享public class Zst{
public static void main(String[] args){
Sc a=new Sc();
XiaoShou aa=new XiaoShou(a);
ShenChan bb=new ShenChan(a);
Thread a1=new Thread(aa);
a1.start();
Thread b1=new Thread(bb);
b1.start();
}
}public class XiaoShou implements Runnable{
private Sc a=null;
public XiaoShou(Sc a){
this.a=a;
}
public void run(){
for(int i=0;i<26;i++){
a.ChuShou();
}
}
}public class ShenChan implements Runnable{
private Sc a=null;
public ShenChan(Sc a){
this.a=a;
}
public void run(){
char aa;
for(int i=0;i<26;i++){
aa=(char)('A'+i);
a.ShengChan(aa);
}
}
}public class Sc{
private char[] rl=new char[8];
private int wp=0;
public synchronized void ShengChan(char aa){
while(wp==rl.length){
try{
this.wait();
}
catch(Exception e){
}
}
this.notify();
rl[wp]=aa; ++wp;
System.out.println("生产了第"+wp+"个物品\t物品名是"+aa);
}
public synchronized void ChuShou(){
char aa;
while(wp==0){
try{
this.wait();
}
catch(Exception e){}
}
this.notify();
aa=rl[wp-1];
System.out.println("出售了第"+wp+"个物品\t物品名是"+aa);
--wp;
}
}