首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >简单乒乓Java线程

简单乒乓Java线程
EN

Stack Overflow用户
提问于 2015-10-20 21:38:51
回答 3查看 1.9K关注 0票数 1

我正在尝试做两个线程,一个Ping和一个Pong。这个想法是Ping应该总是首先执行。我使用的是同步方法。我不确定我的代码在这里出了什么问题。在我看来,它应该是可行的。我已经阅读了很多文档。所以,如果你有任何你认为会有帮助的东西,我很乐意读一读。我确定这是很简单的事情。任何帮助我们都将不胜感激

代码语言:javascript
复制
class Ping extends Thread {
   private Table table;
   private String text1 = "";

   public Ping(Table t)
   {
       table = t;
   }
   public void run() {

       for (int i = 0; i < 10; i++) {
           text1= table.getPing();
           System.out.println(text1);
   }
 }
}


class Pong extends Thread {
   private Table table;
   private String text1 = "";

   public Pong(Table t)
   {
       table = t;
   }
   public void run() {
       for (int i = 0; i < 10; i++) {
           text1= table.getPong();
           System.out.println(text1);   }
 }
}

class Table extends Thread {
    private Table table;
    private boolean pinged = false;

    public synchronized String getPong() {
        while (pinged == false) {
            try {
                //System.out.println("WAIT PONG");
                wait();
            } 
            catch (InterruptedException e) 
            { }
        }        
        pinged = false;

        notifyAll();
        String text = "pong";
        return text;
    }

    public synchronized String getPing() {
        while (pinged == true) {
            try {
                wait();
                //System.out.println("WAIT PING");
            } catch (InterruptedException e) { }
        }    
        pinged = true;
        notifyAll();
        String text = "ping";
        return text;
    }
}


public class PingPong {

    //private static final int WAIT_TIME = 200;

    public static void main(String args[]) {

        Table t = new Table();

        Pong pong = new Pong(t);

        Ping ping = new Ping(t);

        System.out.println("main: starting threads...");

        pong.start();
        ping.start();

        System.out.println("main: threads started, sleep a while " +
                           "and wait for termination of Ping and Pong"); 


        System.out.println("both threads terminated");
   }

}

每个结果都是不同的,但奇怪的是我得到了重复的结果。

代码语言:javascript
复制
ping
pong
ping
ping
pong
pong
pong
ping
ping
ping
pong
pong
pong
ping
pong
ping
ping
pong
ping
pong
EN

Stack Overflow用户

发布于 2015-10-21 00:22:45

Table类中的同步-顺便说一下,它不需要扩展Thread -只保证Ping和Pong线程以交替的方式获得它们的字符串。它不能保证它们以交替的方式打印字符串。

例如,可能会出现以下序列:

代码语言:javascript
复制
Ping gets its first ping, call it ping 1.
Ping prints ping 1.
Pong gets its first pong, call it pong 1.
Ping gets ping 2.
Ping prints ping 2.
Pong prints pong 1.
Pong gets pong 2.
Pong prints pong 2.
Ping gets ping 3.
Pong gets pong 3.
Pong prints pong 3.
Ping prints ping 3.

请注意,每个线程在获取字符串和打印字符串之间交替,并且这两个线程以交替的顺序获取字符串。但是,在一个线程获取字符串和打印字符串之间,另一个线程可能会也可能不会获得时间。这会导致交替序列被分解以进行打印,在我们的示例中,输出为:

代码语言:javascript
复制
ping
ping
pong
pong
pong
ping

如果你想解决这个问题,你需要在同一个synchronized块中包含获取字符串和打印字符串,并且你可能需要在System.out和Table上同步该块。

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33238225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档