執行緒建立的兩種方式

2021-08-15 19:23:28 字數 584 閱讀 6395

建立執行緒的兩種方式:

1.繼承thread並重寫方法,在run方法中定義執行緒要執行的任務

class mythread extends thread 

}}

public class threaddemo1 

}

2.實現runable介面並重寫run方法

class myrunnable implements runnable

}}

public class threaddemo 	

}

3.兩種方式的匿名內部類

public class threaddemo3 

} }; t1.start();

//使用實現runnable介面方式

runnable r2= new runnable()

} }; thread t2=new thread(r2);

t2.start();

} }

建立執行緒的兩種方式

thread thread new thread catch interruptedexception e system.out.println thread.currentthread getname system.out.println this.getname thread.start thr...

建立執行緒的兩種方式

一 繼承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...