Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify threads

Status
Not open for further replies.

Scooby316

IS-IT--Management
Jan 30, 2003
32
0
0
US
How do you identify a thread. Is only when a class extends a thread or can it be any be any code that does something else within the main code. i.e a mouse listener. I am a noobie so any advice is warmly apprecated.

Cheers
 
This is a "thread" :

Code:
class MyThread extends Thread {
  public void run() {

  }
}

This is not a "thread" :

Code:
class MyThread {
  public void run() {

  }
}

This is a thread also :

Code:
class MyThread2 extends MyThread {

}

// where MyThread is :


class MyThread extends Thread {
  public void run() {

  }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
EventListeners, such as the MouseListener, are nothing to do with threads. By default, for a Swing / AWT app, there is one thread which runs all the code. It monitors user activity and ultimately executes the various event listeners, and draws / redraws screen real-estate. This is why, if in a listener somewhere, you have a loop which never terminates, the whole app locks.

You create a new thread by subclassing Thread, or implementing Runnable and passing it as a constructor parameter to a new Thread instance.

Not sure what you mean by 'identify a thread'. Can you elaborate, maybe?

Tim
 
Basically i have a piece of code and need to know how many threads are in it and what they do. The code only has one piece of code that states that it extends thread so it must only have one thread.

After reading a book from T Budd it gave the impresion that any piece of code that did different thing when the main was being executed could be classed as a thread. So I though that a mouse listener would be a thread. But since a thread must be declared, it should be easy to identify them.

Cheers for the quick responces.
 
Just keep in mind that a thread really refers to a 'thread of execution'. You create one as stated above, but the Thread subclass could make calls to other classes. Any such class which is likely to have its methods executed by more than one thread should be made 'thread-safe' (there are loads of tutorials on the web for this).

Any class, in the software you're looking at, which subclasses Thread or implements Runnable will be the source of new threads.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top