Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在WEB应用中实现线程停止,线程终止登记表是否会内存溢出? #6

Open
chunjie008 opened this issue Sep 27, 2018 · 0 comments

Comments

@chunjie008
Copy link

chunjie008 commented Sep 27, 2018

https://github.com/Viscent/javamtia/blob/06faaaa9610a8b9af2ee0eec551a0365154ef8fa/JavaMultiThreadInAction/src/io/github/viscent/mtia/ch5/ThreadTerminationRegistry.java
有add方法
public synchronized void register(Handler handler) {
handlers.add(handler);
}
但并没有
public synchronized void remove(Handler handler) {
handlers.remove(handler);
}
如果在生产环境中,handlers会越来越大,是否会内存溢出?

private final Set handlers = new HashSet();

能否改为

private final Set handlers = new ConcurrentHashSet();

register 和 remove 方法去掉 synchronized 关键字是否也能达到线程安全,并且效率更高。

比如以下写法:
private final Set handlers = Collections.newSetFromMap(new ConcurrentHashMap<Handler,Boolean>());

  public void register(Handler handler) {
		  handlers.add(handler);
  }
  
  public void remove(Handler handler) {
	      handlers.remove(handler);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant