Java学习——多线程例子:李四王五

 

package cys;

public class Example9_2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        People personal1,personal2;
        StringBuffer str=new StringBuffer();
        personal1 = new People("李四",str);
        personal2 = new People("王五",str);
        personal1.start();
        personal2.start();
    }    
}
class People extends Thread{
    StringBuffer str;
    People(String s,StringBuffer str){
        setName(s);
        this.str=str;
    }
    
    public void run(){
            for(int i=1;i<=3;i++){
                str.append(getName()+",");
                System.out.println("我叫"+getName()+",名字是:"+str);
                try{
                    sleep(200);
                }catch(InterruptedException e){
            }
        }
    }
}

 

posted on 2018-12-17 17:43  蔡军帅  阅读(148)  评论(0编辑  收藏  举报