<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://wiki.twig.es/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.twig.es/index.php?action=history&amp;feed=atom&amp;title=Communicating_between_java_threads</id>
		<title>Communicating between java threads - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.twig.es/index.php?action=history&amp;feed=atom&amp;title=Communicating_between_java_threads"/>
		<link rel="alternate" type="text/html" href="https://wiki.twig.es/index.php?title=Communicating_between_java_threads&amp;action=history"/>
		<updated>2026-05-06T15:41:24Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.0</generator>

	<entry>
		<id>https://wiki.twig.es/index.php?title=Communicating_between_java_threads&amp;diff=2201&amp;oldid=prev</id>
		<title>George2: Created page with &quot;&lt;source lang=&quot;java&quot;&gt;  class Chat {     boolean flag = false;      public synchronized void Question(String msg) {         if (flag) {             try {                 wait();...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.twig.es/index.php?title=Communicating_between_java_threads&amp;diff=2201&amp;oldid=prev"/>
				<updated>2015-07-12T13:12:45Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;  class Chat {     boolean flag = false;      public synchronized void Question(String msg) {         if (flag) {             try {                 wait();...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Chat {&lt;br /&gt;
    boolean flag = false;&lt;br /&gt;
&lt;br /&gt;
    public synchronized void Question(String msg) {&lt;br /&gt;
        if (flag) {&lt;br /&gt;
            try {&lt;br /&gt;
                wait();&lt;br /&gt;
            } catch (InterruptedException e) {&lt;br /&gt;
                e.printStackTrace();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println(msg);&lt;br /&gt;
        flag = true;&lt;br /&gt;
        notify();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public synchronized void Answer(String msg) {&lt;br /&gt;
        if (!flag) {&lt;br /&gt;
            try {&lt;br /&gt;
                wait();&lt;br /&gt;
            } catch (InterruptedException e) {&lt;br /&gt;
                e.printStackTrace();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        System.out.println(msg);&lt;br /&gt;
        flag = false;&lt;br /&gt;
        notify();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class T1 implements Runnable {&lt;br /&gt;
    Chat m;&lt;br /&gt;
    String[] s1 = { &amp;quot;Hi&amp;quot;, &amp;quot;How are you ?&amp;quot;, &amp;quot;I am also doing fine!&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
    public T1(Chat m1) {&lt;br /&gt;
        this.m = m1;&lt;br /&gt;
        new Thread(this, &amp;quot;Question&amp;quot;).start();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void run() {&lt;br /&gt;
        for (int i = 0; i &amp;lt; s1.length; i++) {&lt;br /&gt;
            m.Question(s1[i]);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class T2 implements Runnable {&lt;br /&gt;
    Chat m;&lt;br /&gt;
    String[] s2 = { &amp;quot;Hi&amp;quot;, &amp;quot;I am good, what about you?&amp;quot;, &amp;quot;Great!&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
    public T2(Chat m2) {&lt;br /&gt;
        this.m = m2;&lt;br /&gt;
        new Thread(this, &amp;quot;Answer&amp;quot;).start();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void run() {&lt;br /&gt;
        for (int i = 0; i &amp;lt; s2.length; i++) {&lt;br /&gt;
            m.Answer(s2[i]);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class TestThread {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Chat m = new Chat();&lt;br /&gt;
        new T1(m);&lt;br /&gt;
        new T2(m);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
When above program is complied and executed, it produces following result:&lt;br /&gt;
&lt;br /&gt;
 Hi&lt;br /&gt;
 Hi&lt;br /&gt;
 How are you ?&lt;br /&gt;
 I am good, what about you?&lt;br /&gt;
 I am also doing fine!&lt;br /&gt;
 Great!&lt;/div&gt;</summary>
		<author><name>George2</name></author>	</entry>

	</feed>