亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

線程返回和同步

線程返回和同步

手掌心 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 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號