资讯专栏INFORMATION COLUMN

Thread and AbstractQueuedSynchronizer

notebin / 1417人阅读

摘要:详解并发之详解中实现如下其中利用了的方法,调用的前提是已经获得线程的锁,如果对象被锁住则会等待其被释放。

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77902.html

相关文章

  • Thread and AbstractQueuedSynchronizer

    摘要:详解并发之详解中实现如下其中利用了的方法,调用的前提是已经获得线程的锁,如果对象被锁住则会等待其被释放。 Thread详解Java并发之AQS详解 Thread中join实现如下: public final synchronized void join(long millis) throws InterruptedException { long base = System.c...

    LeanCloud 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    omgdog 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    chanjarster 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    zhunjiee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<