I have a problem about Timer in java. When I run the code below, a.start(0), a.start(1) and a.start(2) are being printed at the same time and the output is like 02121010120010122012102 but I want to be printed in order like 00000001111111222222222
How to do this?
public class Main {
public static void main(String[] args) {
A a = new A();
a.start(0);
a.start(1);
a.start(2);
}
public class A {
public void start(int x)
{
Timer myTimer=new Timer();
TimerTask task=new TimerTask() {
int counter=0;
@Override
public void run() {
System.out.print(x);
counter++;
if(counter>=10)
myTimer.cancel();
}
};
myTimer.schedule(task,0,300);
}
}