是否存在JVM的最坏情况实现?

时间:2021-05-23 03:47:25

The Java memory model makes it clear what can and cannot be assumed about how threads interact through memory. For example, if one thread writes a new value to a field without appropriate synchronization then the new value is not guaranteed to be observable by other threads. In practice, however, other threads might anyhow read the new value in spite of inadequate synchronization, depending on time between write and read, hardware architecture, etc.

Java内存模型清楚地说明了线程如何通过内存进行交互可以和不可以假设的内容。例如,如果一个线程在没有适当同步的情况下将新值写入字段,则不保证新值可由其他线程观察到。然而,在实践中,尽管写入和读取之间的时间,硬件体系结构等等,其他线程仍可能无论如何都会读取新值。

This can lead to bugs that are hard to discover and difficult to reproduce. It could therefore be useful to run a java application on a worst case JVM that did absolutely no memory synchronization between threads beyond the guarantees in the Java memory model. Does such a worst case JVM implementation exist?

这可能导致难以发现且难以复制的错误。因此,在最坏情况的JVM上运行java应用程序可能很有用,它在Java内存模型中的保证之外的线程之间绝对没有内存同步。是否存在这种最坏情况的JVM实现?

5 个解决方案

#1


You could try using Terracotta to cluster your program. It is incredibly unforgiving around incorrect synchronization (which will become apparent even with only one node in the cluster). This is a great question: I've often wanted exactly this ability - I'm surprised there's not a switch in the standard JRE -XXJMMExtreme

您可以尝试使用Terracotta来聚类您的程序。对于不正确的同步,这是非常不可原谅的(即使集群中只有一个节点,这种情况也会变得明显)。这是一个很好的问题:我经常想要这种能力 - 我很惊讶标准JRE -XXJMMExtreme没有切换

Terracotta is open-source and free for the basic product.

Terracotta是开源的,免费提供基本产品。

#2


This might help: http://javapathfinder.sourceforge.net/

这可能会有所帮助:http://javapathfinder.sourceforge.net/

#3


I don't know of any VM which guarantees worst case behavior all the time, which seems to be what you are asking for. The situation that you are describing can occur with Sun VMs (as well as many others), but only due to caching issues. I'm not familiar with a VM that intentionally does this all of the time.

我不知道任何VM一直保证最坏的情况,这似乎是你要求的。您正在描述的情况可能发生在Sun VM(以及许多其他虚拟机)上,但仅限于缓存问题。我不熟悉一直故意这样做的虚拟机。

#4


There are many ways which could trigger a concurrency bug.

有很多方法可以触发并发错误。

  • Load your application to many more threads than you would normally expect. Make sure this is more than enough to get 99%+ CPU.
  • 将应用程序加载到比平常预期更多的线程。确保这足以获得99%以上的CPU。

  • Run your program with a profiler enabled or the JIT disabled. This changes the timing behaviour of your application.
  • 在启用了Profiler或禁用JIT的情况下运行程序。这会更改应用程序的计时行为。

  • Test both Java 5 and Java 6 (This is often the simplest and the best way to find a few bugs) I have not found a bug using Java 7 which didn't appear in 5/6.
  • 测试Java 5和Java 6(这通常是查找一些错误的最简单和最好的方法)我没有发现使用Java 7的错误,它没有出现在5/6中。

For worst case JVM, try a mobile phone. (Your application probably won't work at all) ;)

对于最坏情况的JVM,请尝试使用手机。 (你的申请可能根本不起作用);)

#5


Synchronization errors are usually hard to reproduce because they depend on subtle timings between different threads, so no implementation which actually tries to "just run your program" can always be "worst possible". You can't reproduce more than one different way that two threads can interleave their instructions if you just execute these instructions once. Testing all such combinations in a single run is even less possible. One of the other posters suggested Java Pathfinder and that sounds like a good idea - but note that it's an app which runs the same code many times, so you can't really treat is as just another JVM implementation.

同步错误通常很难重现,因为它们依赖于不同线程之间的细微时序,因此实际上只是“只运行程序”的实现并不总是“最糟糕的”。如果只执行一次这些指令,则无法重现两种不同的方式,即两个线程可以交错其指令。在一次运行中测试所有这些组合的可能性更小。其中一个海报提出了Java Pathfinder,这听起来是个好主意 - 但请注意,它是一个多次运行相同代码的应用程序,因此您无法真正将其视为另一个JVM实现。

One more practical tip is to try and run the app on as many different JVMs as possible. Try different vendors, different versions from the same vendor, different CPU architectures and so on. A few years ago, I had experience with a heavily multithreaded app that had been developed, tested and run on Sun's JVM on Xeon CPUs where it worked very well. At one point I tried running it on IBM's J9 Java Virtual Machine on POWER architecture and at first attempt, about 2/3 of the tests failed due to synchronization errors. So, testing in different environments can be quite good at exposing hidden synchronization issues.

