gpt4 book ai didi

java - 无法让 Thread.interrupt 工作

转载 作者:行者123 更新时间:2023-11-30 10:20:30 25 4
gpt4 key购买 nike

我有一个程序执行一些文件 I/O,然后 hibernate 几秒钟,检查文件是否已更改,如果相同则 hibernate 。如果它们已更改,它会执行相同的文件 I/O,然后返回 hibernate 状态。我想实现一个击键,如果按下它会唤醒这个线程,但我无法让线程处理 InterruptedException。我的(简化的)代码在这里:

import java.awt.Color;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class StackOverFlowExample extends JFrame{

private class MyDispatcher implements KeyEventDispatcher {
boolean keyEventDone = false;

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID() == KeyEvent.KEY_PRESSED){
if((e.getKeyCode() == 80 && e.getModifiers() == 11) && !keyEventDone){
System.out.println("interrupting");
interrupt();
}
}
if (e.getID() == KeyEvent.KEY_RELEASED) {
if(e.getKeyCode() == 80){
keyEventDone = false;
}
}
return true;
}

}

public void interrupt(){
System.out.println("recieved interrupt");
Thread.currentThread().interrupt();
}

public static void main(String[] args){
String fileName = "file Name";
String oldFileName = "oldFileName";
JFrame frame = new StackOverFlowExample();
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(217, 104);

do{
if(!fileName.equals(oldFileName)){

}else{
try {
System.out.println("Same file");
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("continuing");
if(Thread.currentThread().isInterrupted()){
System.out.println("continuing");
continue;
}
e.printStackTrace();
}
}
oldFileName = fileName;
}while(true);

}
public StackOverFlowExample(){
super("");
JPanel pane = new JPanel();
pane.setBackground(Color.LIGHT_GRAY);
setContentPane(pane);
KeyboardFocusManager manager =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
}

}

类(class)中还有其他内容,但都与线程或任何内容无关。此外,我知道检查文件名不是执行此操作的最佳方法,但在我的应用程序中,如果文件不同,文件名将始终不同,因为文件名是根据时间生成的。

当我按“ctrl+shift+alt+p”唤醒线程时,它从 MyDispatcher 类打印出“interrupting”,从主类的中断方法打印出“recieved interrupt”,但没有别的,线程只是继续 sleep

我见过的所有中断 hibernate 线程的例子都有不在主类中的线程。这就是我的不起作用的原因吗?

最佳答案

同样,您打断了错误的线程,并希望传递正确的线程。例如,获取您的代码:

import java.awt.Color;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class StackOverFlowExample extends JFrame {
static String oldFileName = "oldFileName";
private Thread runningThread;

private class MyDispatcher implements KeyEventDispatcher {
boolean keyEventDone = false;

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if ((e.getKeyCode() == 80 && e.getModifiers() == 11) && !keyEventDone) {
System.out.println("interrupting");
interrupt();
}
}
if (e.getID() == KeyEvent.KEY_RELEASED) {
if (e.getKeyCode() == 80) {
keyEventDone = false;
}
}
return true;
}

}

// pass in the correct thread to interrupt
public void setRunningThread(Thread runningThread) {
this.runningThread = runningThread;
}

public void interrupt() {
System.out.println("recieved interrupt");
// !! Thread.currentThread().interrupt();
runningThread.interrupt();
System.out.println("interrupt Thread id: " + Thread.currentThread().getId());
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
String fileName = "file Name";
StackOverFlowExample frame = new StackOverFlowExample();
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(217, 104);

Thread thread = new Thread(() -> {
do {
if (!fileName.equals(oldFileName)) {

} else {
try {
System.out.println("do-while Thread id: " + Thread.currentThread().getId());

System.out.println("Same file");
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("continuing");
if (Thread.currentThread().isInterrupted()) {
System.out.println("continuing");
continue;
}
e.printStackTrace();
}
}
oldFileName = fileName;
} while (true);
});

// pass in the thread
frame.setRunningThread(thread);
thread.start(); // start it running
});
}

public StackOverFlowExample() {
super("");
JPanel pane = new JPanel();
pane.setBackground(Color.LIGHT_GRAY);
setContentPane(pane);
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
}

}


您知道,也许有一种更简单的方法来完成您想要做的事情:

  • 创建一个每 x 秒重复一次的 Swing 计时器(我们将在本例中使用 10 秒)
  • 在计时器内启动一个新线程,如果我们需要它更直接地与 GUI 交互,可能是一个 SwingWorker,它会检查文件情况并在需要时执行文件 I/O。
  • 为计时器设置初始延迟 0,以便它立即启动或重新启动
  • 使用键绑定(bind)而不是烦躁的 KeyListener
  • 在由我们的绑定(bind)触发的操作中,只需调用 Swing 定时器的 restart(),它立即重新启动它的操作,因为初始延迟为 0,然后重新开始 10 秒的等待。

类似于:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class StackOverflowExample2 extends JPanel {
// 10 seconds between tasks
private static final int TIMER_DELAY = 10 * 1000;
int keyCode = KeyEvent.VK_P;
int modifiers = InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK
| InputEvent.ALT_DOWN_MASK;
private KeyStroke shftCtrlAltP = KeyStroke.getKeyStroke(keyCode, modifiers);
private int condition = WHEN_IN_FOCUSED_WINDOW;
private InputMap inputMap = getInputMap(condition);
private ActionMap actionMap = getActionMap();
private Timer myTimer = new Timer(TIMER_DELAY, new TimerListener());

public StackOverflowExample2() {
setPreferredSize(new Dimension(400, 300));

// create key binding
inputMap.put(shftCtrlAltP, shftCtrlAltP.toString());
actionMap.put(shftCtrlAltP.toString(), new MyAction());
// myWorker.execute();
myTimer.setInitialDelay(0);
myTimer.start();
}

private class MyAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Key pressed");
myTimer.restart(); // simply re-start the timer
}
}

private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// launch SwingWorker or other thread to check if files have changed
// and if so do file I/O
System.out.println("Timer running");
}
}

private static void createAndShowGui() {
StackOverflowExample2 mainPanel = new StackOverflowExample2();

JFrame frame = new JFrame("StackOverflowExample2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

此外,您的代码和程序设计当前要求轮询一个文件或多个文件的状态,然后在它们发生变化时执行某些操作。一种更有效的替代方法是根本不轮询,而是使用 Java NIO WatchService API ,自 Java 7 以来的核心 Java API 的一部分。

关于java - 无法让 Thread.interrupt 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48178791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com