| 
 | 
 
 
 楼主 |
发表于 2016-5-11 13:31:45
|
显示全部楼层
 
 
 
class ProConDemo2 
{ 
        public static void main(String[] args)  
        { 
                product pro=new product(); 
                Pro p=new Pro(pro); 
                Con c=new Con(pro); 
                Thread t1=new Thread(p); 
                Thread t2=new Thread(c); 
                t1.start(); 
                t2.start(); 
        } 
 
} 
class product 
{ 
        String name; 
        int count; 
        public void set(String name) 
        { 
                this.name=name+"..."+count++; 
                try{Thread.sleep(10);}catch(Exception e){} 
                System.out.println(Thread.currentThread().getName()+"..."+this.name); 
        } 
        public void sell() 
        { 
                System.out.println(Thread.currentThread().getName()+"..."+this.name); 
        } 
} 
class Pro implements Runnable 
{ 
        private product pro; 
        Pro(product pro) 
        { 
                this.pro=pro; 
        } 
        public void run() 
        { 
                while(true) 
                        pro.set("changping"); 
        } 
} 
class Con implements Runnable 
{ 
        private product pro; 
        Con(product pro) 
        { 
                this.pro=pro; 
        } 
        public void run() 
        { 
                while(true) 
                        pro.sell(); 
        } 
} |   
 
 
 
 |