Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.HashMap;

public class Main {
    private static final ThreadLocal s_var = new ThreadLocal();

    public static void init() {
        if (s_var.get() != null) {
            throw new IllegalStateException(
                    "There is already a thread local exists, call init() already or forgot to call cleanup()???");
        } else {
            HashMap map = new HashMap(4);
            s_var.set(map);
            return;
        }
    }

    public static Object get(String key) {
        HashMap map = (HashMap) s_var.get();
        if (map == null)
            throw new IllegalStateException("Thread local not exists!");
        else
            return map.get(key);
    }

    public static void set(String key, Object obj) {
        HashMap map = (HashMap) s_var.get();
        if (map == null)
            throw new IllegalStateException("Thread local not exists!");
        if (obj == null)
            map.remove(key);
        else
            map.put(key, obj);
    }
}