can anyone please tell me why the synchronised keyword is not working.
package Threading;
class NewThreadt extends Thread {
synchronized void dota(int a){
System.out.println(a);
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
dota(i) ;
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
System.out.println(Thread.currentThread());
}
}
class abc {
public static void main(String args[]) {
NewThreadt t=new NewThreadt();
NewThreadt q=new NewThreadt();
t.start();
q.start();
System.out.println("Main thread exiting.");
}
}
Output I am getting on executing above program:
5
5
4
4
3
3
2
2
1
1
The output that I want:
5
4
3
2
1
5
4
3
2
1
dota()
method assynchronized
only ensures that each call todota()
blocks any other call todota()
on the same instance ofNewThreadt
. It doesn't remotely do what you seem to think it does.