下面代码有线程问题吗?

public class TestObject {

private static int num = 0;

public static int getNum(){
    Thread th=Thread.currentThread();
    try {
        th.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if(num<100){
        return num++;
    }
    if(num == 100){
        num = 0;
        return 100;
    }
    return 0;
}

}


public class MainObj {
public static void main(String[] args) {

    Thread t = new Thread(new Thr());
    Thread t1 = new Thread(new Thr());
    t.start();
    t1.start();
}

}
class Thr implements Runnable{

@Override
public void run() {
    while (true) {
        int num = TestObject.getNum();
        System.out.println(Thread.currentThread().getName()+":"+num);

    }
}

}


你想问的应该是是否会有线程安全问题是吧,你启动了两个线程t 和 t1 他们都能同时访问getNum()这个方法
而这个方法会调用num这个静态变量 对t和t1这两个线程来说 num是共有的 也就是说 加入t在给num++的操作
而这个时候t1已经把num读取了正准备++这个时候就有线程安全问题了

getNum没有同步,比如
if(num<100){
return num++;
}
可能在判断之后,另一个线程把num++了,或者两个线程同时++导致num被少加等等