Skip to main content

What are the pros and cons of ThreadLocal

Review the code below.  What would you worry about if this code has to be productionized.

package com.javahowdoi.challengeq;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadLocalQ {
    public ThreadLocal<Long> tli = new ThreadLocal<>();

    private class TLQInner implements Runnable{

        @Override
        public void run() {
            try {
                tli.set(Thread.currentThread().getId());
                Thread.sleep(1000);
            } catch(Exception e) {}
        }
    }

    public TLQInner getInner() {
        return new TLQInner();
    }

    public static void main(String[] args ) {
        try {
            ThreadLocalQ tlq = new ThreadLocalQ();
            ExecutorService es = Executors.newFixedThreadPool(2);
            es.submit(tlq.getInner());
            es.submit(tlq.getInner());
            es.awaitTermination(2, TimeUnit.SECONDS);
            es.shutdown();
        } catch(Exception e ) {}
    }
}

Here's an answer

Comments