thread_local.htm

import java.util.*;
import java.io.*;
//javac thread_synchro.java
public class test implements Runnable
{
static int voter_out;
public test()
{
//constructor
//voter_out++;
}
//create an object to hold local value
static ThreadLocal t_l = new local_nested();
//
static private class local_nested extends ThreadLocal
{
private static int voter_no = 1;
protected synchronized Object initialValue()
{
//voter_no = voter_out;
return new Integer (voter_no++);
}
}
//
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type location : ");
String str = br.readLine();
System.out.println("Welcome to" + str+ " Voting Zone ");
test t_main = new test();
for(int i = 1; i < 6; i++)
{
Thread t = new Thread(t_main);
t.start();
System.out.println("main thread " + i + " id : " + t.getId());
}
}
//mandatory run method for test implementing runnagble
public void run()
{
test t = new test();
try {
Thread.currentThread().sleep(1000);
t.show_values();
} catch (InterruptedException e) {
e.printStackTrace();
}
}//end void run
private void show_values()
{
String str = Thread.currentThread().getName();
System.out.println("\t\tThread Name: " + str + " show_values " + t_l.get() + " threas id : " + Thread.currentThread().getId() );
}
//end of nested inner class
}//end of supper class