LQW_java_home
LQW_home
2017-08-29 10:02

下面代码有线程问题吗?

  • 线程

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);

    }
}

}


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答