伙计们i9知道这是假问题,但我是初学者..我有这个课程

public class threadLocal {
private static ThreadLocal<String> myThreadLocal;

public threadLocal(){
    myThreadLocal = new ThreadLocal<String>();
}

public static ThreadLocal<String> getMyThreadLocal() {
    return myThreadLocal;
}

public static void setMyThreadLocal(ThreadLocal<String> myThreadLocal) {
    threadLocal.myThreadLocal = myThreadLocal;
}
}


我想在另一个班级这样称呼它

myThreadLocal.setMyThreadLocal("patrick");


所以我应该在threadLocal类中做哪些更改?

最佳答案

我认为您应该以这种方式重写您的课程。

public class threadLocal {
 private static ThreadLocal<String> myThreadLocal = new ThreadLocal<String>();

 public static String getMyThreadLocal() {
    return myThreadLocal.get();
  }

 public static void setMyThreadLocal(String str) {
    myThreadLocal.set(str);
  }
}


打电话时,您可以打电话

threadLocal.getMyThreadLocal()

10-08 01:12