Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

A question about Java Thread

Thread view: 
JTL.zheng - 14 Jun 2007 18:20 GMT
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.
Lew - 14 Jun 2007 18:32 GMT
> I see a code like this:
>
[quoted text clipped - 19 lines]
> 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.

Signature

Lew

Knute Johnson - 14 Jun 2007 18:49 GMT
>> I see a code like this:
>>
[quoted text clipped - 45 lines]
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Marco - 15 Jun 2007 09:07 GMT
> I see a code like this:
>
[quoted text clipped - 19 lines]
> 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.
Roedy Green - 15 Jun 2007 10:07 GMT
>  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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.