另一个实用技巧是尝试在尽可能多的不同JVM上运行应用程序。尝试不同的供应商,来自同一供应商的不同版本,不同的CPU架构等。几年前,我有一个经过大量多线程应用程序的经验,该应用程序已在Xeon CPU上的Sun JVM上开发,测试和运行,并且运行良好。有一次,我尝试在POWER架构上的IBM J9 Java虚拟机上运行它,并且在第一次尝试时,由于同步错误,大约有2/3的测试失败了。因此,在不同环境中进行测试可以很好地揭示隐藏的同步问题。

#1


You could try using Terracotta to cluster your program. It is incredibly unforgiving around incorrect synchronization (which will become apparent even with only one node in the cluster). This is a great question: I've often wanted exactly this ability - I'm surprised there's not a switch in the standard JRE -XXJMMExtreme

您可以尝试使用Terracotta来聚类您的程序。对于不正确的同步,这是非常不可原谅的(即使集群中只有一个节点,这种情况也会变得明显)。这是一个很好的问题:我经常想要这种能力 - 我很惊讶标准JRE -XXJMMExtreme没有切换

Terracotta is open-source and free for the basic product.

Terracotta是开源的,免费提供基本产品。

#2


This might help: http://javapathfinder.sourceforge.net/

这可能会有所帮助:http://javapathfinder.sourceforge.net/

#3


I don't know of any VM which guarantees worst case behavior all the time, which seems to be what you are asking for. The situation that you are describing can occur with Sun VMs (as well as many others), but only due to caching issues. I'm not familiar with a VM that intentionally does this all of the time.

我不知道任何VM一直保证最坏的情况,这似乎是你要求的。您正在描述的情况可能发生在Sun VM(以及许多其他虚拟机)上,但仅限于缓存问题。我不熟悉一直故意这样做的虚拟机。

#4


There are many ways which could trigger a concurrency bug.

有很多方法可以触发并发错误。

  • Load your application to many more threads than you would normally expect. Make sure this is more than enough to get 99%+ CPU.
  • 将应用程序加载到比平常预期更多的线程。确保这足以获得99%以上的CPU。

  • Run your program with a profiler enabled or the JIT disabled. This changes the timing behaviour of your application.
  • 在启用了Profiler或禁用JIT的情况下运行程序。这会更改应用程序的计时行为。

  • Test both Java 5 and Java 6 (This is often the simplest and the best way to find a few bugs) I have not found a bug using Java 7 which didn't appear in 5/6.
  • 测试Java 5和Java 6(这通常是查找一些错误的最简单和最好的方法)我没有发现使用Java 7的错误,它没有出现在5/6中。

For worst case JVM, try a mobile phone. (Your application probably won't work at all) ;)

对于最坏情况的JVM,请尝试使用手机。 (你的申请可能根本不起作用);)

#5


Synchronization errors are usually hard to reproduce because they depend on subtle timings between different threads, so no implementation which actually tries to "just run your program" can always be "worst possible". You can't reproduce more than one different way that two threads can interleave their instructions if you just execute these instructions once. Testing all such combinations in a single run is even less possible. One of the other posters suggested Java Pathfinder and that sounds like a good idea - but note that it's an app which runs the same code many times, so you can't really treat is as just another JVM implementation.

同步错误通常很难重现,因为它们依赖于不同线程之间的细微时序,因此实际上只是“只运行程序”的实现并不总是“最糟糕的”。如果只执行一次这些指令,则无法重现两种不同的方式,即两个线程可以交错其指令。在一次运行中测试所有这些组合的可能性更小。其中一个海报提出了Java Pathfinder,这听起来是个好主意 - 但请注意,它是一个多次运行相同代码的应用程序,因此您无法真正将其视为另一个JVM实现。

One more practical tip is to try and run the app on as many different JVMs as possible. Try different vendors, different versions from the same vendor, different CPU architectures and so on. A few years ago, I had experience with a heavily multithreaded app that had been developed, tested and run on Sun's JVM on Xeon CPUs where it worked very well. At one point I tried running it on IBM's J9 Java Virtual Machine on POWER architecture and at first attempt, about 2/3 of the tests failed due to synchronization errors. So, testing in different environments can be quite good at exposing hidden synchronization issues.

另一个实用技巧是尝试在尽可能多的不同JVM上运行应用程序。尝试不同的供应商,来自同一供应商的不同版本,不同的CPU架构等。几年前,我有一个经过大量多线程应用程序的经验,该应用程序已在Xeon CPU上的Sun JVM上开发,测试和运行,并且运行良好。有一次,我尝试在POWER架构上的IBM J9 Java虚拟机上运行它,并且在第一次尝试时,由于同步错误,大约有2/3的测试失败了。因此,在不同环境中进行测试可以很好地揭示隐藏的同步问题。