Re: On the necessity of threads having sleep time...
- From: fguy64s@xxxxxxxxx
- Date: Sat, 25 Apr 2009 13:47:47 -0700 (PDT)
On Apr 25, 3:37 pm, George Peter Staplin <georg...@xxxxxxxxxxxx>
wrote:
fguy...@xxxxxxxxx wrote:
I am currently studying Java, but I imagine this applies to any
language. I'm a newbie when it comes to thread programming.
Here is a quote from an instructional article on a website...
"Note that it is important to sleep somewhere in a thread. If not, the
thread will consume all CPU time for the process and will not allow
any other methods such as threads to be executed."
I don't quite follow. If the purpose of threads is to allow for
multitasking and sharing of resources, then why do you have to give a
thread sleep time in order for other threads to work? Doesn't that
kind of defeat the purpose?
here is my source for the quote.
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html?page=1
That article has some flaws (as Eric has already pointed out). The features
it uses were deprecated in Java, because they are prone to causing
deadlocks.
See:http://java.sun.com/javase/6/docs/api/java/lang/Thread.html
One problem is that if your thread is constantly spinning and doing nothing
essentially while waiting for resources, that CPU core goes to waste, and
interactivity suffers.
If you structure your program's threads to make them react to events, you
can generally create a better application. For instance: you might have
one thread that read()s from client connections when a client has a
readable socket, and another that write()s pending data to clients.
If the writer thread has nothing to write(), you probably don't want it to
do be stuck in some while loop doing nothing, because those are just wasted
cycles. So you might have a pending queue of things to write, and the
writer would wait for something to become ready if there is no more work
for it to do.
There are also some rare situations where you wouldn't want any waiting or
blocking at all, because so many events occur routinely that the thread can
always do work. You would know if you need that, and my assumption is that
you don't.
-George
Yes, I did in fact notice that stop() was deprecated, no problem in
coming up with an alternative. Have the thread monitor a variable that
tells it when to stop.
I'll tell you what I have on the go. A little chess program that is a
learning project for me. It works well as a single threaded
application, except for two issues.
1. I have implemented a form of drag and drop for the human to move.
When the human finishes it's move, the board ( a JPanel with a paint()
method ) does not finish the redraw in a timely fashion because the
program is busy calculating its next move.
2. When it is the human's turn to move, the program is idle. I want
the program to think while it is waiting for me to move. So I have
planned to have the computer opponent run as a thread, that monitors a
boolean value that determines when it is it's turn to move. The board
implements a mouse listener, the MouseReleased() method that kicks in
when I drop a piece on a square sets the boolean variable that the
thread monitors.
So my question is, if the sleep remarks in the article are valid, it
would suggest that if my computer opponent is running as a thread even
when it is my turn, then I will not be able to move the pieces unless
I give the thread some sleep time, in order to free up some CPU to
allow me to move the piece. I suppose all I need to do is try both
methods, but I just thought I'd ask.
Anyways, I hope all this is clear, I have been know to be obtuse with
this kind of stuff.
.
- References:
- On the necessity of threads having sleep time...
- From: fguy64s
- Re: On the necessity of threads having sleep time...
- From: George Peter Staplin
- On the necessity of threads having sleep time...
- Prev by Date: Re: On the necessity of threads having sleep time...
- Next by Date: Re: On the necessity of threads having sleep time...
- Previous by thread: Re: On the necessity of threads having sleep time...
- Next by thread: Re: On the necessity of threads having sleep time...
- Index(es):
Relevant Pages
|