/*- * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * ============LICENSE_END========================================================= */ package org.openecomp.appc.concurrent; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeoutException; import org.openecomp.appc.util.StringHelper; /** * This class is used to synchronize signaling of status between threads. *
* In complex multi-threaded applications it is often necessary to synchronize operations between threads. This is * especially true in complex algorithms where processing dependencies exist between different threads and the * synchronization of the operations of those threads is required. This class is a framework to enable multi-thread * signaling and wait/post logic that makes the thread synchronization easier. *
** Basically, in thread synchronization, one thread is the "waiter" and one or more other threads are the "notifiers". * The notifiers send signals to the waiter to inform that thread that certain conditions are true, processing has been * completed, or to inform the waiter of the state of the other thread(s). In the basic java framework, the waiter and * notifier are simply using the wait/notify mechanism provided, which does not allow for different conditions, state, * or "signals" to exist. The wait/notify mechanism, in combination with the object mutex, provides basic blocking and * releasing of a thread's dispatching state. *
** This class builds upon the java wait/notify mechanism and allows for "signals" to be defined. These signals are * simply string constants that mean something to the waiter and notifier threads. Any number of signals may be defined, * and it is possible to wait for more than one signal to be received, wait for any one of a set to be received, or to * test if a signal has been received without blocking. *
** Some operations are blocking operations. These stop the execution of the calling thread until the specified condition * is true. These blocking methods are all named "wait...", such as {@link #waitFor(String...)} and * {@link #waitForAny(String...)}. The thread making the call to these blocking methods MUST be the waiter thread (the * thread registered with the signal object). *
** Some operations are non-blocking. These operations allow for the testing or setting of signal conditions and do not * block the caller. When calling these methods ({@link #isSignaled(String)}, {@link #signal(String)}, and * {@link #setTimeout(long)} the waiter thread mutex will be held and may block the waiter thread for the duration of * the method call. *
*/ public class Signal { /** * The thread must be the thread of the waiter that is waiting for the signals to be received. It is the recipient * of the signaled condition. This allows any number of other threads to send signals to the recipient and have the * recipient synchronize its operation with the receipt of the appropriate signal(s). */ private Thread thread; /** * The amount of time to wait for a signal to be receieved. Set to zero to wait forever. */ private long timeout = 0L; /** * The collection of all received signals. Note, this need not be a synchronized collection because it will always * be accessed while holding the mutex of the thread, therefore it is implicitly synchronized. */ private List