Java多线程复习_Java多线程复习
一、線程的基本概念
簡單的說:線程就是一個程序里不同的執行路徑
在同一個時間點上cpu只會有一個線程在執行
Java里的多線程是通過java.lang.Thread類來實現的
每個線程都擁有自己獨立的方法棧空間
二、java線程的創建和啟動
第一種
定義線程類實現Runnable接口
Thread myThread = new Thread(target)?//target為Runnable接口類型
Runnable中只有一個方法:
public void run();用以定義線程運行體
第二種
可以定義一個Thread的子類并重寫其run方法:
clas MyThread extends Thread{
public void run(){}
}
線程類必須通過執行Thread的start()方法啟動一個新的線程
如果調用run()方法是屬于方法的調用,不會啟動一個新的線程
推薦使用第一種方式創建線程,使用接口較為靈活
二、線程狀態裝換
調用線程start()方法時,線程進入就緒狀態,Cpu分配時間片,線程進入運行狀態
時間片結束,run()方法未執行完,線程進入阻塞狀態。
三、線程控制基本方法
isAlive()?//判斷線程是否還“活著”,即線程是否還未終止
getPriority()?//獲得線程的優先級數值
setPriority()?//設置線程的優先級指數
Thread.sleep()?//靜態方法,將當前線程睡眠指定毫秒數
join()??//調用某線程的該方法,將當前線程與該線程合并,即等待該線程結束,再回復當前線程的運行。
yield()??//讓出CPU,當前線程進入就緒狀態等待調度,并執行其它線程。
interrupt()?//中斷線程
wait()??//當前線程進入對象的wait pool
notify()/all?//喚醒對象的wait pool中的一個或所有等待線程
四、sleep方法?Thread的靜態方法
public static void sleep(long millis)throws InterruptedException //必須對異常進行捕捉
Thread.currentThread();??//得到當前線程
五、一種讓線程退出的方式(interrupt方法)。
1 importjava.util.*;2 publicclassTestInterrupt{3 publicstaticvoidmain(String[] args){4 MyThread t=newMyThread();5 t.start();6 try{Thread.sleep(10000);}7 catch(InterruptedException i){}8 t.interrupt();9 }10 }11 12 classMyThreadextendsThread{13 publicvoidrun(){14 while(true){15 try{16 System.out.println("------"+newDate()+"-----");17 Thread.sleep(1000);//主線程sleep10秒結束將interrupt該線程的輸出18 }catch(InterruptedException i){19 return;20 }21 }22 }23 }
六、join和yield方法
t.join();?//t的run()方法執行完才會繼續執行當前線程方法體
t.yield();?//暫停當前正在執行的線程對象,并執行其他線程。方法為靜態
1 publicclassTestYield {2 publicstaticvoidmain(String[] args) {3 MyThread3 t1=newMyThread3("t1");4 MyThread3 t2=newMyThread3("t2");5 t1.start(); t2.start();6 }7 }8 classMyThread3extendsThread {9 MyThread3(String s){super(s);}10 publicvoidrun(){11 for(inti=1;i<=100;i++){12 System.out.println(getName()+":"+i);13 if(i%10==0){14 yield();15 }16 }17 }18 }
注意:yield()只是使當前線程重新回到可執行狀態,所以執行yield()的線程有可能在進入到可執行狀態后馬上又被執行。所以上面的程序不會實現2個線程交替每次輸出10個數字!
七、線程優先級別
線程的優先級用數字表示,范圍從1到10,一個線程的缺省優先級為5.
Thread.MAX_PRIORITY=1
Thread.MIN_PRIORITY=10
Thread.NORM_PRIORITY=5
例:t.setPriority(Thread.NORM_PRIORITY+3);
★八、線程同步
1.同步代碼塊
synchronized(this){??//在執行代碼塊過程中,不會被其他線程打斷
...
}
public sunchronized void method?//執行此方法時,當前對象被鎖定
在Java語言中,引入了對象互斥鎖的概念,保證共享數據操作的完整性,每個對象?都對應一個可稱為"互斥鎖"的標記,這個標記保證在任一時刻,只能有一個線程訪?問該對象。
2.生產者、消費者model
1 /*@srchttp://eric-619.javaeye.com/blog/6936812 * 生產者消費者問題其含義就是先生產出了產品,才能拉出去讓消費者購買3 * 一、重點:4 * 1、多個線程數據共享區域化思想!---源于多線程的近親思想!!(類似于靜態變量的改變)5 * (如棧內存和對內存,還有當做棧內存和堆內存,如數組和基本數據類型,只要是訪問的同一個。)6 * 2、生產者消費者7 *8 * 二、synchronized加鎖:9 *10 */11 12 13 publicclassProCon{//主方法14 15 publicstaticvoidmain(String[] args){16 SyncStack stack=newSyncStack();17 Consumer p=newConsumer(stack);18 Producer c=newProducer(stack);19 20 21 newThread(p).start();22 newThread(c).start();23 }24 }25 26 classProducerimplementsRunnable{//生產者27 privateSyncStack stack;28 29 publicProducer(SyncStack stack){30 this.stack=stack;31 }32 33 publicvoidrun(){34 for(inti=0; i
總結
以上是生活随笔為你收集整理的Java多线程复习_Java多线程复习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java自动注入默认_java – 自动
- 下一篇: JSONP 跨域共享信息