package concurrent; import java.util.*; import java.util.concurrent.*; import java.text.SimpleDateFormat; class ThreadLocalTask implements Runnable { private ThreadLocal startDate = new ThreadLocal() { protected String initialValue() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()); } }; public void run() { String id = String.format("%04d", Thread.currentThread().getId()); System.out.println("Thread " + id + " created at " + startDate.get() + " starts"); } } public class ThreadLocalDemo { public static void main(String[] args) throws Exception { ThreadLocalTask task = new ThreadLocalTask(); for (int idx = 0; idx < 10; idx++) { Thread thread = new Thread(task); thread.start(); TimeUnit.SECONDS.sleep(1); } TimeUnit.SECONDS.sleep(2); } }