class WoTou{
int id;
WoTou(int id ){
this.id=id;
}
public String toString(){
return "id="+id;
}
}
class LanZi{
int idex=0;
WoTou arridex[]=new WoTou[6];public synchronized void push(WoTou wt){
if(idex==arridex.length){
try{
this.wait();
}
catch(Exception e){
e.printStackTrace();
}
}
this.notify();
arridex[idex]=wt;
idex++;
}public synchronized WoTou pop(){
if(idex==0){
try{
this.wait();
}
catch(Exception e){
e.printStackTrace();
}
}
this.notify();
idex--;
return arridex[idex];
}
}
class por implements Runnable{ //生产者
LanZi lz=null;
por(LanZi lz){
this.lz=lz;
}
public void run(){
for(int i=0;i<20;i++){
WoTou wt=new WoTou(i);
lz.push(wt);
}
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class cust implements Runnable{ //消费者
LanZi lz=null;
cust(LanZi lz){
this.lz=lz;
}
public void run(){
for(int i=0;i<20;i++){
WoTou wt=lz.pop();
System.out.println(wt);
}
} public class Demo{
public static void main(String[] args){ LanZi lz =new LanZi();
por p=new por(lz);
cust c=new cust(lz);
Thread th=new Thread(p).start();
Thread th1=new Thread(c).start(); }
}
int id;
WoTou(int id ){
this.id=id;
}
public String toString(){
return "id="+id;
}
}
class LanZi{
int idex=0;
WoTou arridex[]=new WoTou[6];public synchronized void push(WoTou wt){
if(idex==arridex.length){
try{
this.wait();
}
catch(Exception e){
e.printStackTrace();
}
}
this.notify();
arridex[idex]=wt;
idex++;
}public synchronized WoTou pop(){
if(idex==0){
try{
this.wait();
}
catch(Exception e){
e.printStackTrace();
}
}
this.notify();
idex--;
return arridex[idex];
}
}
class por implements Runnable{ //生产者
LanZi lz=null;
por(LanZi lz){
this.lz=lz;
}
public void run(){
for(int i=0;i<20;i++){
WoTou wt=new WoTou(i);
lz.push(wt);
}
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class cust implements Runnable{ //消费者
LanZi lz=null;
cust(LanZi lz){
this.lz=lz;
}
public void run(){
for(int i=0;i<20;i++){
WoTou wt=lz.pop();
System.out.println(wt);
}
} public class Demo{
public static void main(String[] args){ LanZi lz =new LanZi();
por p=new por(lz);
cust c=new cust(lz);
Thread th=new Thread(p).start();
Thread th1=new Thread(c).start(); }
}
}
}
多个花括号
LanZi lz=null;
cust(LanZi lz){
this.lz=lz;
}
public void run(){
for(int i=0;i <20;i++){
WoTou wt=lz.pop();
System.out.println(wt);
}
}
这个类少个“}”
public class Demo{
public static void main(String[] args){
LanZi lz =new LanZi();
por p=new por(lz);
cust c=new cust(lz);
Thread th=new Thread(p).start(); //这里又说:类型不匹配:不能从 void 转换为 Thread
Thread th1=new Thread(c).start(); //这里又说:类型不匹配:不能从 void 转换为 Thread }
}
怎么办啊??又是那里出问题了?????
public class Demo{
public static void main(String[] args){
LanZi lz =new LanZi();
por p=new por(lz);
cust c=new cust(lz);
Thread th=new Thread(p).start(); //这里又说:类型不匹配:不能从 void 转换为 Thread
Thread th1=new Thread(c).start(); //这里又说:类型不匹配:不能从 void 转换为 Thread }
}
怎么办啊??又是那里出问题了?????
Thread th=new Thread(p).start();
Thread th1=new Thread(c).start();改成Thread th = new Thread(p);
Thread th1 = new Thread(c);
th.start();
th1.start();