1.繼承Thread類實現多線程
run()為線程類的核心方法,相當於主線程的main方法,是每個線程的入口
a.一個線程調用 兩次start()方法將會拋出線程狀態異常,也就是的start()只可以被調用一次
b.native生明的方法只有方法名,沒有方法體。是本地方法,不是抽象方法,而是調用c語言方法
registerNative()方法包含了所有與線程相關的操作系統方法
c. run()方法是由jvm創建完本地操作系統級線程后回調的方法,不可以手動調用(否則就是普通方法)
public class MyThread extends Thread { public MyThread() { } public void run() { for(int i=0;i<10;i++) { System.out.println(Thread.currentThread()+":"+i); } } public static void main(String[] args) { MyThread mThread1=new MyThread(); MyThread mThread2=new MyThread(); MyThread myThread3=new MyThread(); mThread1.start(); mThread2.start(); myThread3.start(); } }
2.覆寫Runnable()接口實現多線程,而后同樣覆寫run().推薦此方式
a.覆寫Runnable接口實現多線程可以避免單繼承局限
b.當子類實現Runnable接口,此時子類和Thread的代理模式(子類負責真是業務的操作,thread負責資源調度與線程創建輔助真實業務。
public class MyThread implements Runnable{ public static int count=20; public void run() { while(count>0) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"-當前剩余票數:"+count--); } } public static void main(String[] args) { MyThread Thread1=new MyThread(); Thread mThread1=new Thread(Thread1,"線程1"); Thread mThread2=new Thread(Thread1,"線程2"); Thread mThread3=new Thread(Thread1,"線程3"); mThread1.start(); mThread2.start(); myThread3.start(); } }
繼承Thread和實現Runnable接口的區別
a.實現Runnable接口避免多繼承局限
b.實現Runnable()可以更好的體現共享的概念
3.覆寫Callable接口實現多線程(JDK1.5)
a.核心方法叫call()方法,有返回值
b.有返回值
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class MyThread implements Callable<String> { private int count = 20; @Override public String call() throws Exception { for (int i = count; i > 0; i--) { // Thread.yield(); System.out.println(Thread.currentThread().getName()+"當前票數:" + i); } return "sale out"; } public static void main(String[] args) throws InterruptedException, ExecutionException { Callable<String> callable =new MyThread(); FutureTask <String>futureTask=new FutureTask<>(callable); Thread mThread=new Thread(futureTask); Thread mThread2=new Thread(futureTask); Thread mThread3=new Thread(futureTask); // mThread.setName("hhh"); mThread.start(); mThread2.start(); mThread3.start(); System.out.println(futureTask.get()); } }
4.通過線程池啟動多線程
通過Executor 的工具類可以創建三種類型的普通線程池:
FixThreadPool(int n); 固定大小的線程池
使用於為了滿足資源管理需求而需要限制當前線程數量的場合。使用於負載比較重的服務器。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) { ExecutorService ex=Executors.newFixedThreadPool(5); for(int i=0;i<5;i++) { ex.submit(new Runnable() { @Override public void run() { for(int j=0;j<10;j++) { System.out.println(Thread.currentThread().getName()+j); } } }); } ex.shutdown(); } }
SingleThreadPoolExecutor :單線程池
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) { ExecutorService ex=Executors.newSingleThreadExecutor(); for(int i=0;i<5;i++) { ex.submit(new Runnable() { @Override public void run() { for(int j=0;j<10;j++) { System.out.println(Thread.currentThread().getName()+j); } } }); } ex.shutdown(); } }
CashedThreadPool(); 緩存線程池
當提交任務速度高於線程池中任務處理速度時,緩存線程池會不斷的創建線程
適用於提交短期的異步小程序,以及負載較輕的服務器
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) { ExecutorService ex=Executors.newCachedThreadPool(); for(int i=0;i<5;i++) { ex.submit(new Runnable() { @Override public void run() { for(int j=0;j<10;j++) { System.out.println(Thread.currentThread().getName()+j); } } }); } ex.shutdown(); } }
線程之間的狀態,及他們之間的相互轉換
————————————————
版權聲明:本文為CSDN博主「上校的小金魚_」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_41891854/article/details/81265772