Vinoth
Vinoth

Reputation: 9734

What is the internal operation of getId in Java - Multithreading?

I am learning multithreading in Java. Here I am stuck with the getId() method. How does it return "8"? Then, I am also having a doubt with "Thread-0" of t1 and "Thread-1" of t2, are these the initial values of the getName() method?

  class TestJoinMethod3 extends Thread{  
  public void run(){  
   System.out.println("running...");  
  }  
 public static void main(String args[]){  
  TestJoinMethod3 t1=new TestJoinMethod3();  
  TestJoinMethod3 t2=new TestJoinMethod3();  
  System.out.println("Name of t1:"+t1.getName());  
  System.out.println("Name of t2:"+t2.getName());  
  System.out.println("id of t1:"+t1.getId());  

  t1.start();  
  t2.start();  

  t1.setName("Sonoo Jaiswal");  
  System.out.println("After changing name of t1:"+t1.getName());  
 }  
}  

OUTPUT

   Name of t1:Thread-0
   Name of t2:Thread-1
   id of t1:8
   running...
   After changling name of t1:Sonoo Jaiswal
   running...

Upvotes: 0

Views: 1004

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533500

i am stuck with getId() method

As the Javadoc for getId states

/**
  * Returns the identifier of this Thread.  The thread ID is a positive
  * <tt>long</tt> number generated when this thread was created.
  * The thread ID is unique and remains unchanged during its lifetime.
  * When a thread is terminated, this thread ID may be reused.
  *
  * @return this thread's ID.
  * @since 1.5
  */
public long getId() {
    return tid;
}

and

tid = nextThreadID();

private static synchronized long nextThreadID() {
    return ++threadSeqNumber;
}

The JVM has internal threads so the first thread you create might be the 9th thread. Note: main is a thread.

Then i am also having doubt with "Thread-0" of t1 and "Thread-1" of t2, is this the initial value of getName() method?

Similarly the default name for a thread is generated when you create it

/**
 * Allocates a new {@code Thread} object. This constructor has the same
 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 * {@code (null, target, gname)}, where {@code gname} is a newly generated
 * name. Automatically generated names are of the form
 * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 *
 * @param  target
 *         the object whose {@code run} method is invoked when this thread
 *         is started. If {@code null}, this classes {@code run} method does
 *         nothing.
 */
public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
}

Upvotes: 2

Jens Schauder
Jens Schauder

Reputation: 81907

It gets generated in the method nextId, which just counts up.

Some snippets from the source code.

public long getId() {
    return tid;
}

private static synchronized long nextThreadID() {
    return ++threadSeqNumber;
}


private void init(ThreadGroup g, Runnable target, String name,
                  long stackSize) {
    // lots of other stuff
    tid = nextThreadID();
}

public Thread() {
    init(null, null, "Thread-" + nextThreadNum(), 0);
}

Other constructors do similar calls to init

Upvotes: 1

Related Questions