暂停执行线程,并根据用户输入继续执行 [英] Pause Thread execution, and resume based on user input

查看:97
本文介绍了暂停执行线程,并根据用户输入继续执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程会推动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周期.对于您而言,我不确定您实际上将如何进行纠正...

I have a thread which impelments JNativeHook.jar which creates a global keybourd and mouse listener. Based on user input the thread can act as such.

I would like to stop all Thread execution when a user (let's say) presses VK_SPACE. I would want to spawn another Thread which also watches the keyboard, and when it gets a VK_SPACE again it will tell the main Thread to resume.

Is this sort of action possible in Java, and how might it look?

Here is some code to work with and JNativeHook.jar

CODE:

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){}
}

解决方案

Instead of running around trying to lock on different monitors and stuff, I would, personally, place a flag in GlobalKeyListenerExample that would, when a certain key combination is detected, start ignoring any more incoming keys until a certain key combination was raised.

The problem you face currently is knowing 1- What thread you are in and what effect "pausing" it might have and 2- Knowing when the "un-pause" combination is raised...cause you're no longer listening to key events (because you may have inadvertently stopped the thread that the native interface was using to raise the events with...)

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...
        }
    }
}

Updated

Just as a side note...this

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

Isn't a good idea. Basically, it leaves the thread in "running" state, consuming CPU cycles unnecessarily. In your case, I'm not sure how you would actually be able to correct for it...

这篇关于暂停执行线程,并根据用户输入继续执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