如何将线程中的wait方法唤醒

如题所述

使用ReentrantLock配合多个condition来达到唤醒指定线程的目的,将唤醒目标 condition ä¸Žthread åšå…³è”,建立联系,找到thread的instance就可以找到thread的监视对象condition.



import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;


public class NotifySpecfiedThread {


    private static final Map<Thread, Condition> notifyRelationship = new ConcurrentHashMap<Thread, Condition>();

    private static final ReentrantLock locker = new ReentrantLock(false);


    /**

     * @param args

     * @throws InterruptedException

     */

    public static void main(String[] args) throws InterruptedException {

        final int waitThreadNumber = 10;

        for (int i = 1; i <= waitThreadNumber; i++) {

            final Thread t = new WaitThreadTest();

            notifyRelationship.put(t, locker.newCondition());

        }

        final Set<Thread> threads = notifyRelationship.keySet();

        for (final Thread t : threads) {

            t.start();

        }


        new NotifyThreadTest().join();

    }


    static class WaitThreadTest extends Thread {

        public WaitThreadTest() {

        }


        /**

         * notify the specified thread using different condition and lock with

         * the same ReentrantLock instance

         */

        public void run() {

            locker.lock();

            try {

                final Thread t = Thread.currentThread();

                final ThreadLocal<Boolean> isWait = new ThreadLocal<Boolean>();

                isWait.set(true);


                while (isWait.get()) {

                    try {

                        System.out.println("i am ready to wait,"

                                + " the thread name is:" + t.getName());

                        final Condition c = notifyRelationship.get(this);

                        c.await();

                        isWait.set(false);

                    } catch (InterruptedException ex) {

                        System.err.println(ex.getMessage());

                    }

                }

                System.out.println("i have been released, the thread name is:"

                        + t.getName());

            } finally {

                locker.unlock();

            }


        }

    }


    /**

     * loop all the condition instance and release the wait thread..

     */

    static class NotifyThreadTest extends Thread {

        public NotifyThreadTest() {

            this.start();

        }


        public void run() {

            final Set<Entry<Thread, Condition>> entrySet = notifyRelationship

                    .entrySet();

            for (final Entry<Thread, Condition> entry : entrySet) {

                final Thread t = entry.getKey();

                final Condition c = entry.getValue();


                System.out.println("i am ready to signal, "

                        + "will be signaled thread name is:" + t.getName());

                locker.lock();

                try {

                    c.signal();

                } finally {

                    locker.unlock();

                }

                System.out.println("i have alreay signaled the specified "

                        + "thread:" + t.getName());

            }


        }

    }

}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答