摘要:詳解并發之詳解中實現如下其中利用了的方法,調用的前提是已經獲得線程的鎖,如果對象被鎖住則會等待其被釋放。
Thread詳解
Java并發之AQS詳解
Thread中join實現如下:
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方法,調用的前提是已經獲得join線程的鎖,如果thread對象被鎖住則會等待其被釋放。
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
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/77902.html
摘要:詳解并發之詳解中實現如下其中利用了的方法,調用的前提是已經獲得線程的鎖,如果對象被鎖住則會等待其被釋放。 Thread詳解Java并發之AQS詳解 Thread中join實現如下: public final synchronized void join(long millis) throws InterruptedException { long base = System.c...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會被掛起或者是自旋,然后當線程釋放鎖后,線程再被喚醒,以此類推,按照申請鎖的先后順序來。 Node exclusive lock(獨占鎖) ReentrantLock ReentrantLock實現了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會被掛起或者是自旋,然后當線程釋放鎖后,線程再被喚醒,以此類推,按照申請鎖的先后順序來。 Node exclusive lock(獨占鎖) ReentrantLock ReentrantLock實現了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會被掛起或...
摘要:公平鎖線程占用鎖,等待,然后依次獲取鎖,其中會被掛起或者是自旋,然后當線程釋放鎖后,線程再被喚醒,以此類推,按照申請鎖的先后順序來。 Node exclusive lock(獨占鎖) ReentrantLock ReentrantLock實現了公平鎖與非公平鎖,公平鎖提供順序獲取鎖的方式,而非公平鎖提供搶占式獲取鎖的方式。公平鎖: 線程A占用鎖,B等待,然后依次獲取鎖,其中B會被掛起或...
閱讀 3685·2021-09-07 10:19
閱讀 3626·2021-09-03 10:42
閱讀 3583·2021-09-03 10:28
閱讀 2547·2019-08-29 14:11
閱讀 808·2019-08-29 13:54
閱讀 1592·2019-08-29 12:14
閱讀 416·2019-08-26 12:12
閱讀 3613·2019-08-26 10:45