- class resouse
- {
- private String name;
- private String sex;
- private boolean flag=false;
- public synchronized void set(String name,String sex)
- {
- while(flag)
- try{this.wait();}
- catch(Exception e)
- {
-
- }
- this.name=name;
- this.sex=sex;
- System.out.println(name+"%……………………………………"+sex);
- flag=true;
- this.notifyAll();
-
- }
- public synchronized void prit()
- {
- while(!flag)
- try{this.wait();}
- catch(Exception e){}
-
- System.out.println(name+"....."+sex);
- flag=false;
- this.notifyAll();
-
- }
-
- }
- class Shengchang implements Runnable
- {
- private resouse r;
- Shengchang(resouse r)
- {
- this.r=r;
- }
- public void run()
- {
-
- while(true)
- {
-
- r.set("李四","男");
-
- }
- }
-
- }
- class outprint implements Runnable
- {
- private resouse r;
- outprint(resouse r)
- {
- this.r=r;
- }
- public void run()
- {
- r.prit();
- }
- }
-
- public class Dengdai {
- public static void main(String[] args)
- {
- resouse r=new resouse();
-
- Shengchang in=new Shengchang(r);
- outprint ou=new outprint(r);
- Thread t=new Thread(in);
- Thread t1=new Thread(in);
- Thread t2=new Thread(ou);
- Thread t3=new Thread(ou);
- t1.start();
- t.start();
-
- t2.start();
- t3.start();
- }
- }
复制代码 |