1.實現Runnable介面,重載run(),無返回值 2.繼承Thread類,覆寫run() 使用時通過調用Thread的start()(該方法是native),再調用創建線程的run(),不同線程的run方法裡面的代碼交替執行。 不足:由於java為單繼承,若使用線程類已經有個父類,則不能使用該 ...
1.實現Runnable介面,重載run(),無返回值
package thread; public class ThreadRunnable implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } package thread; public class ThreadMain { public static void main(String[] args) throws Exception { ThreadRunnable threadRunnable1 = new ThreadRunnable(); ThreadRunnable threadRunnable2 = new ThreadRunnable(); ThreadRunnable threadRunnable3 = new ThreadRunnable(); ThreadRunnable threadRunnable4 = new ThreadRunnable(); Thread thread1 = new Thread(threadRunnable1); Thread thread2 = new Thread(threadRunnable2); Thread thread3 = new Thread(threadRunnable3); Thread thread4 = new Thread(threadRunnable4); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }
2.繼承Thread類,覆寫run()
使用時通過調用Thread的start()(該方法是native),再調用創建線程的run(),不同線程的run方法裡面的代碼交替執行。
不足:由於java為單繼承,若使用線程類已經有個父類,則不能使用該方式創建線程。
public class ThreadEx extends Thread { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread() + ":" + i); } } } public class ThreadMain { public static void main(String[] args) { ThreadEx threadEx = new ThreadEx(); threadEx.start(); } }
3.實現Callable介面,通過FutureTask/Future來創建有返回值的Thread線程,通過Executor執行
補充:與實現Runnable介面類似,都是實現介面,不同的是該方式有返回值,可以獲得非同步執行的結果。
延伸:FutureTask是類,Future是介面。
package thread; import java.util.concurrent.*; public class ThreadCallable { public static void main(String[] args) throws Exception { FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() { public Integer call() throws Exception { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } return 1; } }); Executor executor = Executors.newFixedThreadPool(1); ((ExecutorService) executor).submit(futureTask); //獲得線程執行狀態 System.out.println(Thread.currentThread().getName() + ":" + futureTask.get()); } }
4.使用Executors創建ExecutorService,入參Callable或Future
補充:適用於線程池和併發
package thread; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import static java.lang.Thread.sleep; public class ThreadExecutors { private final String threadName; public ThreadExecutors(String threadName) { this.threadName = threadName; } private ThreadFactory createThread() { ThreadFactory tf = new ThreadFactory() { public Thread newThread(Runnable r) { Thread thread = new Thread(); thread.setName(threadName); thread.setDaemon(true); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return thread; } }; return tf; } public Object runCallable(Callable callable) { return Executors.newSingleThreadExecutor(createThread()).submit(callable); } public Object runFunture(Runnable runnable) { return Executors.newSingleThreadExecutor(createThread()).submit(runnable); } } package thread; import java.util.concurrent.*; public class ThreadMain { public static void main(String[] args) throws Exception { ThreadExecutors threadExecutors = new ThreadExecutors("callableThread"); threadExecutors.runCallable(new Callable() { public String call() throws Exception { return "success"; } }); threadExecutors.runFunture(new Runnable() { public void run() { System.out.println("execute runnable thread."); } }); } }
5 Runnable介面和Callable介面區別
1)兩個介面需要實現的方法名不一樣,Runnable需要實現的方法為run(),Callable需要實現的方法為call()。
2)實現的方法返回值不一樣,Runnable任務執行後無返回值,Callable任務執行後可以得到非同步計算的結果。
3)拋出異常不一樣,Runnable不可以拋出異常,Callable可以拋出異常。