摘要:詳解并發(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://specialneedsforspecialkids.com/yun/75064.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)鎖的先后順序來。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會(huì)被掛起或者是自旋,然后當(dāng)線程釋放鎖后,線程再被喚醒,以此類推,按照申請(qǐng)鎖的先后順序來。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會(huì)被掛起或者是自旋,然后當(dāng)線程釋放鎖后,線程再被喚醒,以此類推,按照申請(qǐng)鎖的先后順序來。 Node exclusive lock(獨(dú)占鎖) ReentrantLock ReentrantLock實(shí)現(xiàn)了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會(huì)被掛起或...
閱讀 2671·2021-11-25 09:43
閱讀 2579·2021-11-22 09:34
閱讀 2823·2021-11-12 10:34
閱讀 1431·2021-10-20 13:46
閱讀 2300·2019-08-30 13:21
閱讀 929·2019-08-30 11:21
閱讀 483·2019-08-30 11:20
閱讀 2186·2019-08-29 17:20