假設一個項目擁有三塊獨立代碼塊,須要執行,何時用多線程?java
這些代碼塊某些時候須要同時運行,彼此獨立,那麼須要用到多線程操做更快。。。多線程
這裏把模板放在這裏,須要用的時候尋找合適的來選用。ide
整體分爲兩種:函數
1、使用匿名內部類的方式(推薦)學習
1)方式1開啓多線程(Thread)測試
//使用第一種方式,開啓線程 new Thread() { public void run() { for(int i=0;i<100;i++) { System.out.println(Thread.currentThread().getName()+"線程方式1"+ i); } } }.start();
2)方式2內部開啓線程(Runnable)spa
//使用第二種方式,開啓線程 Runnable r =new Runnable() { public void run() { for(int i=0;i<100;i++) { System.out.println(Thread.currentThread().getName()+".....方式2++"+ i); } } }; //不可或缺 new Thread(r).start();
2、經過在外部定義類,類的實例化開啓線程線程
3)使用第三種方式,類實例化開啓線程(繼承方式)code
類定義:blog
class ThreadTest extends Thread { public void run() { for(int i=0;i<100;i++) { System.out.println(Thread.currentThread().getName()+".....類的方式(extends)----"+ i); } } }
主函數中調用:
//使用第三種方式,類實例化開啓線程(繼承方式) new ThreadTest().start();
4)使用第四種方式,類實例化開啓線程(實現方式)
類定義:
class RunnableTest implements Runnable { public void run() { for(int i=0;i<100;i++) { System.out.println(Thread.currentThread().getName()+".........類的方式(implements)----++"+ i); } } }
主函數調用:
//使用第四種方式,類實例化開啓線程(實現方式) new Thread(new RunnableTest()).start();
附測試代碼:
1 class ThreadUseDemo 2 { 3 public static void main(String[] args) 4 { 5 //System.out.println("Hello World!"); 6 //使用第一種方式,開啓線程 7 new Thread() 8 { 9 public void run() 10 { 11 for(int i=0;i<100;i++) 12 { 13 System.out.println(Thread.currentThread().getName()+"線程方式1"+ i); 14 } 15 16 } 17 }.start(); 18 //主線程在這裏運行 19 for(int i=0;i<100;i++) 20 { 21 System.out.println(Thread.currentThread().getName()+"前臺線程"+ i); 22 } 23 24 //使用第二種方式,開啓線程 25 Runnable r =new Runnable() 26 { 27 public void run() 28 { 29 for(int i=0;i<100;i++) 30 { 31 System.out.println(Thread.currentThread().getName()+".....方式2++"+ i); 32 } 33 } 34 }; //不可或缺 35 new Thread(r).start(); 36 37 //使用第三種方式,類實例化開啓線程(繼承方式) 38 new ThreadTest().start(); 39 //使用第四種方式,類實例化開啓線程(實現方式) 40 new Thread(new RunnableTest()).start(); 41 } 42 } 43 44 class ThreadTest extends Thread 45 { 46 public void run() 47 { 48 for(int i=0;i<100;i++) 49 { 50 System.out.println(Thread.currentThread().getName()+".....類的方式(extends)----"+ i); 51 } 52 } 53 54 } 55 class RunnableTest implements Runnable 56 { 57 public void run() 58 { 59 for(int i=0;i<100;i++) 60 { 61 System.out.println(Thread.currentThread().getName()+".........類的方式(implements)----++"+ i); 62 } 63 } 64 }
線程學習告一段落了,以後關於線程其餘的知識點,這裏記下筆記,用的時候再說:
1)線程中止:
經過在主函數中控制標誌位來停止子線程的循環狀態,特殊狀況:
當現場處於凍結狀態(wait)時,就讀取不到flag標記,那麼線程就不會結束。解決辦法:
Interrupt:清楚凍結狀態,固然此時會拋異常,在異常中更改標誌位便可
wait ---
sleep ----===》一磚頭下去---》清醒(運行態)拋異常->有人強制結束,能夠獲取運行資格,操做標記爲false,循環判斷爲假,線程結束
2) 守護線程
eg: t1.setDaemon(true);//此時t1線程爲守護線程,開啓後和前臺線程共同運行,互搶CPU資格,但當主線程(前臺)結束後,守護線程也自動中止(依賴於前臺主線程)
3) join()
eg: t1.start();t1.join();//t1在start後,join表示向CPU申請執行權(CPU交出,處於wait狀態),t1和其餘正在運行的線程一塊兒爭奪,直到t1結束後交還資格給CPU
4) 線程組:誰開啓的線程,就屬於某個組(幾乎用不到)
5)線程優先級:1--10,默認爲5,常常用獲得有:MIN_PRIORITY (1);MAX_PRIORITY (10); NORM_PRIORITY (5 )
定義:
這是java線程的優先級: java.lang.Thread public static final int MAX_PRIORITY 10 public static final int MIN_PRIORITY 1 public static final int NORM_PRIORITY 5
使用:
1 //第一種方案 2 class MyThead implements Runnable 3 { 4 public void run() 5 { 6 for (int i = 1; i <= 10; i++) 7 { 8 System.out.println(Thread.activeCount() + "thread======>AAA"); 9 } 10 } 11 } 12 //第二種方案 13 class MyThreadRunnable extends Thread 14 { 15 16 public void run() 17 { 18 for (int i = 1; i <= 10; i++) 19 { 20 System.out.println(Thread.activeCount() + "thread======BBB"); 21 } 22 } 23 24 } 25 26 public class TheadMain 27 { 28 public static void main(String[] args) 29 { 30 MyThead myThead = new MyThead(); 31 Thread thread = new Thread(myThead); 32 MyThreadRunnable thread2 = new MyThreadRunnable(); 33 thread.start(); 34 thread.setPriority(Thread.MIN_PRIORITY); 35 thread2.start(); 36 thread2.setPriority(Thread.MAX_PRIORITY); 37 } 38 }