Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes AtomicInteger.incrementAndGet infinite loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "AtomicInteger.incrementAndGet infinite loop?" Watch "AtomicInteger.incrementAndGet infinite loop?" New topic
Author

AtomicInteger.incrementAndGet infinite loop?

Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42
Hi,

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




thanks,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42
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..
 
wood burning stoves
 
subject: AtomicInteger.incrementAndGet infinite loop?