Java线程创建的两种方式

package test;




/**
 * 创建线程
 *
 */
public class Demo1 {
     public static void main(String arg[]){
    MyThread t=new MyThread();
    t.start();
    myRunnable r=new myRunnable();
    Thread run=new Thread(r);
    run.start();
     }
}
/**
 * 
 * 继承Thread类
 *
 */
class MyThread extends Thread{
@Override
public void run() {
System.out.println("Thread线程:"+Thread.currentThread().getId()+"正在运行");
}
}
/**
 * 实现Runnable接口
 *
 */
class myRunnable implements Runnable{


@Override
public void run() {
System.out.println("Runnable线程"+Thread.currentThread().getId()+"正在运行");
}

}

猜你喜欢

转载自blog.csdn.net/xiaosong_2016/article/details/77678892