为了账号安全,请及时绑定邮箱和手机立即绑定

线程返回和同步

线程返回和同步

手掌心 2022-06-08 17:36:33
我正在制作纸牌游戏应用程序。我有 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}
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

首先你需要了解wait()是Object类的方法,所以它就像一个特定的线程在等待一些与Object相关的动作。因此,如果在 proHs 线程下调用 bidStep (int gt)并且您想停止 proHs 线程,基本上要等到选择特定按钮然后您需要将等待放在某个对象上,通常它应该是 Object需要采取一些行动。您需要在此处列出以下步骤:

  1. proHs 对象引用。

  2. 锁定 proHs 对象。

  3. 调用 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() 之前调用,这样一旦线程等待,另一个线程就会通知它。


查看完整回答
反对 回复 2022-06-08
  • 1 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信