在下面的代码片段中,将 doThings() 方法声明为静态将使类成为线程安全的。这是因为如果启动了多个 TestSeven 线程并且由于 x 是静态变量,可能会发生竞争条件吗?
public class TestSeven extends Thread{
private static int x;
public synchronized void doThings(){
int current = x;
current++;
x = current;
}
public void run(){
doThings();
}
public static void main(String args[]){
TestSeven t = new TestSeven();
Thread thread = new Thread(t);
thread.start();
}
}