国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java中線程的基本方法使用技巧

Java中線程的基本方法使用技巧

2021-01-04 15:51jianshu Java教程

這篇文章主要介紹了Java中線程的基本方法使用技巧,需要的朋友可以參考下

java線程的基本方法的熟練使用是精通多線程編程的必經(jīng)之路,線程相關(guān)的基本方法有wait,notify,notifyAll,sleep,join,yield等,本文淺要的介紹一下它們的使用方式。

線程的狀態(tài)圖

Java中線程的基本方法使用技巧

java將操作系統(tǒng)中的就緒和運行兩種狀態(tài)統(tǒng)稱為可運行狀態(tài),java中線程的狀態(tài)可以認為有以上六種。

wait

調(diào)用該方法的線程進入WAITING狀態(tài),只有等待另外線程的通知或被中斷才會返回,需要注意的是調(diào)用wait()方法后,會釋放對象的鎖。

因此,wait方法一般用在同步方法或同步代碼塊中。

sleep

sleep導致當前線程休眠,與wait方法不同的是sleep不會釋放當前占有的鎖,sleep(long)會導致線程進入TIMED-WATING狀態(tài),而wait()方法會導致當前線程進入WATING狀態(tài)

yield

yield會使當前線程讓出CPU執(zhí)行時間片,與其他線程一起重新競爭CPU時間片。一般情況下,優(yōu)先級高的線程有更大的可能性成功競爭得到CPU時間片,但這又不是絕對的,有的操作系統(tǒng)對線程優(yōu)先級并不敏感。

interrupt

中斷一個線程,其本意是給這個線程一個通知信號,會影響這個線程內(nèi)部的一個中斷標識位。這個線程本身并不會因此而改變狀態(tài)(如阻塞,終止等)。

1.調(diào)用interrupt()方法并不會中斷一個正在運行的線程。也就是說處于Running狀態(tài)的線程并不會因為被中斷而被終止,僅僅改變了內(nèi)部維護的中斷標識位而已。

2.若調(diào)用sleep()而使線程處于TIMED-WATING狀態(tài),這時調(diào)用interrupt()方法,會拋出InterruptedException,從而使線程提前結(jié)束TIMED-WATING狀態(tài)。

3.許多聲明拋出InterruptedException的方法(如Thread.sleep(long mills方法)),拋出異常前,都會清除中斷標識位,所以拋出異常后,調(diào)用isInterrupted()方法將會返回false。

4.中斷狀態(tài)是線程固有的一個標識位,可以通過此標識位安全的終止線程。比如,你想終止一個線程thread的時候,可以調(diào)用thread.interrupt()方法,在線程的run方法內(nèi)部可以根據(jù)thread.isInterrupted()的值來優(yōu)雅的終止線程。當然,你可以在線程內(nèi)部自己維護一個boolean變量來控制線程的運行和終止。

