Java多线程:意外的结果

前端之家收集整理的这篇文章主要介绍了Java多线程:意外的结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个企业应用程序.我在多线程环境中运行应用程序时遇到了一些问题.我正在编写一个程序,其中有一个变量,其值以非常快的速率更新(递增)(例如10000次更新/ persecond).循环运行一定的迭代,变量的值递增并存储在HashMap中.一旦循环终止并且值打印HashMap中的变量.我得到了变量的意外价值.

这是演示程序(请阅读评论以便更好地理解):

  1. class test implements Runnable {
  2.  
  3. static ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();
  4. static AtomicInteger value_to_be_incremented_stored = new AtomicInteger(0); // variable whose value to be updated
  5. static AtomicInteger i = new AtomicInteger(0); // this runs the loop
  6.  
  7. @Override
  8. public void run() {
  9.  
  10. for (i.set(0); i.get() < 100000; i.incrementAndGet()) {
  11. /*
  12. This loop should run 100000 times and when loop terminates according to me value of variable
  13. "value_to_be_incremented_stored" should be 100000 as its value is incremented
  14. 100000 times the loop also runs 100000 times.
  15. */
  16. System.out.println("Thread > " + Thread.currentThread() + " " + value_to_be_incremented_stored.incrementAndGet());
  17. map.put("TC",value_to_be_incremented_stored.intValue());
  18. }
  19.  
  20. System.out.println("Output by Thread " + Thread.currentThread() + " " + map.toString());
  21. }
  22.  
  23. public static void main(String[] args) {
  24.  
  25. test t1 = new test();
  26. Thread thread1 = new Thread(t1);
  27. thread1.setName("Thread 1");
  28.  
  29. Thread thread2 = new Thread(t1);
  30. thread2.setName("Thread 2");
  31.  
  32. Thread thread3 = new Thread(t1);
  33. thread3.setName("Thread 3");
  34.  
  35. Thread thread4 = new Thread(t1);
  36. thread4.setName("Thread 4");
  37.  
  38. thread1.start();
  39. thread2.start();
  40. thread3.start();
  41. thread4.start();
  42.  
  43. }
  44. }

输出(变化):

问题 :
我正在运行循环100000次(i.get()< 100000)然后变量value_to_be_incremented_stored的值如何变得超过100000.

解决方法

我发现了三个缺陷.在比较循环计数器的点与增加循环计数器的位置之间的for循环中存在竞争条件.您应该在一个步骤中执行此操作以获取原子操作:
  1. for ( ; i.incrementAndGet() < 100000; ) {

另一个是你的计数器增量和放在地图之间也存在竞争条件.即使你将它们串联递增,任何线程都可以在内部具有不同的值(它位于循环中的不同点),并且它可以将先前的值放在全局映射中.这里需要原子性以确保增加的值是您在循环中放置的值.

  1. synchronized( lock ) {
  2. value_to_be_incremented_stored.incrementAndGet();
  3. map.put("TC",value_to_be_incremented_stored.intValue());
  4. }

最后由于某种原因<比较产生的值为99999.我不得不使用< =来修复它. (正如我们在评论中所讨论的那样,在每个for循环开始时设置i.set(0)并不是很明显的原因.我猜想有四个缺陷.)

  1. class ThreadTestX implements Runnable {
  2.  
  3. static ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();
  4. static AtomicInteger value_to_be_incremented_stored = new AtomicInteger(0); // variable whose value to be updated
  5. static AtomicInteger i = new AtomicInteger(0); // this runs the loop
  6. static final Object lock = new Object();
  7.  
  8. @Override
  9. public void run() {
  10.  
  11. for ( ; i.incrementAndGet() <= 100000; ) {
  12. /*
  13. This loop should run 100000 times and when loop terminates according to me value of variable
  14. "value_to_be_incremented_stored" should be 100000 as its value is incremented
  15. 100000 times the loop also runs 100000 times.
  16. */
  17. synchronized( lock ) {
  18. value_to_be_incremented_stored.incrementAndGet();
  19. // System.out.println("Thread > " + Thread.currentThread() +
  20. // " " + value_to_be_incremented_stored.get());
  21. map.put("TC",value_to_be_incremented_stored.intValue());
  22. }
  23. }
  24.  
  25. System.out.println("Output by Thread " + Thread.currentThread()
  26. + " " + map.toString());
  27. }
  28.  
  29. public static void main(String[] args) {
  30.  
  31. ThreadTestX t1 = new ThreadTestX();
  32. Thread thread1 = new Thread(t1);
  33. thread1.setName("Thread 1");
  34.  
  35. Thread thread2 = new Thread(t1);
  36. thread2.setName("Thread 2");
  37.  
  38. Thread thread3 = new Thread(t1);
  39. thread3.setName("Thread 3");
  40.  
  41. Thread thread4 = new Thread(t1);
  42. thread4.setName("Thread 4");
  43.  
  44. thread1.start();
  45. thread2.start();
  46. thread3.start();
  47. thread4.start();
  48.  
  49. }
  50. }

输出

  1. run:
  2. Output by Thread Thread[Thread 4,5,main] {TC=100000}
  3. Output by Thread Thread[Thread 3,main] {TC=100000}
  4. Output by Thread Thread[Thread 1,main] {TC=100000}
  5. Output by Thread Thread[Thread 2,main] {TC=100000}
  6. BUILD SUCCESSFUL (total time: 0 seconds)

事后判断:尽管标记正确,但我不确定我是否正确.这里的问题是你试图让三件事情保持同步:循环计数器i,要增加的值和地图.允许在同步块之外执行这些中的任何一个可以邀请它们处于意外状态.我认为以下可能更安全:

  1. @Override
  2. public void run() {
  3.  
  4. for ( ;; ) {
  5. synchronized( lock ) {
  6. if( i.incrementAndGet() <= 100000 ) {
  7. value_to_be_incremented_stored.incrementAndGet();
  8. map.put("TC",value_to_be_incremented_stored.intValue());
  9. }
  10. else
  11. break;
  12. }
  13. }
  14. System.out.println("Output by Thread " + Thread.currentThread()
  15. + " " + map.toString());
  16. }

这消除了将变量声明为AtomicInteger的需要,但是我没有看到如何确保它们的值在该循环执行时不会改变(由于某些其他线程).

猜你在找的Java相关文章