java的高級特性——線程1

  1. 線程
    (1) cpu進行調度的最小單位
    (2) 進程:運行中的應用
    (3) 一個進程中至少有一個線程,該線程由jvm發起,稱之爲主線程
    (4) 線程的生命週期

       1、創建線程對象:    新生new born
       2、調用start方法後: 就緒ready(程序員對線程不可控)
       3、CPU 調度後       運行running 阻塞blocked(sleep/wait(0)->  notify)
       			  阻塞狀態正常結束後,進入就緒狀態
       4、run方法結束:     死亡狀態  死亡dead(自然死亡:讓循環條件不成立)
    

(5) 開啓一條線程的方法

a、匿名內部類創建線程
Thread thread = new Thread(){ 
              @overridee    
                          Public void run(){
                          ....}
                          };
                          Thread.start() 
b、自定義類繼承 Thread
Class MyThread extends Thread{
//自定義屬性
...
//自定義方法
...
@override
Public void run(){
....}
}
Thread thread = new MyThread ();
Thread.start();
c、自定義實現Runable接口
Class MyRun implents Runable{
  @overridePublic 
  void run(){}
   }
   Runable mr= new MyRun ();
   Thread thread = new Thread(mr);
   Thread.start();
d、線程池//緩存線程池:小型任務
public class MyCall implements Callable<T>{
//自定義屬性
//自定義方法
@override
Public T call(){
...
}
}
Mycall mc = new Mycall(...);
Future<T> fu = esf.submit(mc);
T t = fu.get();     
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章