001/* Helper thread for calling notifyAll()on a LinkedList of locks.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027
028
029 */
030package ptolemy.actor.process;
031
032import java.util.Iterator;
033import java.util.LinkedList;
034
035///////////////////////////////////////////////////////////////////
036//// NotifyThread
037
038/**
039 Helper thread for calling notifyAll() on a single lock or a LinkedList
040 of locks. Since this is a new thread without any locks, calling notifyAll
041 from this thread reduces the possibility of deadlocks.
042 <p>
043 To use this to wake up any threads waiting on a lock, create a new instance
044 of this class with a LinkedList of lock objects (or single lock) to call
045 notifyAll() on.
046 <p>
047 @author Neil Smyth, Mudit Goel
048 @version $Id$
049 @since Ptolemy II 0.2
050 @Pt.ProposedRating Green (mudit)
051 @Pt.AcceptedRating Yellow (mudit)
052
053 */
054public class NotifyThread extends Thread {
055    /** Construct a thread to be used call notifyAll() on a set of locks.
056     *  @param locks The set of locks to call notifyAll() on.
057     */
058    public NotifyThread(LinkedList locks) {
059        this.setName("NotifyThread");
060        _locks = new LinkedList();
061
062        Iterator e = locks.iterator();
063
064        while (e.hasNext()) {
065            _locks.addLast(e.next());
066        }
067    }
068
069    /** Construct a thread to be used call notifyAll() on a set of locks.
070     *  @param lock The lock to call notifyAll() on.
071     */
072    public NotifyThread(Object lock) {
073        this.setName("NotifyThread");
074        _lock = lock;
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Call NotifyAll() on the lock object (or objects) passed to this
081     *  class in its constructor.
082     */
083    @Override
084    public void run() {
085        if (_locks != null) {
086            Iterator objects = _locks.iterator();
087
088            while (objects.hasNext()) {
089                Object nextObj = objects.next();
090
091                synchronized (nextObj) {
092                    nextObj.notifyAll();
093                }
094            }
095        } else {
096            synchronized (_lock) {
097                _lock.notifyAll();
098            }
099        }
100    }
101
102    ///////////////////////////////////////////////////////////////////
103    ////                         private variables                 ////
104    // The locks to call notifyAll() on.
105    private LinkedList _locks = null;
106
107    // The lock to call notifyAll() on.
108    private Object _lock = null;
109}