繁体   English   中英

如何同步线程

[英]How to synchronize Thread

我有一个单例类,它可以向/从链表中添加和删除客户端(小应用程序),如下所示:

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中删除,因为它会抛出错误如果我在新线程中运行以上任何内容。

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

添加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
    }

删除方法开始时相同

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM