| 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
|
|
|
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
|
 |
 |
|
|
subject: Setting a value for Sleep from the command line
|
|
|