現(xiàn)在,我們看一下源碼里這個方法是怎么說明的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * Interrupts this thread.
 * 中斷這個線程
 *
 * <p> Unless the current thread is interrupting itself, which is
 * always permitted, the {@link #checkAccess() checkAccess} method
 * of this thread is invoked, which may cause a {@link
 * SecurityException} to be thrown.
 * 如果不是當前線程中斷自身,這經(jīng)常是被允許的。不過要檢查安全性,
 * 可能會拋出安全異常的。
 *
 * <p> If this thread is blocked in an invocation of the {@link
 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 * class, or of the {@link #join()}, {@link #join(long)}, {@link
 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 * methods of this class, then its interrupt status will be cleared and it
 * will receive an {@link InterruptedException}.
 * 如果中斷的線程由于調(diào)用一個Object對象的多個wait方法或者當前對象的
 * join,sleep方法而正處于阻塞狀態(tài)(僅表示沒有獲得CPU的時間片執(zhí)行,不表示
 * 線程的BLOCKED狀態(tài))。那么它的中斷狀態(tài)將被清除(復位),而且它會收到中
 * 斷異常。
 *
 * <p> If this thread is blocked in an I/O operation upon an {@link
 * java.nio.channels.InterruptibleChannel InterruptibleChannel}
 * then the channel will be closed, the thread's interrupt
 * status will be set, and the thread will receive a {@link
 * java.nio.channels.ClosedByInterruptException}.
 *
 * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
 * then the thread's interrupt status will be set and it will return
 * immediately from the selection operation, possibly with a non-zero
 * value, just as if the selector's {@link
 * java.nio.channels.Selector#wakeup wakeup} method were invoked.
 *
 * <p> If none of the previous conditions hold then this thread's interrupt
 * status will be set. </p>
 * 如果上述的條件都沒有比中的話,那么這個線程的中斷標志位將被設(shè)置。
 *
 * <p> Interrupting a thread that is not alive need not have any effect.
 * 中斷一個不存活的線程(未啟動或已結(jié)束)不會有任何影響
 *
 * @throws SecurityException
 *  if the current thread cannot modify this thread
 *
 * @revised 6.0
 * @spec JSR-51
 */
 public void interrupt() {
 //檢查當前線程對this線程的安全權(quán)限,如果不允許修改,會拋出異常
 if (this != Thread.currentThread())
  checkAccess();
 //加鎖同步
 synchronized (blockerLock) {
  Interruptible b = blocker;
  if (b != null) {
  interrupt0();  // Just to set the interrupt flag
  b.interrupt(this);
  return;
  }
 }
 interrupt0(); //設(shè)置標識位,本地方法
 }

基本很簡單,首先檢查當前線程對this線程的安全權(quán)限,如果不允許修改,會拋出異常。隨后加鎖同步設(shè)置中斷標識位。盡管方法聲明中有詳細說明,但是代碼中看不出來,處于wating狀態(tài)的線程被中斷后,中斷標識會復位。我認為這是靠本地代碼interrupt0實現(xiàn)的。

join

在線程A上下文中執(zhí)行了線程B.join()語句,其含義是線程B執(zhí)行結(jié)束后,join()方法才會返回,線程A才可繼續(xù)執(zhí)行。

舉個例子,如果你創(chuàng)建了10個線程,同時調(diào)用start()方法執(zhí)行線程,但是你想讓它們有序執(zhí)行,就可以使用join來輔助完成。代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/***
* 此示例中10個線程在執(zhí)行時,,需要等待前一個線程執(zhí)行完;
* 比如線程0要等待main線程執(zhí)行完
* 線程9要等到線程8執(zhí)行完
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread previous = Thread.currentThread();
for (int i = 0; i < 10; i++) {
 Thread thread = new Thread(new Dimon(previous), String.valueOf(i));
 thread.start();
 previous = thread;
}
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName() + "terminate");
}
static class Dimon implements Runnable{
private Thread thread;
public Dimon(Thread thread){
 this.thread = thread;
}
@Override
public void run() {
 try {
 thread.join();
 }catch (Exception e){
 }
 System.out.println( Thread.currentThread().getName() + ":terminate" );
}
}

總結(jié)

以上所述是小編給大家介紹的Java中線程的基本方法使用技巧,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.jianshu.com/p/56ffebe60310

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产专区在线 | jizz欧美大片 | 成人在线网址 | 成人综合网站 | av黄色在线 | 国内精品久久久久久中文字幕 | 一区二区不卡视频 | 亚洲一区二区三区在线 | 国产日韩视频 | 91国内外精品自在线播放 | 久久五月天婷婷 | 国产精品美女在线观看 | 亚洲狠狠 | av黄色网页 | 免费一二三区 | 天堂资源最新在线 | 亚洲激情在线 | 能直接看的av网站 | 香蕉久久久久久 | 在线a电影 | 黄色一级片| 国产精品国产三级国产aⅴ中文 | 日本一区二区在线观看视频 | 色伊人| 国产精品久久久久久久久免费高清 | 亚洲精品久久久久久下一站 | 国产成人综合在线观看 | 波多野结衣福利电影 | 欧美国产伦久久久久久 | 久久综合九色综合网站 | 欧美一区二区三区免费 | 久久精品无码一区二区三区 | 亚洲视频中文字幕 | 免费看少妇高潮一级毛片特黄 | 亚洲精品成人 | 夜夜草视频 | 欧美麻豆视频 | 精品亚洲一区二区三区 | 精品久久久久一区二区国产 | 国产精品久久九九 | 欧美激情视频一区 |