线程实例1(如何创建和启动线程)

作者在 2010-05-23 18:43:54 发布以下内容


public class Test1 {
//创建和启动线程
    public static void main(String[] args) {
        //启动线程1
    Thread1 t1=new Thread1();
    t1.start();
    //启动线程2
    Thread2 t2=new Thread2();
    Thread t2i=new Thread(t2);
    t2i.start();
    }

}
//创建一个线程的第一种方式
class Thread1 extends Thread{
    public void run(){
        System.out.println(this.getName());
    }
}
//第二种方式
class Thread2 implements Runnable{

    public void run() {
        System.out.println(Thread.currentThread().getName());
        //得到线程的名称
        
    }
    
}
线程 | 阅读 773 次
文章评论,共0条
游客请输入验证码
浏览275934次