new Thread().start(); } System.out.println("value of i is " + i);
we have synchorized void run() means Synchronize(this) lock based on current object when the for loop executes it creates five threads and calls start() which intern calls run() since it is synchorized only one thread can access i++ and finally it will print 5 am i correct but the answer to this question was different my doubt is that doesnt Synchronize(this) block two threads in the above code.
testthread is never run, since the code allocates 5 Thread objects, not 5 testthread objects. Replace "new Thread()" with "new testthread()", and see what happens.
Even then, what this code does is not necessarilty what you may think it does. The JVM may or may not run any of the threads before the println statement of the main method is executed, so any value of i between 0 and 5 could be printed.
That means to say synchronized(this) will not block the threads created in the for loop can any one please explain the way "synchronized(this)" and "synchronized(class literal)" behaves and any links on this will be very help full