Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question about Java Thread

Reply
Thread Tools

A question about Java Thread

 
 
JTL.zheng
Guest
Posts: n/a
 
      06-14-2007
I see a code like this:

in a Thread:
--------------------------
public void run() {

Thread currentThread = Thread.currentThread();

while (thread == currentThread) {

try {
repaint();
thread.sleep(100);
}
catch (InterruptedException ex) {
}

}
}
---------------------------

what's the "while (thread == currentThread) " codes mean?
what is it used for?

Thank you in advance.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      06-14-2007
JTL.zheng wrote:
> I see a code like this:
>
> in a Thread:
> --------------------------
> public void run() {
>
> Thread currentThread = Thread.currentThread();
>
> while (thread == currentThread) {
>
> try {
> repaint();
> thread.sleep(100);
> }
> catch (InterruptedException ex) {
> }
>
> }
> }
> ---------------------------
>
> what's the "while (thread == currentThread) " codes mean?
> what is it used for?


There really needs to be more context to be certain. I will make a guess,
though. Apparently 'thread' is an instance variable or final method variable
from outside the Runnable that keeps track of some sort of "active" Thread
knowledge.

If you provide a short, complete example we'll know better.

One observation - the code you're reading might be flawed. It uses the expression

thread.sleep(100);

But sleep() is a static method, so it should not be called via the instance
'thread' but via the class 'Thread':

Thread.sleep(100);

The instance reference implies to some people that sleep() operates on the
specified instance; it does not, necessarily. (In this example it works
because the logic 'thread == currentThread' guarantees that 'thread' refers to
the current Thread, so the two idioms are equivalent this time, luckily.)

To make the logic clear, static methods should be called via class references,
not object references.

--
Lew
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      06-14-2007
Lew wrote:
> JTL.zheng wrote:
>> I see a code like this:
>>
>> in a Thread:
>> --------------------------
>> public void run() {
>>
>> Thread currentThread = Thread.currentThread();
>>
>> while (thread == currentThread) {
>>
>> try {
>> repaint();
>> thread.sleep(100);
>> }
>> catch (InterruptedException ex) {
>> }
>>
>> }
>> }
>> ---------------------------
>>
>> what's the "while (thread == currentThread) " codes mean?
>> what is it used for?

>
> There really needs to be more context to be certain. I will make a
> guess, though. Apparently 'thread' is an instance variable or final
> method variable from outside the Runnable that keeps track of some sort
> of "active" Thread knowledge.
>
> If you provide a short, complete example we'll know better.
>
> One observation - the code you're reading might be flawed. It uses the
> expression
>
> thread.sleep(100);
>
> But sleep() is a static method, so it should not be called via the
> instance 'thread' but via the class 'Thread':
>
> Thread.sleep(100);
>
> The instance reference implies to some people that sleep() operates on
> the specified instance; it does not, necessarily. (In this example it
> works because the logic 'thread == currentThread' guarantees that
> 'thread' refers to the current Thread, so the two idioms are equivalent
> this time, luckily.)
>
> To make the logic clear, static methods should be called via class
> references, not object references.
>


This used to be a fairly commonly seen bit of code for applets. The
thread reference is changed somewhere else (eg. the stop method) to end
the execution of this run method. You don't see many people writing
applets or asking questions about them here very often.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Marco
Guest
Posts: n/a
 
      06-15-2007
JTL.zheng wrote:
> I see a code like this:
>
> in a Thread:
> --------------------------
> public void run() {
>
> Thread currentThread = Thread.currentThread();
>
> while (thread == currentThread) {
>
> try {
> repaint();
> thread.sleep(100);
> }
> catch (InterruptedException ex) {
> }
>
> }
> }
> ---------------------------
>
> what's the "while (thread == currentThread) " codes mean?
> what is it used for?


Usually one uses such constructs to determine the validity of the
thread. That means a Thread-Object holds a reference to itself ("thread"
in the example) which can be set to null from the _outside_ of the
thread. Any 100 ms the thread awakes and checks if it's "thread"
reference still points to itself or has been set to null. If it has been
set to null, the thread exits the while loop and therefore the run()
method returns (say: thread is dead).

whenever you see such constructs, you can be sure they are used to
signal the thread to exit.

But one more Point: Don't use the ref-name "thread" for a reference to a
"Thread" Object! This can easily be misunderstood.




 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-15-2007
On Thu, 14 Jun 2007 10:20:29 -0700, "JTL.zheng" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

> while (thread == currentThread) {


the key to understanding is that "thread" is not a field of the Thread
class. It is an outside variable used for communicating with the
thread.

By fiddling with the value of thread, the repaint/sleep can be turned
on or off. In other words, the run method can be optionally turned
into a dummy that does nothing. Consider that is possible to invoke
the run method without using start to create a new thread. To figure
out exactly precisely what this guy is going we would have to see the
whole program.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining Java Reflection API with Java Annotation Types for Thread Safety pek Java 2 10-23-2007 03:00 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM
PyNew_Interpreter(): "Fatal Python error, invalid thread state for this thread" question vincent wehren Python 0 12-11-2003 08:09 PM
java Bug? -> Java threads: Wait X Notify X [Thread AGAINST Runnable] Renato Battaglia Java 3 10-24-2003 04:54 PM



Advertisments