线程操作实例

来源:互联网 发布:淘宝刷手的风险 编辑:程序博客网 时间:2024/05/24 16:17

<李兴华视频笔记>

//线程A休眠10秒
//线程B休眠20秒
//线程C休眠30秒
class MyThread implements Runnable
{
    private String name ;
    private int time ;
    public MyThread(String name,int time){
        this.name = name ;
        this.time = time ;
    }
    public void run(){
        try{
            Thread.sleep(time) ;
        }catch(Exception e){
            e.printStackTrace() ;
        }
        System.out.println(this.name+"线程,休眠"+this.time+"毫秒") ;
    }
}
public class ExecDemo02
{
    public static void main(String args[]){
        MyThread mt1 = new MyThread("线程A",10000) ;
        MyThread mt2 = new MyThread("线程B",20000) ;
        MyThread mt3 = new MyThread("线程C",30000) ;
        new Thread(mt1).start();
        new Thread(mt2).start();
        new Thread(mt3).start();
    }
}

//线程A休眠10秒
//线程B休眠20秒
//线程C休眠30秒
class MyThread extends Thread
{
    private int time ;
    public MyThread(String name,int time){
        super(name) ;
        this.time = time ;
    }
    public void run(){
        try{
            Thread.sleep(time) ;
        }catch(Exception e){
            e.printStackTrace() ;
        }
        System.out.println(Thread.currentThread().getName()+"线程,休眠"+this.time+"毫秒") ;
    }
}
public class ExecDemo01
{
    public static void main(String args[]){
        MyThread mt1 = new MyThread("线程A",10000) ;
        MyThread mt2 = new MyThread("线程B",20000) ;
        MyThread mt3 = new MyThread("线程C",30000) ;
        mt1.start();
        mt2.start();
        mt3.start();
    }
}