Datasets:

ArXiv:
License:
File size: 3,010 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Chat {
    boolean flag = false;

    public synchronized void sendText(String msg) { // Person-1
        if (flag) {
            try {
                wait();
                // ? Why does the wait() call have a warning?
            } catch (InterruptedException e) {
                e.printStackTrace();
                // ? Why does this catch block have a warning?
            }
        }
        System.out.println("Person-1: " + msg);
        flag = true;
        notify();
        // ? Why does using notify() over here lead to a warning?
    }

    public synchronized void sendReply(String msg) { // Person-2
        while (!flag) {
            try {
                // * ANSWER
                /**
                 * When waiting upon a Condition, a "spurious wakeup" is permitted to occur, in
                 * general, as a concession to the underlying platform semantics.
                 */
                wait();
            } catch (InterruptedException e) {
                // * ANSWER
                /**
                 * InterruptedExceptions should never be ignored in the code, and simply logging
                 * the exception counts in this case as "ignoring". The throwing of the
                 * InterruptedException clears the interrupted state of the Thread, so if the
                 * exception is not handled properly the fact that the thread was interrupted
                 * will be lost.
                 */
                e.printStackTrace();
                Thread.currentThread().interrupt(); // restore interrupted state
            }
        }
        System.out.println("Person-2: " + msg);
        flag = false;
        notifyAll();
        // * ANSWER
        // notify and notifyAll both wake up sleeping threads, but notify only
        // rouses one, while notifyAll rouses all of them. Since notify might not wake
        // up the right thread, notifyAll should be used instead.
    }
}

class Person1 implements Runnable {
    Chat personalChat;
    String[] messages = { "HiπŸ˜€", "Just checking up on you. How are you?", "I am also doing fine.😁" };

    public Person1(Chat personalChat) {
        this.personalChat = personalChat;
        new Thread(this, "Question").start();
    }

    public void run() {
        for (int i = 0; i < messages.length; i++) {
            personalChat.sendText(messages[i]);
        }
    }
}

class Person2 implements Runnable {
    Chat personalChat;
    String[] messages = { "Hi!πŸ™Œ", "I am good.πŸ‘Œ What about you?", "Cool!πŸ‘" };

    public Person2(Chat personalChat) {
        this.personalChat = personalChat;
        new Thread(this, "Answer").start();
    }

    public void run() {
        for (int i = 0; i < messages.length; i++) {
            personalChat.sendReply(messages[i]);
        }
    }
}

public class ChatThreadSafetyDemo {
    public static void main(String[] args) {
        Chat justAChat = new Chat();
        new Person1(justAChat);
        new Person2(justAChat);
    }
}