初步學習多線程2
阿新 • • 發佈:2017-12-23
優點 加油 缺點 pos 學習 機會 super 加油!!!! 加油!
線程代碼展示:
兔子線程實現Runnable接口:
package thread; /** * 兔子的線程 * @author superdrew */ public class RabbitThread implements Runnable{ public void run() { while(true){ System.out.println("兔子領先了...加油!!!!!"+Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()); } } }
測試線程:
package thread; /** * 功能:龜兔賽跑 實現方法二 * 使用線程 * 思路:分別創建兩個線程 一個是烏龜 另外一個是兔子 ,完成賽跑任務 * 總結: * 1.如何定義線程 * 實現Runnable接口,實現run方法 * 2.如何創建線程對象 * RabbitThread rt = new RabbitThread(); * Thread th = new Thread(rt); * 3.如何啟動線程 * th.strat(); * * 兩種方式的優缺點 * 1.繼承Thread * 優點:代碼簡單些 * 缺點:不能繼承其他類 * 2.實現了Runnable * 優點:能夠繼承其他類 * 缺點:代碼復雜點 *@author superdrew */ public class TestThread1 { public static void main(String[] args) { RabbitThread rt = new RabbitThread(); Thread th = new Thread(rt); th.start(); while(true){//烏龜有機會嗎? System.out.println("烏龜領先了.....加油!!!!!"+ Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()); } } }
初步學習多線程2