同步(对象) { } 问题
我遇到了一个性能问题,880 个线程同时执行 synchronized() { method() }
,这导致了一个主要的性能问题。
synchronized()
处等待的线程是否可能存在某种限制?哪里可以得到限额?
另一个问题是什么最好放入 synchronized( ? )
中。因为我有不同的类访问该变量,所以我不能放置synchronized(this)
。
I have ran into a perfomance problem where 880 threads are doing synchronized() { method() }
in the same time and this has lead to a major perfomance problem.
Is it possible that there is some limit of threads waiting at synchronized()
? Where can I get the limit?
Another question is what is best to put into synchronized( ? )
. Because I have different classes accessing that variable, so I can not put synchronized(this)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法使用同步来限制任何内容,对于高级并发构造,您需要查看 http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html。
关于您在synchronized(?)中放入的内容,这意味着您锁定的内容,这取决于您想要实现的锁定行为。如果您有一个可以从所有不同类访问的全局变量(例如 public static Object LOCK = new Object();),并且您对其进行同步,那么所有类都将锁定该全局变量。
请查看有关同步的 Java 教程。
There is no way to limit anything with synchronized, for advanced concurrency constructs you need to have a look at http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html.
Regarding what you put inside synchronized(?), which means on what you lock on, it depends on the locking behavior you want to achieve. If you have a global (for example public static Object LOCK = new Object();) which is accessible from all different classes, and you synchronize on that, then all classes will be locking on that one.
Have a look at the java tutorial on synchronization.