Multiple Threads calling static helper method

前端 未结 7 665
孤街浪徒
孤街浪徒 2021-02-08 03:05

I have a web application running on Tomcat.

There are several calculations that need to be done on multiple places in the web application. Can I make those calculations

7条回答
  •  忘了有多久
    2021-02-08 03:29

    If you don't have to share it you can make it thread local, then it doesn't have to be thread safe.

    public class Helper {
    
    private static final ThreadLocal obj = new ThreadLocal() {
        public SomeObject initialValue() {
            return enw SomeObject();
        }
    }
    
    public static void changeMember() {
        Helper.obj.get().changeValue();
    }
    
    public static String readMember() {
        Helper.obj.get().readValue();
    }
    
    }
    

提交回复
热议问题