How to identify a thread
1 posts in topic
Flat View  Flat View

Posted By:   H_Morgan
Posted On:   Monday, July 15, 2002 05:11 PM

I need to implement some code that allows a thread to acquire an object lock multiple times. How can i identify this particular thread using code?

Re: How to identify a thread

Posted By:   Eric_Lindauer  
Posted On:   Tuesday, July 16, 2002 05:44 PM

A Thread is an object just like anything else. You can obtain a reference to a Thread object in the following way:

Thread theThread = Thread.currentThread ();

This particular line hands you a reference to whatever thread is currently running. You can then store this reference and use it to do whatever you need ( here, check if the currentThread is allowed to do something before doing it ).

If you need additional control, you can always extend the Thread object and start your own thread. For example:

class MyThread extends Thread {
private String _name;
public MyThread ( String name ) {
this._name = name;

public String getName () {
return _name;

Thread specialThread = new MyThread ( "special" );
myThread.start ();

Then your access code might look like this:

if ( Thread.currentThread () instanceof MyThread ) {
MyThread thread = (MyThread) Thread.currentThread ();
if ( thread.getName ().equals ( "special" ) ) {
... do your thing ...

Hope this helps.

About | Sitemap | Contact