加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

线程通信

发布时间:2020-12-15 06:50:55 所属栏目:Java 来源:网络整理
导读:1.? wait / notify 要想执行 wait() / notify() / notifyAll() 这些方法,必须首先获得对象的监视器 换言之,这些方法必须在?synchronized 中执行 package com.example.demo22; import java.util.LinkedList; public class App2 { static void main(String[]

1.? wait / notify

要想执行 wait() / notify() / notifyAll() 这些方法,必须首先获得对象的监视器

换言之,这些方法必须在?synchronized 中执行

package com.example.demo22;

import java.util.LinkedList;

public class App2 {

    static void main(String[] args) {

        final Factory factory = new Factory();

        Thread producer = new Thread( Runnable() {
            @Override
             run() {
                try {
                    while (true) {
                        factory.offer();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread consumer = ) {
                        factory.take();
                    }
                }  (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        producer.start();
        consumer.start();
    }


}

 Factory {

    private LinkedList<Object> container = new LinkedList<>();

    private Integer capacity = 5;

    synchronized void offer() throws InterruptedException {
        while (container.size() == capacity) {
            wait();
        }

        int e = (int) Math.ceil(Math.random() * 100);
        container.push(e);
        System.out.println("生产" + e);
        notify();
    }


    void take() while (container.size() == 0) {
            wait();
        }

        Object e = container.poll();
        System.out.println("消费" + e);
        notify();
    }

}

2.? Condition??

 java.util.concurrent.locks.Condition;
 java.util.concurrent.locks.Lock;
 java.util.concurrent.locks.ReentrantLock;

 App {

     main(String[] args) {
        final Worker worker =  Worker();

        for (int i = 0; i < 10; i++) {
                    worker.red();
                }
            }
        }).start();
        ) {
                    worker.green();
                }
            }
        }).start();
        ) {
                    worker.yellow();
                }
            }
        }).start();

    }

}

 Worker {

    private final Lock lock =  ReentrantLock();
    final Condition conditionA = lock.newCondition();
    final Condition conditionB =final Condition conditionC = lock.newCondition();

    private Integer signal = 1;


     red() {
        lock.lock();
         {
            while (signal != 1) {
                conditionA.await();
            }
            System.out.println("Red");
            signal = 2;
            conditionB.signal();
        }  (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

     green() {
        lock.lock();
        while (signal != 2) {
                conditionB.await();
            }
            System.out.println("Green");
            signal = 3;
            conditionC.signal();
        }  yellow() {
        lock.lock();
        while (signal != 3) {
                conditionC.await();
            }
            System.out.println("Yellow");
            signal = 1;
            conditionA.signal();
        }  {
            lock.unlock();
        }
    }

}

3.? wait()

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object,or some other thread interrupts the current thread,or a certain amount of real time has elapsed.

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

调用wait()方法会造成当前线程等待,直到另一个线程调用这个对象的notify()或者notifyAll()方法,或者其它线程打断当前线程,或者时钟周期耗尽。

调用wait()方法必须当前线程首先获得这个对象的监视器。线程释放对象的监视器的所有权,并等待直到另一个线程通知由于这个对象的监视器造成等待的线程来唤醒(通过调用notify()或notifyAll()方法)。线程等待直到它再次获得对象的监视器所有权。

synchronized (obj) {
    while (<condition does not hold>)
    obj.wait();
 ... // Perform action appropriate to condition
}

?4.? notify()

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object,one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.?

唤醒一个在对象的监视器等待的线程。如果有多个线程等待在该对象的监视器上,它们中的一个会被选中唤醒。这个选中是任意的,并且取决于具体实现。

5.? notifyAll()

Wakes up all threads that are waiting on this object's monitor.

唤醒在该对象的监视器上等待的所有线程

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读