File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Synchronization across diff JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization across diff JVM" Watch "Synchronization across diff JVM" New topic
Author

Synchronization across diff JVM

George Nan
Greenhorn

Joined: Mar 09, 2006
Posts: 3
How do you synchronize a section of code that can only be run by one thread
on multiple JVMs
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
You'll need an artificial mechanism: Synchronization is process specific.


Java Regular Expressions
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What's a good external sync, Max? Maybe a database? This seems really clunky:

start transaction
read a row for update
do the synchronized code
update the row
end transaction


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Originally posted by Stan James:
What's a good external sync, Max? Maybe a database? This seems really clunky:

start transaction
read a row for update
do the synchronized code
update the row
end transaction



The question is "why would you want a synchronization across JVMs"?... Interestingly, in many cases, it is to either protect a service or database.

So... although it is clunky, in many cases, it may be the best thing to do.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by George Nan:
How do you synchronize a section of code that can only be run by one thread
on multiple JVMs


A section of code? You can't. Why would you want to? Perhaps you are trying to synchronize access to some specific resource that is shared across multiple JVMs?
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Stan James:
What's a good external sync, Max? Maybe a database? This seems really clunky:

start transaction
read a row for update
do the synchronized code
update the row
end transaction



Boy, that's a really excellent question. You could use a shared File, but with virtual file shares and such, you're fishing for trouble there. You could use an external service: say a locking external webservice or EJB. But that's clunky too, and prone to network issues.

I'm amazed to see myself type this: but yes, I'd say a database: with some sort of transactional activity.

What do you think, Stan? I'm very interested in hearing your thoughts.

M
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I don't think this question can be adequately answered until you explain what you're trying to do and why. Often times questions like this are perplexing because without knowledge of your requirements and motivations we can only guess at what an optimal solution is. Furthermore, it's not uncommon for the question to be a result of a misguided attempt at solving a related problem. By understanding the problem that motivates this need we may also be able to offer suggestions that avoid the issue completely.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
I'm not sure I agree with that, Ken. This is a general problem, and I can see cropping up all over the place: especially as multi clustering and multiprocessing becomes more common.

I think I can recommend the database approach generally, without know the details of exactly why it's necessary to synch across multiple processors. I think this sort of problem is becoming more and more common: I suspect that java 6 or 7 will provide a mechanism to address it.

M
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Multiple processors? I believe they asked about multiple JVMs.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Multiple processors are superset of multiple JVMs: solve the problem for multiple processors, and you've also solved it across multiple JVMs on a single processor.

Since Java is designed to work consistently across various OSs, and most Unix production machines do use multiple processors, a Java solution to multiple JVMs on the same processor probably isn't very useful.

Practically speaking, it's probably a very safe bet to assume that we're talking multiple processors in most all production grade JVM environments.

-best,
M
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I thought a single JVM could operate on multiple processors if it used multiple threads?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm curious about the problem requirements, too. I can imagine (make up) serializing access to a shared physical device but this isn't something I've run into mice elf. And it would be better if the device came with a driver that took care of this for me.

I'd probably try a database option if the app already has a transactional database. DB vendors have spent a lot of time working on concurrency and I'm sure they do it better than I do.
[ March 24, 2006: Message edited by: Stan James ]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

for access to a shared resource i would not use a database. I would create a server to arbitrate access. probably across RMI.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Stan James:
I'd probably try a database option if the app already has a transactional database. DB vendors have spent a lot of time working on concurrency and I'm sure they do it better than I do.



I have to agree. Still, it *does* seem like we need better answers: probably from the language itself.
[ March 27, 2006: Message edited by: Max Habibi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization across diff JVM