您的 Java 低延迟应用程序开发清单是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2574579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is your development checklist for Java low-latency application?
提问by Mohan Narayanaswamy
I would like to create comprehensive checklist for Java low latency application. Can you add your checklist here?
我想为 Java 低延迟应用程序创建全面的清单。你能在这里添加你的清单吗?
Here is my list
1. Make your objects immutable
2. Try to reduce synchronized method
3. Locking order should be well documented, and handled carefully
4. Use profiler
5. Use Amdhal's law, and find the sequential execution path
6. Use Java 5 concurrency utilities, and locks
7. Avoid Thread priorities as they are platform dependent
8. JVM warmup can be used
9. Prefer unfair locking strategy
10. Avoid context-switching (many threads lead to counter productive)
11. Avoid boxing, un-boxing
12. Give attention to compiler warnings
13. Number of threads should be equal or lesser than the number of core
这是我的清单
1. 使您的对象不可变
2. 尝试减少同步方法
3. 锁定顺序应该有据可查,并小心处理
4. 使用分析器
5. 使用 Amdhal 定律,并找到顺序执行路径
6. 使用 Java 5并发实用程序和锁
7. 避免线程优先级,因为它们依赖于平台
8. 可以使用 JVM 预热
9. 首选不公平的锁定策略
10. 避免上下文切换(许多线程导致反效果)
11. 避免装箱、拆箱
12. 注意编译器警告
13. 线程数应等于或小于核心数
Low-latency application is tuned for every milli-seconds.
低延迟应用程序每毫秒进行一次调整。
回答by medopal
Use StringBuilder
instead of String
when generating large Strings. For example queries.
在生成大字符串时使用StringBuilder
代替String
。例如查询。
回答by Thorbj?rn Ravn Andersen
Buy, read, and understandEffective Java. Also available online
购买、阅读和理解Effective Java。也可以上网
回答by Tom Hawtin - tackline
Although immutability is good, it is not necessarily going to improve latency. Ensuring low-latency is likely to be platform dependent.
尽管不变性很好,但它不一定会改善延迟。确保低延迟可能取决于平台。
Other than general performance, GC tuning is very important. Reducing memory usage will help GC. In particular if you can reduce the number of middle-aged objects that need to get moved about - keep it object either long lived or short lived. Also avoid anything touching the perm gen.
除了一般性能外,GC 调优非常重要。减少内存使用将有助于 GC。特别是如果您可以减少需要移动的中年对象的数量 - 将其保持为长期或短期的对象。还要避免任何接触烫发的东西。
回答by Eleco
avoid boxing/unboxing, use primitive variables if possible.
避免装箱/拆箱,尽可能使用原始变量。
回答by maerics
Another important idea is to get it working first, then measure the performance, then isolate any bottlenecks, then optimize them, then measure again to verify improvement.
另一个重要的想法是先让它工作,然后测量性能,然后隔离任何瓶颈,然后优化它们,然后再次测量以验证改进。
As Knuth said, "premature optimization is the root of all evil".
正如 Knuth 所说,“过早的优化是万恶之源”。
回答by bobah
Avoid context switching wherever possible on the message processing path Consequence: use NIO and single event loop thread (reactor)
尽可能避免在消息处理路径上进行上下文切换 后果:使用 NIO 和单事件循环线程(反应器)
回答by Brad
Do not schedule more threads in your application than you have cores on the underlying hardware. Keep in mind that the OS will require thread execution and potentially other services sharing the same hardware, so your application may be requried to use less than the maximunm number of cores available.
不要在应用程序中安排的线程数超过底层硬件上的内核数。请记住,操作系统将需要线程执行和可能共享相同硬件的其他服务,因此您的应用程序可能需要使用少于最大可用内核数。
回答by Nitsan Wakart
Measure, measure and measure. Use as close to real data with as close to production hardware to run benchmarks regularly. Low latency applications are often better considered as appliances, so you need to consider the whole box deployed not just the particular method/class/package/application/JVM etc. If you do not build realistic benchmarks on production like settings you will have surprises in production.
测量,测量和测量。使用尽可能接近真实数据和生产硬件来定期运行基准测试。低延迟应用程序通常被更好地视为设备,因此您需要考虑部署的整个框,而不仅仅是特定的方法/类/包/应用程序/JVM 等。生产。
回答by Amir Moghimi
Avoid extensive locking and multi-threading in order not to disrupt the enhanced features in modern processors (and their caches). Then you can use a single thread up to its unbelievable limits (6 million transactions per second) with very low latency.
避免大量锁定和多线程,以免破坏现代处理器(及其缓存)中的增强功能。然后,您可以将单个线程使用到令人难以置信的限制(每秒 600 万个事务),并且延迟非常低。
If you want to see a real world low-latency Java application with enough details about its architecture have a look at LMAX:
如果您想查看真实世界的低延迟 Java 应用程序及其架构的足够详细信息,请查看 LMAX:
回答by L. Blanc
I think "Use mutable objects only where appropriate" is better than "Make your objects immutable". Many very low latency applications have pools of objects they reuse to minimize GC. Immutable objects can't be reused in that way. For example, if you have a Location class:
我认为“仅在适当的地方使用可变对象”比“使您的对象不可变”更好。许多非常低延迟的应用程序都有对象池,它们可以重用以最小化 GC。不可变对象不能以这种方式重用。例如,如果您有一个 Location 类:
class Location {
double lat;
double lon;
}
You can create some on bootup and use them over and over again so they never cause allocations and the subsequent GC.
您可以在启动时创建一些并一遍又一遍地使用它们,这样它们就不会导致分配和随后的 GC。
This approach is much trickier than using an immutable location object though, so it should only be used where needed.
不过,这种方法比使用不可变的位置对象要复杂得多,因此它应该只在需要的地方使用。