Three threads.T1 print 1,4,7.. messages sequence T2 print 2,5,8.. and T3 print 3,6,9.. How do I synchronize these three to print 1-15 message sequence

huangapple go评论71阅读模式
英文:

Three threads.T1 print 1,4,7.. messages sequence T2 print 2,5,8.. and T3 print 3,6,9.. How do I synchronize these three to print 1-15 message sequence

问题

public class Chat {
    int flag = 0;

    public synchronized void Friend1(String msg) {
        if (flag > 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(msg);
        flag = 1;
        notify();
    }

    public synchronized void Friend2(String msg) {
        if (flag == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(msg);
        flag = 0;
        notify();
    }

    public synchronized void Friend3(String msg) {
        if (flag < 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(msg);
        flag = -1;
        notify();
    }
}

public class T1 implements Runnable {
    Chat m;
    String[] s1 = {"Hi", "How are you all?", "Why what happened", "Yes, We are in a hectic situation but we have to continue our studies and be strong inside", "Fave faith in Allah! Eveything will be ok"};

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

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

public class T2 implements Runnable {
    Chat m;
    String[] s2 = {"Hy", "I am fine", "Is there anything wrong?", "There is nothing we can do about in this pandemic situation but to study and pray", "Everything will be fine soon"};

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

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

public class T3 implements Runnable {
    Chat m;
    String[] s3 = {"Hello", "I am not fine", "I am very depressed about my online classes", "I feel too much preassure", "Yeap I will start praying to Allah that everything comes around"};

    public T3(Chat m3) {
        this.m = m3;
        new Thread(this, "Friends3").start();
    }

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

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

输出结果:
Hi

Hy

How are you all?

I am fine

Why what happened

Is there anything wrong?

Yes, We are in a hectic situation but we have to continue our studies and be strong inside

There is nothing we can do about in this pandemic situation but to study and pray

Fave faith in Allah! Eveything will be ok

Everything will be fine soon

Hello

英文:
public class Chat {
int flag=0;
public synchronized void Friend1(String msg) {
if(flag&gt;0) {
try {
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag=1;
notify();
}
public synchronized void Friend2(String msg) {
if(flag==0) {
try {
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag=0;
notify();
}
public synchronized void Friend3(String msg) {
if(flag&lt;0) {
try {
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag=(-1);
notify();
}
}
public class T1 implements Runnable {
Chat m;
String[] s1= {&quot;Hi&quot;,&quot;How are you all?&quot;,&quot;Why what happened&quot;,&quot;Yes, We are in a hectic situation but we have to continue our studies and be strong inside&quot;,&quot;Fave faith in Allah! Eveything will be ok&quot;};
public T1(Chat m1) {
this.m=m1;
new Thread(this, &quot;Friend1&quot;).start();
}
public void run() {
for(int i=0; i&lt;s1.length; i++) {
m.Friend1(s1[i]);
}
}
}
public class T2 implements Runnable{
Chat m;
String[] s2= {&quot;Hy&quot;,&quot;I am fine&quot;,&quot;Is there anything wrong?&quot;,&quot;There is nothing we can do about in this pandemic situation but to study and pray&quot;,&quot;Everything will be fine soon&quot;};
public T2(Chat m2) {
this.m=m2;
new Thread(this, &quot;Answer&quot;).start();
}
public void run() {
for(int i=0; i&lt;s2.length; i++) {
m.Friend2(s2[i]);
}
}
}
public class T3 implements Runnable {
Chat m;
String[] s3= {&quot;Hello&quot;,&quot;I am not fine&quot;,&quot;I am very depressed about my online classes&quot;,&quot;I feel too much preassure&quot;,&quot;Yeap I will start praying to Allah that everything comes around&quot;};
public T3(Chat m3) {
this.m=m3;
new Thread(this, &quot;Friends3&quot;).start();
}
public void run() {
for(int i=0; i&lt;s3.length; i++) {
m.Friend3(s3[i]);
}
}
}
public class Main {
public static void main(String[] args) {
Chat m=new Chat();
new T1(m);
new T2(m);
new T3(m);
}
}

Output is:
Hi

Hy

How are you all?

I am fine

Why what happened

Is there anything wrong?

Yes, We are in a hectic situation but we have to continue our studies and be strong inside

There is nothing we can do about in this pandemic situation but to study and pray

Fave faith in Allah! Eveything will be ok

Everything will be fine soon

Hello

答案1

得分: 1

在您的情况下,您知道每个线程都应该在前一个线程之后打印短语:

  • 在 1 之后是 2
  • 在 2 之后是 3
  • 在 3 之后是 1

因此,您可以创建一个变量,用于存储当前应该执行动作的线程编号:

AtomicInteger currentSpeaker = new AtomicInteger(1);

每个线程在打印下一个短语之前可以检查此变量,并为下一个线程设置新的值:

public class T1 implements Runnable {
    // ... 线程内部的其他代码 ...
    public void run() {
        for (int i = 0; i < s1.length; i++) {
            while (currentSpeaker.get() != 1) delay(100L);
            m.Friend1(s1[i]);
            currentSpeaker.set(2);
        }
    }
}

public class T2 implements Runnable {
    // ... 线程内部的其他代码 ...
    public void run() {
        for (int i = 0; i < s2.length; i++) {
            while (currentSpeaker.get() != 2) delay(100L);
            m.Friend2(s2[i]);
            currentSpeaker.set(3);
        }
    }
}

public class T3 implements Runnable {
    // ... 线程内部的其他代码 ...
    public void run() {
        for (int i = 0; i < s3.length; i++) {
            while (currentSpeaker.get() != 3) delay(100L);
            m.Friend3(s3[i]);
            currentSpeaker.set(1);
        }
    }
}

我为了可读性添加了方法 delay,用于稍微清晰地阅读代码:

public static void delay(long t) {
    try {
        Thread.sleep(t);
    } catch (InterruptedException e) {}
}

输出结果:

> Hi
> 
> Hy
> 
> Hello
> 
> How are you all?
> 
> I am fine
> 
> I am not fine
> 
> Why what happened
> 
> Is there anything wrong?
> 
> I am very depressed about my online classes
> 
> Yes, We are in a hectic situation but we have to continue our studies
> and be  strong inside
> 
> There is nothing we can do about in this pandemic situation but to
> study and pray
> 
> I feel too much preassure
> 
> Fave faith in Allah! Eveything will be ok
> 
> Everything will be fine soon
> 
> Yeap I will start praying to Allah that everything comes around
英文:

In your case you know that every thread should print phrase after previous:

  • 2 after 1
  • 3 after 2
  • 1 after 3

So you can create variable that will be store number of the thread that shouldmake action now:

AtomicInteger currentSpeaker = new AtomicInteger(1);

Every thread can check this var before printing next phrase and set new value for next thread:

public class T1 implements Runnable {
Chat m;
String[] s1 = {&quot;Hi&quot;, &quot;How are you all?&quot;, &quot;Why what happened&quot;, &quot;Yes, We are in a hectic situation but we have to continue our studies and be strong inside&quot;, &quot;Fave faith in Allah! Eveything will be ok&quot;};
public T1(Chat m1) {
this.m = m1;
new Thread(this, &quot;Friend1&quot;).start();
}
public void run() {
for (int i = 0; i &lt; s1.length; i++) {
while (currentSpeaker.get() != 1) delay(100L);
m.Friend1(s1[i]);
currentSpeaker.set(2);
}
}
}
public class T2 implements Runnable {
Chat m;
String[] s2 = {&quot;Hy&quot;, &quot;I am fine&quot;, &quot;Is there anything wrong?&quot;, &quot;There is nothing we can do about in this pandemic situation but to study and pray&quot;, &quot;Everything will be fine soon&quot;};
public T2(Chat m2) {
this.m = m2;
new Thread(this, &quot;Answer&quot;).start();
}
public void run() {
for (int i = 0; i &lt; s2.length; i++) {
while (currentSpeaker.get() != 2) delay(100L);
m.Friend2(s2[i]);
currentSpeaker.set(3);
}
}
}
public class T3 implements Runnable {
Chat m;
String[] s3 = {&quot;Hello&quot;, &quot;I am not fine&quot;, &quot;I am very depressed about my online classes&quot;, &quot;I feel too much preassure&quot;, &quot;Yeap I will start praying to Allah that everything comes around&quot;};
public T3(Chat m3) {
this.m = m3;
new Thread(this, &quot;Friends3&quot;).start();
}
public void run() {
for (int i = 0; i &lt; s3.length; i++) {
while (currentSpeaker.get() != 3) delay(100L);
m.Friend3(s3[i]);
currentSpeaker.set(1);
}
}
}

I created method delay to make code little clear for reading:

public static void delay(long t) {
try {
Thread.sleep(t);
} catch (InterruptedException e) {}
}

Output:

> Hi
>
> Hy
>
> Hello
>
> How are you all?
>
> I am fine
>
> I am not fine
>
> Why what happened
>
> Is there anything wrong?
>
> I am very depressed about my online classes
>
> Yes, We are in a hectic situation but we have to continue our studies
> and be strong inside
>
> There is nothing we can do about in this pandemic situation but to
> study and pray
>
> I feel too much preassure
>
> Fave faith in Allah! Eveything will be ok
>
> Everything will be fine soon
>
> Yeap I will start praying to Allah that everything comes around

huangapple
  • 本文由 发表于 2020年9月25日 21:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64064753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定