實現(xiàn)多線程的方式到底有幾種?
1.如何創(chuàng)建多線程
方式一:繼承Thread類
方式二:實現(xiàn)Runnable接口
繼承Thread類
//MyThread繼承Thread
MyThread myThread = new MyThread();
myThread.start();
實現(xiàn)Runnable接口
public class RunnableStyle implements Runnable{
@Override
public void run() {
//執(zhí)行自己的邏輯
}
public static void main(String[] args) {
Thread thread = new Thread(new RunnableStyle());
thread.start();
}
}
本質(zhì)區(qū)別
2.常見面試問題
實現(xiàn)多線程有幾種方式?
實現(xiàn)Runnable接口和繼承Thread類那種方式更好?