Thread safety for static variables

前端 未结 4 1459
天涯浪人
天涯浪人 2021-02-19 03:12
class ABC implements Runnable {
    private static int a;
    private static int b;
    public void run() {
    }
}

I have a Java class as above. I hav

4条回答
  •  死守一世寂寞
    2021-02-19 03:31

    Depends on what needs to be thread-safe. For these int primitives, you'll need either to replace them with AtomicInteger's or only operate with them within synchronized method or block. If you need to make your cross-thread Hashtable thread-safe, you don't need to do anything, as it already is synchronized.

提交回复
热议问题