• Post Reply Bookmark Topic Watch Topic
  • New Topic

Atomic Operations  RSS feed

 
Allen George
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way using J2SE 5.0 to group a number of function calls (2 in my case) into a single atomic operation?

I have 3 independent threads (no contention for resources). On a MP box I don't want one of my threads to be interrupted while it's in the middle of making these two critical ops. At the same time I don't want to have a common lock object because in effect I start to serialize the operation of my threads...

Any suggestions? Or even a "this is not possible"?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

I think you have two conflicting goals - atomic operations without serializing the threads. Making them atomic will necessarily make one thread wait until all others are done before proceeding. This is about all I can think of ...

If those methods are fast the penalty of making your three threads go through there single file may not be too high.
 
Allen George
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey - thanks for the reply!

I was curious about what to do on a multiprocessor box. In that case, I'd want all threads to conceivably execute simultaneously since their actions are independent. That's why I don't want to implement a shared lock object.

Does that make sense?
 
Henry Wong
author
Sheriff
Posts: 22860
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allen George:
Hey - thanks for the reply!

I was curious about what to do on a multiprocessor box. In that case, I'd want all threads to conceivably execute simultaneously since their actions are independent. That's why I don't want to implement a shared lock object.

Does that make sense?


If the threads "actions are independent", then don't synchronized them. If they are dependent on each other, that they can't interrupt each other, then synchronize them.

If you don't want to use synchronization, with threads that can't be interrupted by other threads, you have to redesign it so that they are truly independent.

Henry
 
Henry Wong
author
Sheriff
Posts: 22860
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, since you mentioned 5.0 and "atomic"... Java 5.0 added a bunch of atomic classes that in-theory make it possible to remove any synchronization. It is to support a style of threaded programming -- called optimistic programming.

Optimistic locking can get very complicated, since it entails being able to detect concurrent modifications, and recovering from them.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!