這篇文章主要介紹“java中有哪些創(chuàng)建線程的方法”,在日常操作中,相信很多人在java中有哪些創(chuàng)建線程的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java中有哪些創(chuàng)建線程的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)公司、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了蓮湖免費建站歡迎大家使用!
public class ExtendsThreadTest extends Thread { @Override public void run() { System.out.println("thread is running!"); } public static void main(String[] args) { ExtendsThreadTest et1 = new ExtendsThreadTest(); et1.start(); } }
public class RunnableTest implements Runnable{ @Override public void run() { System.out.println("thread is running!"); } public static void main(String[] args) { Thread t1 = new Thread(new RunnableTest()); t1.start(); } }
public class App { public static void main(String[] args){ new Thread(new Runnable() { @Override public void run() { System.out.println("thread1 is running!"); } }){}.start(); new Thread(){ @Override public void run(){ System.out.println("thread2 is running!"); } }.start(); } }
public class CallableTest { public static void main(String[] args) throws Exception { Callablecall = new Callable () { @Override public Integer call() throws Exception { System.out.println("thread is running!"); return 1; } }; FutureTask task = new FutureTask<>(call); Thread t = new Thread(task); t.start(); } }
public class TimerTest { public static void main(String[] args) throws Exception { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("thread is running!"); } }, new Date()); } }
public class ThreadPoolTest { public static void main(String[] args) { // 創(chuàng)建線程池 ExecutorService threadPool = Executors.newFixedThreadPool(10); while(true) { threadPool.execute(new Runnable() { // 提交多個線程任務(wù),并執(zhí)行 @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running .."); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
到此,關(guān)于“java中有哪些創(chuàng)建線程的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章標題:java中有哪些創(chuàng)建線程的方法
轉(zhuǎn)載來源:http://weahome.cn/article/igsgps.html