Java執行緒建立的兩種方式
阿新 • • 發佈:2018-11-27
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()+"正在執行");
}
}
/**
* 建立執行緒
*
*/
public class Demo1 {
public static void main(String arg[]){
MyThread t=new MyThread();
t.start();
myRunnable r=new myRunnable();
Thread run=new Thread(r);
}
}
/**
*
* 繼承Thread類
*
*/
class MyThread extends Thread{
@Override
public void run() {
System.out.println("Thread執行緒:"+Thread.currentThread().getId()+"正在執行");
}
}
/**
* 實現Runnable介面
*
*/
class myRunnable implements Runnable{
public void run() {
System.out.println("Runnable執行緒"+Thread.currentThread().getId()+"正在執行");
}
}