U
UFO
hello,
i would like to know how to use a similar feature that exist on Java, on my
C# program.
basically , the ThreadLocal allows you to decare&use a variable to be set
per thread, without the need to assign it from outside the thread itself.it
means that whoever uses the code doesn't even know that each thread he
creates , when it uses the class, it gets a new variable just for this thread.
for example, if i want to have a threadID , so that each new thread will
have a new number (from 0 , ascending) , i could use the next code on Java:
// Assigns unique contiguous ids to threads.
public class ThreadID
{
// The next thread ID to be assigned
private static AtomicInteger nextID =new AtomicInteger(0);
// My thread-local ID.
private static ThreadLocalID threadID =new ThreadLocalID();
// return unique ID for this thread
public static int get()
{
return threadID.get();
}
// Reset this thread's ID.the parameter is the index new ID
public static void set(int index)
{
threadID.set(index);
}
// Assign new IDs from zero.
public static void reset()
{
nextID.set(0);
}
public static int getNumberOfRegisteredThreads()
{
return nextID.get();
}
private static class ThreadLocalID extends ThreadLocal<Integer>
{
protected Integer initialValue()
{
return nextID.getAndIncrement();
}
}
}
so,for the first thread that uses this class by calling the 'get' function,
it will always (for all the next times that it calls this function) return 0.
for the second thread, it will always return 1 , and so on...
i would like to know how to use a similar feature that exist on Java, on my
C# program.
basically , the ThreadLocal allows you to decare&use a variable to be set
per thread, without the need to assign it from outside the thread itself.it
means that whoever uses the code doesn't even know that each thread he
creates , when it uses the class, it gets a new variable just for this thread.
for example, if i want to have a threadID , so that each new thread will
have a new number (from 0 , ascending) , i could use the next code on Java:
// Assigns unique contiguous ids to threads.
public class ThreadID
{
// The next thread ID to be assigned
private static AtomicInteger nextID =new AtomicInteger(0);
// My thread-local ID.
private static ThreadLocalID threadID =new ThreadLocalID();
// return unique ID for this thread
public static int get()
{
return threadID.get();
}
// Reset this thread's ID.the parameter is the index new ID
public static void set(int index)
{
threadID.set(index);
}
// Assign new IDs from zero.
public static void reset()
{
nextID.set(0);
}
public static int getNumberOfRegisteredThreads()
{
return nextID.get();
}
private static class ThreadLocalID extends ThreadLocal<Integer>
{
protected Integer initialValue()
{
return nextID.getAndIncrement();
}
}
}
so,for the first thread that uses this class by calling the 'get' function,
it will always (for all the next times that it calls this function) return 0.
for the second thread, it will always return 1 , and so on...