线程返回和同步

我正在制作纸牌游戏应用程序。我有 2 个线程:

线程curr = 这里保存当前线程(JavaFx 线程)

线程proHs = 这是应用程序的大脑,它通过接口运行方法

我想停止线程proHs停止一秒钟,直到我选择这两个按钮中的一个mam nemam

然后我必须返回true或false。

我感谢任何建议或建议。谢谢!


我试过无限循环


public boolean biddingStep(int gt) { //above this method is @Override, I can't post this with it

    System.out.println("  ");

    System.out.println("I HAVE OR NOT PART");

    try {

        proHs.wait();

    }

    catch (Exception e) {

        System.out.print(e);

    }

    panelLicitace.setVisible(true);

    mam.setVisible(true);

    nemam.setVisible(true);

    return false;//there would be the resolution of button "mam" or "nemam"

}


编辑#1

我想从你这里得到什么:


public boolean biddingStep(int gt) { //above this method is @Override, I can't post this with it

    System.out.println("  ");

    System.out.println("I HAVE OR NOT PART");

    panelLicitace.setVisible(true);

    mam.setVisible(true);

    nemam.setVisible(true);

    // HERE a code i want

    //1. stop proHS thread

    //2. loop program, wait for input from 2 buttons

    //3. return true or false

}


手掌心
浏览 110回答 1
1回答

陪伴而非守候

首先你需要了解wait()是Object类的方法,所以它就像一个特定的线程在等待一些与Object相关的动作。因此,如果在 proHs 线程下调用 bidStep (int gt)并且您想停止 proHs 线程,基本上要等到选择特定按钮然后您需要将等待放在某个对象上,通常它应该是 Object需要采取一些行动。您需要在此处列出以下步骤:proHs 对象引用。锁定 proHs 对象。调用 proHs.wait()。从第二个线程您将执行以下操作: 1. 在 buttonClickListener 第二个线程内锁定 proHs 对象。) 2. 调用 proHs.notify()。class InterfaceImpl {    Thread proHs;    boolean btnResponse;    public boolean biddingStep(int gt) {        System.out.println("  ");        System.out.println("I HAVE OR NOT PART");        panelLicitace.setVisible(true);        mam.setVisible(true);        nemam.setVisible(true);        // HERE a code i want        //1. stop proHS thread        synchronized(proHs) {            proHs.wait();            //2. loop program, wait for input from 2 buttons            //3. return true or false            return btnResponse;        }    }       // This method should be called from another thread    public boolean btnClickListener() {         btnResponse = true or false        synchronized(proHs) {            proHs.notify();        }      }}这里 bidStep() 方法应该在 btnClickListener() 之前调用,这样一旦线程等待,另一个线程就会通知它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java