gpt4 book ai didi

java - Java同步等待和通知方法

转载 作者:行者123 更新时间:2023-12-03 13:12:25 27 4
gpt4 key购买 nike

我是同步的新手,试图了解等待和通知的工作原理。

问题:-

程序同时执行2个线程-T1和T2,但是基于输出T1首先运行,执行问题(first)方法,打印问题,设置标志(true),运行notify()方法,执行问题(第二)方法并输入等待方法。现在,T2启动并执行。

  • 为什么直到T1调用对象聊天的wait方法后T2才开始。
  • 第一次执行问题方法并调用notify()方法时,聊天对象上没有wait()(因为t2尚未启动)。因此,哪个线程监听此notify方法。

  • 我的代码:
    package TestThread;

    class Chat {
    boolean flag = false;

    public synchronized void Question(String msg) {

    System.out.println("Question method = " + flag);
    if (flag) {
    try {
    System.out.println("Question method wait start");
    wait();
    System.out.println("Question method wait finish");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println(msg);
    flag = true;
    notify();
    }

    public synchronized void Answer(String msg) {
    System.out.println("Answer method = " + flag);
    if (!flag) {
    try {
    System.out.println("Answer method wait start");
    wait();
    System.out.println("Answer method wait finish");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    System.out.println(msg);
    flag = false;
    notify();
    }
    }

    class T1 implements Runnable {
    Chat m;
    String[] s1 = { "Hi", "How are you ?", "I am also doing fine!" };

    public T1(Chat m1) {
    this.m = m1;
    new Thread(this, "Question").start();
    }

    public void run() {
    for (int i = 0; i < s1.length; i++) {
    m.Question(s1[i]);
    }
    }
    }

    class T2 implements Runnable {
    Chat m;
    String[] s2 = { "Hi", "I am good, what about you?", "Great!" };

    public T2(Chat m2) {
    this.m = m2;
    new Thread(this, "Answer").start();
    }

    public void run() {
    for (int i = 0; i < s2.length; i++) {
    m.Answer(s2[i]);
    }
    }
    }

    public class MultiThread {
    public static void main(String[] args) {
    Chat m = new Chat();
    new T1(m);
    new T2(m);
    }
    }

    结果:-
    Question method = false
    Hi
    Question method = true
    Question method wait start
    Answer method = true
    Hi
    Answer method = false
    Answer method wait start
    Question method wait finish
    How are you ?
    Question method = true
    Question method wait start
    Answer method wait finish
    I am good, what about you?
    Answer method = false
    Answer method wait start
    Question method wait finish
    I am also doing fine!
    Answer method wait finish
    Great!

    最佳答案

    Why does T2 doesn't start until T1 calls the wait method of the Object Chat.



    您有一个相同的对象“m”,并将其传递给不同的线程,并且存在 方法级别的同步,因此,在“调用”进入question()的那一刻,整个对象将被锁定,直到调用wait()为止。方法将被阻止。

    When the question method is executed first time and notify() method is called, there is no wait() on the chat object(since t2 did not yet start). so, which thread listens to this notify method.



    notify()第一次没有意义,同步方法必须完成多数民众赞成。如之前所述,在question()完​​成/释放锁之前,不会执行answer()(调用wait())

    关于java - Java同步等待和通知方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27182266/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com