process 進程
thread 線程
1.程序、進程、線程
程序是指令和數據的有序集合,它沒有任何運行的含義,是靜態的
進程是程序的一次執行,是動態的,是系統資源分配的單位
進程里面包含多個線程,一個進程里面包含至少一個線程,線程是CPU調用和執行的基本單位。
真正的多線程是有多個CPU同時執行多個線程喲(如服務器),現實中有很多模擬出來的多線程,只有一個CPU,在同那個一個時間點,CPU只能執行一個代碼,
但由於CPU在多個線程之間切換的太快,就產生了同時執行的錯覺。
2.線程的創建方式(有三種)
2.1第一種,繼承Thread類
1.創建一個線程類,繼承Thread類
2.重寫run()方法
3.創建線程類對象,對象調用start()方法開啟線程
注意:線程開啟不一定立即執行,由CPU調度執行。
不建議使用,避免OOP單繼承的局限性
/* * 1.繼承Thread類 * 2.重寫run()方法 * 3.創建對象,調用start()方法,開啟線程*/ public class TestThread extends Thread{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("我自己開創的線程"+i); } } public static void main(String[] args) { TestThread testThread = new TestThread(); testThread.start(); for (int i = 0; i < 1000; i++) { System.out.println("main進程"+i); } } }
2.2第二種,實現Runable接口
1.創建一個線程類,實現Runnable接口
2.實現run()方法
3.創建線程類對象,new Thread(對象).start()來啟動線程(代理)(自己創建的線程無start()方法)
推薦使用:避免單繼承的局限性,靈活方便,方便同一個對象被多個線程所使用。
public class TestThread03 implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("自創線程"+i); } } public static void main(String[] args) { TestThread03 testThread03 = new TestThread03(); new Thread(testThread03).start();//這里與第一種方法不同,用到了靜態代理模式 for (int i = 0; i < 500; i++) { System.out.println("main線程"+i); } } }
當多個線程在搶占同一個資源時,如果不加以處理,會出現多個線程同時使用同一個資源的情況,發生資源數據紊亂了。--線程並發問題
2.3創建線程的第三種方式:利用callable接口
* 1.繼承callable接口
* 2.重寫call()方法,含返回值類型和拋出異常
* 3.創建類對象
* 4.創建執行任務 ExecutorService service = Executors.newFixedThreadPool(3);
* 5。提交執行 Future<Boolean> f1 = service.submit(t1);
* 6.獲取結果 boolean rs1 = f1.get();
* 7.關閉服務 service.shutdownNow();
/* * 創建線程的第三種方式:利用callable接口 * 1.繼承callable接口 * 2.重寫call()方法,含返回值類型和拋出異常 * 3.創建類對象 * 4.創建執行任務 ExecutorService service = Executors.newFixedThreadPool(3); * 5。提交執行 Future<Boolean> f1 = service.submit(t1); * 6.獲取結果 boolean rs1 = f1.get(); * 7.關閉服務 service.shutdownNow(); * */ import java.util.concurrent.*; public class TestCallable implements Callable { @Override public Boolean call() throws Exception { System.out.println("自創線程開始運行。。。。。"); return true; } public static void main(String[] args) { TestCallable t1 = new TestCallable(); TestCallable t2 = new TestCallable(); TestCallable t3 = new TestCallable(); //創建執行服務 ExecutorService service = Executors.newFixedThreadPool(3); //提交執行 Future<Boolean> f1 = service.submit(t1); Future<Boolean> f2 = service.submit(t2); Future<Boolean> f3 = service.submit(t3); //獲取結果 try { boolean rs1 = f1.get(); boolean rs2 = f2.get(); boolean rs3 = f3.get(); System.out.println(rs1+" "+rs2+" "+rs3); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } // 關閉服務 service.shutdownNow(); } }
也可以這樣啟動線程:
package thread; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class TestCallable3 implements Callable<Integer> { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<Integer> future = new FutureTask<Integer>(new TestCallable3()); new Thread(future).start(); Integer integer = future.get(); System.out.println(integer); } @Override public Integer call() throws Exception { System.out.println("線程開始運行。。。。。。。"); return 100; } }