三个线程。T1打印1,4,7 ..消息序列T2打印2,5,8 ..和T3打印3,6,9 ..如何同步这三个以打印1-15消息序列

莫西尔·拉赫曼·费萨尔
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);
   }
   }

输出为:嗨

你们好吗?

我很好

为什么发生了什么

有什么不对的吗?

是的,我们处在忙碌的状况中,但我们必须继续学习并保持坚强的内心

在这种大流行情况下,除了学习和祈祷外,我们无能为力

信仰阿拉!一切都会好的

一切都会很快好起来的

你好

ekiryuhin

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

  • 1后2
  • 2后3
  • 1后3

因此,您可以创建一个变量,该变量将是应该立即执行操作的线程的存储号:

AtomicInteger currentSpeaker = new AtomicInteger(1);

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

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++) {
            while (currentSpeaker.get() != 1) delay(100L);
            m.Friend1(s1[i]);
            currentSpeaker.set(2);
        }
    }
}


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++) {
            while (currentSpeaker.get() != 2) delay(100L);
            m.Friend2(s2[i]);
            currentSpeaker.set(3);
        }
    }
}

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++) {
            while (currentSpeaker.get() != 3) delay(100L);
            m.Friend3(s3[i]);
            currentSpeaker.set(1);
        }
    }
}

我创建了方法延迟,以使代码更易于阅读:

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

输出:

你好

你好

你们好吗?

我很好

我不好

为什么发生了什么

有什么不对的吗?

我对在线课程感到非常沮丧

是的,我们处在忙碌的状况中,但我们必须继续学习并保持坚强的内心

在这种大流行情况下,除了学习和祈祷外,我们无能为力

我感到很放心

信仰阿拉!一切都会好的

一切都会很快好起来的

是的,我将开始向真主祈祷一切都会发生

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档