Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting a value for Sleep from the command line  RSS feed

 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!