Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ThreadLocals With Singletons

807597Jan 17 2005 — edited Jan 18 2005
We are using a 3rd party product here. This product runs in its own JVM. We can customize it by subclassing some of their classes. But their architecture will only create one instance of our subclasses. This instance is reused for multiple requests.

My subclass is pretty complicated and is just an abstract class for numerous other subclasses. I need to make my class thread safe. My first inclination is to use a ThreadLocal class variable to hold the state for each instance of my class.

I've never used ThreadLocal before, so I wrote a little test. As I haven't used Java Threads in years, I am having trouble getting this sample code to work.

The sample essentially starts 2 threads. The 1st thread is supposed to wait 4 seconds so the 2nd thread can start running and I can test 2 threads in the same instance to make sure I get two separate serial numbers.

The problem is, for some reason the 1st thread runs t o completion before the 2nd thread starts. I don't know why. I'm sure I'm an idiot and the solution is very simple. But I can't figure it out and would appreciate any help.

Thanks.

Here is the thread creator/driver:
public class ThreadMgr
{
    public void runIt()
    {
        ThreadTest t1 = ThreadTest.getInstance();
        ThreadTest t2 = ThreadTest.getInstance();
        System.out.println("t1 == t2: " + (t1 == t2));

        System.out.println("Start A");
        Thread threadA = new Thread(t1);
        threadA.setName("ThreadA");
        threadA.run();

        System.out.println("A has been started");
        System.out.println("Start B");
        Thread threadB = new Thread(t2);
        threadB.setName("ThreadB");
        threadB.run();
        System.out.println("B has been started");

    }

    public static void main(String[] args)
    {
        new ThreadMgr().runIt();

    }
Here is the singleton which I need to ensure is threadsafe:
public class ThreadTest implements Runnable
{
    private ThreadLocal serialNumber = null;
    private static ThreadTest threadTest = null;

    public static ThreadTest getInstance()
    {
        if (threadTest == null)
        {
            threadTest = new ThreadTest();
        }
        return threadTest;
    }

    private ThreadTest()
    {

    }

    public void run()
    {
        System.out.println("In ThreadTest");

        serialNumber = new ThreadLocal();
        Double randomNbr = new Double(Math.random());
        serialNumber.set(randomNbr); //Just for testing

        try
        {
            Thread.sleep(4000);  //Sleep 4 seconds to allow the other thread to enter
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println("Serial # for thread " + Thread.currentThread().getName() + " = " + serialNumber.get());

    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2005
Added on Jan 17 2005
3 comments
104 views