java concurrent programming threads collaboration between

Collaboration between threads

1. Wait and notify

Standard form and wait for notification of
waiting by:

  1. Gets the object lock
  2. Loop determination condition is satisfied, not call wait () method
  3. Execute business logic conditions are met

Notify Party:

  1. Obtaining object
  2. Changing conditions
  3. Inform all threads waiting on the object

2. wait、notify/notifyAll

wait: causes the current thread to wait until another thread invokes the current sync monitor notify()or a notifyall()way to wake up this thread. wait()There are three ways form - no time parameters wait (wait until another thread notifies), wait waut with millisecond parameter () and with milliseconds and nanoseconds parameters (automatic wake up after a specified time)
the Notify: wake up this synchronization a single thread waiting on the monitor, if there are multiple threads waiting on this monitor synchronization, then only call a random thread them; waiting threads only are activated, but have to get a lock on again to continue down the implementation , that is to say as long as the lock has not been released, because the original is still waiting thread can not continue to acquire locks.
notifyAll: wake up all waiting on this monitor synchronization of threads, all threads are active.

wait () method, Notify () method and notiftAll () method from java.lang.Object, for coordinating the multi-threaded access to shared data, the method can only be used in synchronous or sync blocks, otherwise an exception is thrown (IllegalMonitorStateException).

3. join

Thread provides a make a thread wait for another thread to complete execution method --join () method; when the program is executed in the other thread calls join () method, the calling thread will be blocked until the join () method join join until the thread is finished.

static class JoinQueue implements Runnable {
    private Thread thread;
    public JoinQueue(Thread thread) {
        this.thread = thread;
    }
    @Override
    public void run() {
        try {
            thread.join();
            System.out.println(Thread.currentThread().getName()+" terminted.");
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
}

public static void main(String[] args) throws InterruptedException {
    Thread previous = Thread.currentThread();
    for (int i = 0; i < 10; i++) {
        Thread thread = new Thread(new JoinQueue(previous), String.valueOf(i));
        System.out.println(" thread " + previous.getName() + " is in front of " + thread.getName());
        thread.start();
        previous = thread;
    }
    Thread.sleep(2000);
    System.out.println(Thread.currentThread()+" done");
}

4. Invoke yield (), Effects of sleep (), wait (), notify () on the lock etc.

Thread execution yield () after holding the lock is not released;
SLEEP () after the method is called, is not held by the lock release;
wait () method is called before the thread must hold the lock, call wait () methods, the lock will be released when the wait () method returns when the thread that holds the lock again;
call notify () before the method, it is necessary to hold the lock, call notify () method itself does not release the lock .

Guess you like

Origin www.cnblogs.com/huizhipeng/p/12079958.html