• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protect a block of code to execute from second instance of class running  RSS feed

 
Saurabh Agarwal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a class called FirstProgram.class with a main method.
In this class I am have defined one method called doData() and calling that inside a infinite loop using run() method.

I have deployed this class in my local environment.
Now I am running this class from command prompt three times.
cmd> java FirstProgram arg1
cmd> java FirstProgram arg2
cmd> java FirstProgram arg3

Now I want that if doData() is currently executing for any running instance out of three, then remaining two should wait for their turn on doData() until the running instance is finished.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread would fit better in our synchronisation forum: moving.

You do not protect the run method (does this class implement Runnable?); you protect the other method. Since Java5 you would have used the Lock interface
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really need them to be separate application instances, then the easiest way to synchronize them is through a lock file.

Open a file channel, and then acquire a lock on a file that all other application instances know about. Applications will have to wait on the lock to be released before they can continue. This is what it looks like:


Be careful: File locks are system dependent, and may only be advisory. If you need something robust then you need to develop a protocol for the application instances to communicate with each other.
 
Saurabh Agarwal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for replies.

Yes my class implements Runnable.
Also my runtime environment is 1.4....sorry I didnt' pointed this out earlier.

I guess the solution you both have provided will not work in 1.4.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh Agarwal wrote:Now I am running this class from command prompt three times.
cmd> java FirstProgram arg1
cmd> java FirstProgram arg2
cmd> java FirstProgram arg3

Why? And why are you running Java 1.4? Public support and security updates for it ended in October 2008.

So, if it was me, I'd change my version. Immediately.

However, assuming you can't do that, I would do two things:

1. Change your app to accept more than one argument, viz:
  java FirstProgram arg1 arg2 arg3 ...
and then run each "argument" as a separate Thread.

2. Have it touch a flag file on entry and remove it on exit, and throw an Exception if the flag file exists on entry.
It isn't a 100% solution, but it'll stop most inadvertent misuse.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree about getting rid of Java1.4, which is now only of historic interest. In Java1.4. you would have to mark the method synchronized.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!