gpt4 book ai didi

java - 线程访问全局变量并强制终止旧线程

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

一切都在具有给定字段的 GUIActions 类中:

public class GUIActions {
private GUIActions GUI;
private JButton connectButton;
private JTextArea messageArea;

private String IP;
private Integer PORT;
private CommunicationManager comManager;

public static Boolean flag = false;

我有处理连接管理的代码:它启动 comManager 并将标志更改为 true,以便 ThreadManager 知道它必须启动新的 ReceiverThread

class connectButtonActionListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {
try {
comManager = null; //to make sure that old connection is erased
comManager = new CommunicationManager(IP, PORT);
flag = true;
if(!comManager.isAlive()) {
JOptionPane.showMessageDialog(null, "Can't connect with server \n Server is either unreachable or offline");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Can't connect with server \n Server is either unreachable or offline");
}
}
}

线程代码:

/*
* @desc Static method that setup GUI including adding tabbed cards into Frame
* @param -none-
* @return -none-
*/
public void setupGUI() {
JFrame frame = new JFrame("Chat Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GUI = new GUIActions();
GUI.setupCards(frame.getContentPane());

Thread threadManager = new Thread(new ThreadManager());
threadManager.start();

frame.pack();
frame.setVisible(true);
}

class ThreadManager implements Runnable {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Looking for connection!");
if(flag == true) {
System.out.println("Connection found!");
Thread receiverThread = new Thread(new MessageReceiverRunnable());
receiverThread.start();
flag = false;
}
}
}
}

class MessageReceiverRunnable implements Runnable {
public synchronized void run() {
System.out.println("Thread running");
BufferedReader reader;
String message;
try {
if(GUI.comManager==null) {
System.out.println("comManager null");
if(GUI.IP == null || GUI.PORT == null) {
System.out.println("IP or PORT null");
}
comManager = new CommunicationManager(GUI.IP, GUI.PORT);
reader = comManager.getReader();
} else {
System.out.println("comManager NOT null");
reader = GUI.comManager.getReader();
}
while((message = reader.readLine()) != null) {
GUI.messageArea.append(message + "\n");
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}

因此 ThreadManager 等待连接 (comManager),如果有连接,它会启动新线程,等待传入消息。我有两个问题:

1)为什么我必须使用 GUI.IP 或 GUI.comManager 而不是 IP 或 comManager 访问 GUIActions 字段?

可运行线程类在 GUIActions 类中!是否与每个线程都有单独的堆栈有关?

2)当有新的receiverThread初始化时,如何强制杀死旧的receiverThread?

干杯!

最佳答案

  1. 如果不使用包含它们的对象,您将无法访问类成员变量。这是大多数语言的规则,因为它们不是全局常量,JVM(在这种情况下)不知道您指的是哪些变量。

您能否澄清“可运行线程类在 GUIActions 类中!它与每个线程都有单独的堆栈有关吗?”这个问题? ?

  1. 要终止旧的接收器线程,请将 MessageReceiverRunnable 对象存储在一个变量中并添加一个将退出 run() 方法的标志。创建新的接收器线程时,修改标志以使线程完成。另一种选择是在线程上调用 interrupt() 方法。

关于java - 线程访问全局变量并强制终止旧线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456271/

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