gpt4 book ai didi

java - 我无法停止 java SwingWorker 进程

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

我在 java 中运行一个 tcp 服务器,它是使用 SwingWorker 类完成的。第一次它将成功启动,但是当我停止并启动该过程时,它将无法正常工作。我找不到问题所在,请帮我解决这个问题

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SwingWorkerDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;

public SwingWorkerDemo() {
initialize();
}

private void initialize() {
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton startButton = new JButton("Start");
final JButton stopButton = new JButton("Stop");
final LongRunProcess process = new LongRunProcess();



startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
process.execute();
} catch (Exception e) {
e.printStackTrace();
}
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
});

stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JOptionPane.showMessageDialog(null, "Hello There");

process.cancel(true);
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
});


this.getContentPane().add(startButton);
this.getContentPane().add(stopButton);

this.pack();
this.setSize(new Dimension(300, 80));
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingWorkerDemo().setVisible(true);
}
});
}

class LongRunProcess extends SwingWorker {
/**
* @throws Exception
*/
protected Object doInBackground() throws Exception {
try {
serverSocket = new ServerSocket(4545); //Server socket

} catch (IOException e) {
System.out.println("Could not listen on port: 4545");
}

System.out.println("Server started. Listening to the port 4545");

while (true) {
try {

clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();
if(message.equals("shutdown")){

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 00");
System.exit(0);

}
else if(message.equals("restart")){
Runtime runtime1 = Runtime.getRuntime();
Process proc2 = runtime1.exec("shutdown -r -t 00");
System.exit(0);


}

System.out.println(message);
inputStreamReader.close();
clientSocket.close();

} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}

}
}
}

最佳答案

根据 SwingWorker#execute() 的 Javadocs

Note: SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice.

因此您看到的行为。所以为了实现你正在寻找的东西。每次按下开始按钮时,您都需要创建 LongRunProcess 的新实例。像这样的东西。

在类级别声明

LongRunProcess process = null;

修改 Action 监听器

 startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
process = new LongRunProcess();
process.execute();
} catch (Exception e) {
e.printStackTrace();
}
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
});

希望这对您有所帮助。

关于java - 我无法停止 java SwingWorker 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880467/

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