Thread's 'getId()' Problem

Hello All,

I try to use the ID of threads withoud any success.

I declare two threads in this way:

Thread threads[] =new Thread[2];

threads[0] =new Thread();

threads[1] =new Thread();

for(int i=0;i<2;i++)

threads[i].start();

then, when I look for its ID's, I get for both '1' (is it the 'main' ID?)

// from the method which the thread has activated

System.out.printf("Thread's ID is: %d",Thread.currentThread().getId());

Is there any way to change the ID of a thread?

Thanks :)

Message was edited by:

drortrie@

[835 byte] By [drortrie@a] at [2007-11-26 23:44:31]
# 1

>// from the method which the thread has activated

>System.out.printf("Thread's ID is: %d",Thread.currentThread().getId());

Shouldn't there be a "+" in the system.out.println() rather than a comma?

It might be that the last thrad thats runs is threads[1] because of the for loop, not sure though? What happens if you don't use a for loop?

abshirf2a at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...
# 2

> // from the method which the thread has activated

I don't believe the code which followed this comment was actually in a method being executed by the seperate threads, which is why it showed the same ID twice. Why don't you actually show where you placed that code?

Edit: Actually I'm positive you didn't, because since you created base Thread objects (not subclasses of them nor constructed them with your own Runnable objects), the only method the thread would have executed in its own thread was Thread.run(), which does nothing. There's no way you could have executed that code in its own thread.

Here's a simple example which would do what you want:

public class ThreadExample extends Thread

{

public void run()

{

// This really is from the method which the thread has activated

System.out.printf("Thread's ID is: %d",Thread.currentThread().getId());

}

public static void main(String[] args)

{

Thread[] threads = new Thread[2];

for (int i = 0; i < threads.length; ++i)

{

threads[i] = new ThreadExample();

threads[i].start();

}

}

}

Note: I did not actually compile the above.

Note 2: The above is not necessarily recommended practice. You would be advised to not extend Thread but create implementations of Runnable instead.

Message was edited by:

warnerja

warnerjaa at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...
# 3

Thread.currentThread() will return the current, um, thread. the two threads are created in a method that is running in the main thread of execution, and since you call Thread.currentThread() from that thread, that's the ID you're getting back. what makes you think you're calling that method from inside another thread? there's no code you've posted that would do that

georgemca at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...
# 4

> >// from the method which the thread has activated

> >System.out.printf("Thread's ID is:

> %d",Thread.currentThread().getId());

>

> Shouldn't there be a "+" in the system.out.println()

> rather than a comma?

no. it's printf, not println. a comma is perfectly acceptable

georgemca at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...
# 5

Interesting, i tired it and got the following:

public class Final{

public static void main(String [] args){

Thread threads[] = new Thread[2];

threads[0] = new Thread();

threads[1] = new Thread();

for(int i=0;i<2;i++){

threads[ i ].start();}

// from the method which the thread has activated

System.out.println("Thread's ID is: " + Thread.currentThread().getId()); //returns Thread's ID is: 1

System.out.println("Thread = " + threads[0].getId()); //returns Thread = 7

}

}

Like what has been said already it returns 1 because its been called from the main method and its returing that threads ID, ....i think!

Message was edited by:

abshirf2

Message was edited by:

abshirf2

Message was edited by:

abshirf2

abshirf2a at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...
# 6

I try to implement Peterson Algorithm (which uses the threads' ID)

I extended 'Thread' class, here's 'start()'

public void start()

{

int i=0;

while (i<5)

{counter.inc();

i++;

}

}

I have a shared counter for both threads. (I have a constructor which handle it)

class Counter

{

long value=0;

Peterson lock = new Peterson();

public void inc(){

lock.lock();

try{

long temp;

temp = value+1;

value = temp;

} finally{

lock.unlock();}

}

}

and the 'lock()' code:

public void lock(){

int i = (int) Thread.currentThread().getId();

System.out.println(i);

int j = 1-i;

flag[i] = true;

victim = i;

while (flag[j] && victim==i){}; // WAIT HERE

}

Probably you are right, because I get '1' in 'System.out.printf(i)'.

drortrie@a at 2007-7-11 15:16:02 > top of Java-index,Java Essentials,Java Programming...