簡體   English   中英

暫停執行線程,並根據用戶輸入繼續執行

[英]Pause Thread execution, and resume based on user input

我有一個線程會推動JNativeHook.jar的創建一個全局鍵盤和鼠標偵聽器。 根據用戶輸入,線程可以這樣操作。

我想在用戶(例如)按下VK_SPACE時停止所有線程執行。 我想生成另一個也監視鍵盤的線程,當它再次獲得VK_SPACE時,它將告訴主線程恢復。

Java中是否可以執行這種操作,它的外觀如何?

這是和JNativeHook .jar一起使用的一些代碼

碼:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListenerExample implements NativeKeyListener 
{
public void nativeKeyPressed(NativeKeyEvent e) 
{
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

    if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) 
    {
        GlobalScreen.unregisterNativeHook();
    }
    if (e.getKeyCode() == NativeKeyEvent.VK_SPACE) 
    {
            WatchForMe w = new WatchForMe(this);
            w.start();
            this.wait();
    }
}

public void nativeKeyReleased(NativeKeyEvent e){}
public void nativeKeyTyped(NativeKeyEvent e){}

public static void main(String[] args) 
{
    try 
    {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex) 
    {
        System.err.println("There was a problem registering the native hook.");
        System.exit(1);
    }

    //Construct the example object and initialze native hook.
    GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
}
}

public class WatchForMe implements NativeKeyListener 
{
boolean alive = true;
Thread me = null;
public WatchForMe(Thread me)
{
    if(me == null) return;
    this.me = me;
    GlobalScreen.getInstance().addNativeKeyListener(this);
}
public void nativeKeyPressed(NativeKeyEvent e) 
{
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
        if (e.getKeyCode() == NativeKeyEvent.VK_SPACE) 
        {
                me.notify(); 
                alive = false;
        }
}
 public void run()
 {
     while(alive){}
 }

public void nativeKeyReleased(NativeKeyEvent e){}
public void nativeKeyTyped(NativeKeyEvent e){}
}

我個人不是在嘗試鎖定不同的監視器和其他東西,而是在GlobalKeyListenerExample中放置一個標志,該標志將在檢測到某個特定的組合鍵時開始忽略更多傳入的鍵,直到引發特定的組合鍵為止。

您當前面臨的問題是知道1-您正在使用哪個線程以及該線程可能具有“暫停”的作用,並且2-知道何時會出現“取消暫停”組合...因為您不再在聽關鍵事件(因為您可能無意中停止了本機接口用來引發事件的線程...)

public void nativeKeyPressed(NativeKeyEvent e) 
{
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

    if (isPaused) {
        if (e.getKeyCode == UNPAUSE_KEY_CODE) { 
            isPaused = false;
        }
    } else {
        if (e.getKeyCode == PAUSE_KEY_CODE) { 
            isPaused = true;
        } else {
            // Handle other key strokes...
        }
    }
}

更新

恰如其分...

public void run()
{
    while(alive){}
}

這不是一個好主意。 基本上,它使線程處於“運行”狀態,不必要地消耗了CPU周期。 就您而言,我不確定您實際上將如何進行糾正...

在方法運行中:

public void run(){
 while(me!=null) //execute forever
   try{
      Thread.sleep(5);
      if(!alive) continue; //define pause

      ... do somthing   
   catch(Exception e){}

我們需要一直“保持線程”狀態,以檢查是否存在“暫停”狀態。

重要! 建議使用public synchronized void setAlive (boolean value)來避免“線程”沖突

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM