gpt4 book ai didi

java - 如何同步线程

转载 作者:行者123 更新时间:2023-11-30 11:32:47 24 4
gpt4 key购买 nike

我有一个单例类,可以像这样在链表中添加和删除客户端(小程序):

public class ClientManager {
//Collections.unmodifiableList
private static ClientManager instance = new ClientManager();
private static LinkedList<Bot> Clients = new LinkedList<>();

//private constructor here..

public static Bot add(final Bot bot) {
Clients.add(bot);
new Thread(new Runnable() {
@Override
public void run() {
while (bot.getApplet() == null) {
Utilities.sleep(10);
}
}

}).start();
return bot;
}

public static Bot remove(int hashCode) {
for (Iterator<Bot> it = Clients.iterator(); it.hasNext();) {
Bot bot = it.next();
if (bot.getCanvas().getClass().getClassLoader().hashCode() == hashCode) {
it.remove();
return bot;
}
}
return null;
}

public static Bot getBot(int hashCode) {
for (Bot bot : Clients) {
if (bot.getCanvas() != null && (bot.getCanvas().getClass().getClassLoader().hashCode() == hashCode)) {
return bot;
}
}
return null;
}
}

然后我有一个带有 JTabbedPane 的 JFrame,它在每个带有 actionListener 的选项卡上都有一个退出按钮:

CloseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Frame.this.removeBot((Loader) component);
}
});

//Need to synchronize this function some how so that when I run it in a new Thread, they don't all access the LinkedList at the same time..

private void removeBot(final Loader loader) {
Frame.this.TabPanel.remove(loader); //Need to synchronize this or tabbedpane throws.. This call removes a tab..

List<Bot> Clients = ClientManager.getBots();

for (Iterator<Bot> it = Clients.iterator(); it.hasNext();) {
Bot bot = it.next();
if (bot != null && bot.getLoader() != null && bot.getLoader() == loader) {
it.remove();
loader.destruct(); //Destroy my applet. Calls applet.stop() and then applet.destroy()
}
}
System.gc();
}

问题:

我如何同步 Tabs 的删除,以便在多个线程尝试删除选项卡时 TabbedPane 不会抛出,以及如何同步我的删除功能,以便多个线程不会同时从 LinkedList 中删除,因为如果我在新线程中运行上述任何一个,它会抛出错误。

我尝试查看教程并将同步放在我的函数之前,但这没有帮助。

最佳答案

加2把锁:

一)

    synchronized( Frame.this.TabPanel ) {
... remove etc until the end of the method
}

将此也添加到向 TabPanel 添加内容的代码中:

    synchronized( Frame.this.TabPanel ) {
...add the loader here...
}

二)

机器人列表也是如此:

    synchronized( Clients ) {
Clients.add(bot);
new Thread(new Runnable() {
@Override

i.e. the contents of the add method
}

remove方法开头也是一样

public static Bot remove(int hashCode) {
synchronized( Clients ) {
for (Iterator<Bot> it = Clients.iterator(); it.hasNext();) {
Bot bot = it.next();
if (bot.getCanvas().getClass().getClassLoader().hashCode() == hashCode) {
it.remove();
return bot;
}
}
return null;
}// synchronized
}

然后在 removeBot 中!

    synchronized( Clients ) {
for (Iterator<Bot> it = Clients.iterator(); it.hasNext();) {
Bot bot = it.next();
if (bot != null && bot.getLoader() != null && bot.getLoader() == loader) {
it.remove();
loader.destruct(); //Destroy my applet. Calls applet.stop() and then applet.destroy()
}
}
}

关于java - 如何同步线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16405227/

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