Lets say I have code like this:
class A extends Thread {
Thread t = new Thread();
private static int id = 0;
A(){
id++;
this.t.start();
}
private synchronized static int getId() { return id; }
public void run() { System.out.println(getId()); }
}
public class Test throws InterruptedException {
public static void main(String[] args) {
A thread1 = new A();
A thread2 = new A();
try {
thread1.t.join();
thread2.t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread ends here");
}
}
I'm expecting the output like this:
1
2
But the output is:
2
2
What should I do here? Thanks for help. Q.