I have code to communicating 2 threads in JRE6.
When i run following program my expected output is come
like,
A: Hi
B: hi
A: How r u?
B: im fine wat about u?
A: I'm fine
B: Me too
class Chat {
boolean flag = false;
public synchronized void getTalk1(String msg) throws InterruptedException {
if (flag) {
wait();
}
System.out.println(msg);
flag = true;
notify();
}
public synchronized void getTalk2(String msg) throws InterruptedException {
if (!flag) {
wait();
}
System.out.println(msg);
flag = false;
notify();
}
}
class Thread1 extends Thread {
Chat chat;
public Thread1(Chat chat) {
this.chat = chat;
}
String[] talk = { "Hi", "How r u?", "I'm fine" };
@Override
public void run() {
for (int i = 0; i < talk.length; i++) {
try {
chat.getTalk1("A: " + talk[i]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Thread2 extends Thread {
Chat chat;
public Thread2(Chat chat) {
this.chat = chat;
}
String[] talk = { "hi", "im fine wat about u?", "Me too" };
@Override
public void run() {
for (int i = 0; i < talk.length; i++) {
try {
chat.getTalk2("B: " + talk[i]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Conversation {
public static void main(String[] args) {
Chat chat = new Chat();
new Thread1(chat).start();
new Thread2(chat).start();
}
}
But when i change Chat class flag variable boolean to int type
class Chat {
volatile int flag = 2;
public synchronized void getTalk1(String msg) throws InterruptedException {
if (flag == 1) {
wait();
}
System.out.println(msg);
flag = 2;
notify();
}
public synchronized void getTalk2(String msg) throws InterruptedException {
if (flag == 2) {
wait();
}
System.out.println(msg);
flag = 1;
notify();
}
}
The output is varied and executing not stop like
A: Hi
A: How r u?
A: I'm fine
...still running
What is the reason?