建立執行緒的兩種方式

2021-09-24 02:13:36 字數 831 閱讀 3559

thread thread=new thread() catch(interruptedexception e) 

system.out.println(thread.currentthread().getname());

system.out.println(this.getname());}}

};thread.start();

thread thread2=new thread(new runnable() catch(interruptedexception e) 

system.out.println(thread.currentthread().getname());

//不能用這種方法

}} }

);

thread thread2=new thread(new runnable() catch(interruptedexception e) 

system.out.println(thread.currentthread().getname());

//不能用這種方法

}} }

) catch(interruptedexception e)

system.out.println(thread.currentthread().getname());

system.out.println(this.getname());

}} };

會執行被覆蓋的run方法,而不會執行runnable方法,因為 子類將父類的run方法覆蓋了,所以run方法不會再去找runnable裡面的run方法

執行緒建立的兩種方式

建立執行緒的兩種方式 1.繼承thread並重寫方法,在run方法中定義執行緒要執行的任務 class mythread extends thread public class threaddemo1 2.實現runable介面並重寫run方法 class myrunnable implements...

建立執行緒的兩種方式

一 繼承thread類 public class mythead extends thead override public void run public class demo01 二 實現runnable介面 public class myrunnable implements runnable...

建立執行緒的兩種方式

將執行緒任務實現runnable介面 class a implements runnable run system.out.println 執行緒任務 將實現runnable介面的類的物件作為引數傳遞給執行緒物件 class b public static void main string args...