摘要:詳解并發(fā)之詳解中實(shí)現(xiàn)如下其中利用了的方法,調(diào)用的前提是已經(jīng)獲得線程的鎖,如果對(duì)象被鎖住則會(huì)等待其被釋放。
Thread詳解
Java并發(fā)之AQS詳解
Thread中join實(shí)現(xiàn)如下:
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }
其中利用了Object的wait方法,調(diào)用的前提是已經(jīng)獲得join線程的鎖,如果thread對(duì)象被鎖住則會(huì)等待其被釋放。
import core.AbstractCommonConsumer; import core.CommonUtils; import core.MagnetMonitor; import core.NamedThreadPoolExecutor; import org.apache.ibatis.io.Resources; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.concurrent.*; import java.util.concurrent.locks.LockSupport; class JoinTester01 implements Runnable { private String name; JoinTester01(String name) { this.name = name; } public void run() { System.out.printf("%s begins: %s ", name, new Date()); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("%s has finished: %s ", name, new Date()); } } class JoinTester02 implements Runnable { Thread thread; JoinTester02(Thread thread) { this.thread = thread; } Thread getThread(){ return this.thread; } public void run() { synchronized (thread) { System.out.printf("getObjectLock at %s ", new Date()); try { Thread.sleep(2000); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.printf("ReleaseObjectLock at %s ", new Date()); } try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } synchronized (thread) { System.out.printf("getObjectLock again at %s ", new Date()); try { Thread.sleep(2000); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.printf("ReleaseObjectLock again at %s ", new Date()); } } } class TestMain { public static void main(String[] args) { Thread thread = new Thread(new JoinTester01("Leon")); JoinTester02 tester02 = new JoinTester02(thread); Thread getLockThread = new Thread(tester02); getLockThread.start(); thread.start(); try { System.out.printf("start join at %s ", new Date()); thread.join(1000); System.out.printf("stop join at %s ", new Date()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
start join at Sun Jun 30 18:40:08 CST 2019 Leon begins: Sun Jun 30 18:40:08 CST 2019 getObjectLock at Sun Jun 30 18:40:08 CST 2019 ReleaseObjectLock at Sun Jun 30 18:40:10 CST 2019 stop join at Sun Jun 30 18:40:11 CST 2019 getObjectLock again at Sun Jun 30 18:40:11 CST 2019 ReleaseObjectLock again at Sun Jun 30 18:40:13 CST 2019 Leon has finished: Sun Jun 30 18:40:18 CST 2019
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/77902.html
摘要:詳解并發(fā)之詳解中實(shí)現(xiàn)如下其中利用了的方法,調(diào)用的前提是已經(jīng)獲得線程的鎖,如果對(duì)象被鎖住則會(huì)等待其被釋放。 Thread詳解Java并發(fā)之AQS詳解 Thread中join實(shí)現(xiàn)如下: public final synchronized void join(long millis) throws InterruptedException { long base = System.c...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會(huì)被掛起或者是自旋,然后當(dāng)線程釋放鎖后,線程再被喚醒,以此類推,按照申請(qǐng)鎖的先后順序來(lái)。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會(huì)被掛起或者是自旋,然后當(dāng)線程釋放鎖后,線程再被喚醒,以此類推,按照申請(qǐng)鎖的先后順序來(lái)。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會(huì)被掛起或者是自旋,然后當(dāng)線程釋放鎖后,線程再被喚醒,以此類推,按照申請(qǐng)鎖的先后順序來(lái)。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
閱讀 3670·2021-09-07 10:19
閱讀 3611·2021-09-03 10:42
閱讀 3571·2021-09-03 10:28
閱讀 2531·2019-08-29 14:11
閱讀 781·2019-08-29 13:54
閱讀 1560·2019-08-29 12:14
閱讀 394·2019-08-26 12:12
閱讀 3595·2019-08-26 10:45