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