Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to delay without using Thread.sleep()

 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because I suspect my application has a multi threading issue because of this statement(2 programs running at the same time)

This is my code:

However, it does not go to the exception at all, let's say I run an application in task scheduler, then stop in task scheduler, but sometimes the process doesn't seem to stop completely, then run again.
Will there be 2 threads? Already specified do not start a new instance in task scheduler, in that case is it a multi threading issue?

 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you kick off a different process there are two different processes.
Its not really clear from the info that you provided what you are trying to do ... maybe more details might help
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know
for just delay purpose in my application
should I use thread.sleep()?

http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

Will it cause any multi threading issues?
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:I would like to know
for just delay purpose in my application
should I use thread.sleep()?

http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

Will it cause any multi threading issues?




for just delay purpose in my application
should I use thread.sleep()?


The question is why do you need a delay in the application? In most (well almost all) cases thread-sleep-is-a-sign-of-a-poorly-designed-program

Will it cause any multi threading issues?

Yes. unless you are 100% sure what you are doing. Anything that's multi-threaded needs careful code consideration
 
Sunderam Goplalan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use Thread.sleep if there are, say 4 threads, and assume threads 1,2,3 hog the limelight for some reason. Suppose I want to give a chance for other thread 4 to run, I would use
Thread.sleep on Thread 1,2 3 so that output from Thread 4 is visible too.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but then Thread.sleep() will also take lock along with it when goes to sleep.

yeah then thread 4 can do some work on un-blocked methods but can't do in critical blocked method by rest of the thread 1/2/3.

so it won't solve purpose completely....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunderam Goplalan wrote:I'd use Thread.sleep if there are, say 4 threads, and assume threads 1,2,3 hog the limelight for some reason. Suppose I want to give a chance for other thread 4 to run, I would use
Thread.sleep on Thread 1,2 3 so that output from Thread 4 is visible too.


You don't need to do that. Unless you have a very old or very unusual JVM implementation or underlying OS and hardware, those three will handle scheduling pretty well without any interference from you.

If you have measured and observed that some threads are being starved, you could try adjusting the various threads' relative priorities. As an absolute last resort, you might try calling Thread.yield(). Although, really, you shouldn't need to concern yourself with thread scheduling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic