2

我有一个单例类,可以将客户端(小程序)添加到/从链接列表中删除,如下所示:

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();
    }

问题:

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

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

4

1 回答 1

1

添加2个锁:

一种)

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

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

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

b)

机器人列表也是如此:

    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()
        }
    }
    }
于 2013-05-06T19:08:52.680 回答