Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method synchronization

 
Dar Var
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dar Var
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And are you bound to command-line options?

You could read linewise from a file:

or from stdin
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would rephrase the question as what is synchronized and how does it work and move to the beginner sesion.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic