• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AtomicInteger.incrementAndGet infinite loop?

 
Peter Kovac
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible that AtomicInteger.incrementAndGet or similar methods will run forever?




thanks,
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Kovac wrote:
Is it possible that AtomicInteger.incrementAndGet or similar methods will run forever?


In theory, I guess it is possible. In practice, I don't see how.

The only way to get an infinite loop, is to have constant CAS failures. And the only way to have constant CAS failures, is for another thread to exist -- another thread which constantly changes the integer. And for the scheduler to have the other thread change the integer in a way that your thread can't do two iterations in a row.

Henry
 
Peter Kovac
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Peter Kovac wrote:
Is it possible that AtomicInteger.incrementAndGet or similar methods will run forever?

The only way to get an infinite loop, is to have constant CAS failures.


Thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic