How to assign a variable to Threads

S

Sanny

I want to assign a unique integer value to each thread.

That integer value is needed to keep track which thread is currently
running.

callfunction1() Called by Thread 1.
callfunction1() Called by Thread 2.
callfunction1() Called by Thread 3.
callfunction1() Called by Thread 4.

Now I want to know in function which thread is Called

if Thread 1 then function will use array[1] value;
if Thread 2 then function will use array[2] value;
if Thread 3 then function will use array[3] value;
if Thread 4 then function will use array[4] value;

How to know in the function the Thread Number.

I want someway to assign an Unique Integer to each thread

Bye
Sanny
 
A

Andrea Francia

Sanny said:
I want to assign a unique integer value to each thread.

That integer value is needed to keep track which thread is currently
running.

callfunction1() Called by Thread 1.
callfunction1() Called by Thread 2.
callfunction1() Called by Thread 3.
callfunction1() Called by Thread 4.

Now I want to know in function which thread is Called

if Thread 1 then function will use array[1] value;
if Thread 2 then function will use array[2] value;
if Thread 3 then function will use array[3] value;
if Thread 4 then function will use array[4] value;

How to know in the function the Thread Number.

I want someway to assign an Unique Integer to each thread

Bye
Sanny
See the docs of ThreadLocal:
http://java.sun.com/javase/6/docs/api/java/lang/ThreadLocal.html

The relevant portion of the page is reported below:
>For example, the class below generates unique identifiers local to
each >thread. A thread's id is assigned the first time it invokes
 
S

Sanny

 >     private static final ThreadLocal < Integer > uniqueNum =
 >         new ThreadLocal < Integer > () {
 >             @Override protected Integer initialValue() {
 >                 return uniqueId.getAndIncrement();

I am using Java 1.3 which do not use Generics.

< Integer > will not be recognized in java1.3 giving compiler error.

Is there any way to do that Manually???

I want to bot set and reset the Values of Thread Number.

Bye
Sanny
 
A

Andrea Francia

Sanny said:
I am using Java 1.3 which do not use Generics.

< Integer > will not be recognized in java1.3 giving compiler error.

Is there any way to do that Manually???

I want to bot set and reset the Values of Thread Number.

Generics are just something more of syntactic sugar that prevent you
from using casting.
Every example that uses generics can be rewritten without them adding
some casting. The only problem is that you lose the compile time type
checking.
This is the version without generics (and without autoboxing):

import java.util.concurrent.atomic.AtomicInteger;

public class UniqueThreadIdGenerator {

private static final AtomicInteger uniqueId = new AtomicInteger(0);

private static final ThreadLocal uniqueNum =
new ThreadLocal () {
@Override protected Object initialValue() {
return uniqueId.getAndIncrement();
}
};

public static int getCurrentThreadId() {
return ((Integer)uniqueNum.get()).intValue();
}
} // UniqueThreadIdGenerator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top