| Author |
Is it possible to delay without using Thread.sleep()
|
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
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
Joined: Feb 08, 2009
Posts: 576
|
|
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
|
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
|
 |
cle tan
Ranch Hand
Joined: Jun 11, 2012
Posts: 68
|
|
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
Joined: Feb 08, 2009
Posts: 576
|
|
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
Joined: Oct 10, 2011
Posts: 63
|
|
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.
|
SCJP 5.0 , SCEA Java EE 5
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
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....
|
Thanks and Regards,<br />Amit Taneja
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
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.
|
 |
 |
|
|
subject: Is it possible to delay without using Thread.sleep()
|
|
|