• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to restrict execution of same class from two different consoles at same time?  RSS feed

 
Shailesh Sukare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing one problem. I want only one instance of the class to be running on my OS. I have written one loader program and want to execute only one instance of loader at a time. If I execute the loader from one console and while the first loader is running I want to prevent execution of second loader from the another console.Is there any way to restrict the execution of same class from two different consoles.

[ September 05, 2008: Message edited by: Shailesh S. ]
[ September 05, 2008: Message edited by: Bear Bibeault ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has already been a topic about this (although I can't find it right now) which already explored several options.

You could use one of the following mechanisms:
  • Use a lock file; if it exists exit immediately. Otherwise create it and continue. Remember to remove it when the current instance has created it and exits.
  • Create a ServerSocket on a specific port. When the application starts, try to connect to this port. If it succeeds, exit immediately. This one can be a problem if another program uses the same port to listen on already.
  •  
    Adinath Shirsath
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Shailesh S.:
    I am facing one problem. I want only one instance of the class to be running on my OS. I have written one loader program and want to execute only one instance of loader at a time. If I execute the loader from one console and while the first loader is running I want to prevent execution of second loader from the another console.Is there any way to restrict the execution of same class from two different consoles.

    [ September 05, 2008: Message edited by: Shailesh S. ]



    May be Singleton pattern will help you
    [ September 05, 2008: Message edited by: Adinath Shirsath ]
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not really since Singleton only works within a single JVM. If you start another, that will create a new Singleton.
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello "Shailesh S."-

    On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

    As to your question, that depends if you want a single instance of an application or a single instance of a particular class. Your post mentions both, so I'm not sure which one it is.

    If the former, search the forums for the solution Rob mentions (ServerSocket). If the latter, that's much harder (and would be rather unusual).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!