This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes main method synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "main method synchronization" Watch "main method synchronization" New topic
Author

main method synchronization

Dar Var
Ranch Hand

Joined: Oct 12, 2004
Posts: 74
I have a main method which is synchronized



I am using cron to run the program 4 times all at the same time.
e.g. the program is run 4 times a 3o'clock

The synchronization doesn't seem to be working however. The program is run as usual. java -arguments

Are the serperate java calls run in different virtual machines and the reason for it not working is that synchronization doesn't work accross JVMs??

Is there are solution to this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, each time you launch a separate JVM... you get a separate JVM. Synchronization doesn't work accross processes.

If you want to run the program 4 times, but not all at once, why not just run it four times from a single script, one after the other? For that matter, why not just put a loop in main(), and run it once? Am I missing something here?


[Jess in Action][AskingGoodQuestions]
Dar Var
Ranch Hand

Joined: Oct 12, 2004
Posts: 74
I don't think the loop will work as I take in a different args[] each time and run the program using them.

The script isn't an option either I'm afraid
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

1. synchronized method is a bad idea.
2. synchronization main method is a worse idea.


Are the serperate java calls run in different virtual machines and the reason for it not working is that synchronization doesn't work accross JVMs??


Yes.

If you can't modify the script there are some tricks you can try. In your main method try to connect to an external java program through RMI. If you cant connect to it, create it. Then you can use that external program to grant each new program the permission to run. You can have it shut itself down when it feels it should.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Dar Var:
I don't think the loop will work as I take in a different args[] each time and run the program using them.


So pass all the arguments at once, and have main() sort them out.

But why isn't the script an option?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

And are you bound to command-line options?

You could read linewise from a file:

or from stdin


http://home.arcor.de/hirnstrom/bewerbung
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Or you can write a separate Java class which calls the main() in the other class with the options one at a time. The problem we're having is that we don't fully understand your requirements, so we're just guessing solutions.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
I would rephrase the question as what is synchronized and how does it work and move to the beginner sesion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method synchronization