Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Setting a value for Sleep from the command line Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Setting a value for Sleep from the command line " Watch "Setting a value for Sleep from the command line " New topic
Author

Setting a value for Sleep from the command line

Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 26
I am currently trying to write a class that can take a value from the command line and then cause a subsequent process to sleep for that period of time.

I have so far written the following code:


I would like to pass the value in sleepTime to the Sleeper class and have the sleep method use it as a parameter.

Can anyone suggest how this can be done? Clearly I can pass a second argument (args[1]) to Sleeper and it will use it in this is because there is a getName() method in Thread.

Trying the following gives the compilation error:


Any help would be greatly appreciated..
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Give your Sleeper class a constructor that takes an int as a parameter and store that value in an instance variable. You can then use it in your run method.


Joanne
Stuart Lord
Greenhorn

Joined: Oct 27, 2006
Posts: 26
Joanne - thanks for your help, I changed the code as you suggested and it works fine! I insert the working version, for interest, with a few minor modifications:

Just a final comment, I note that the API documentation, states that sleep expects a long. I assume it is safer therefore to use Long.parseLong in Sleeping and make corresponding changes to Sleeper..
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hi stuart,

I think it is better if the sleeping varriable be set to a long type as you would be setting the time in milliseconds and sleep() take an argument of type long!!!..


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Setting a value for Sleep from the command line
 
Similar Threads
doubt in join method.
Threads & Locks??
Output to a file
Want timeout error on hung thread
Single thread that runs 5 times, sleeps, then runs 5 more times