Why syncronization by static variable doesn't work?
public class ThreadSynchronizationTest {
public static void main(String[] args) throws Exception {
ExtendsThread extendsThread = new ExtendsThread();
Thread et1 = new Thread(extendsThread);
Thread et2 = new Thread(extendsThread);
et1.start();
et2.start();
et1.join();
et2.join();
System.out.println(extendsThread.getCount());
}
}
public class ExtendsThread extends Thread {
private Integer count = 0;
public void run() {
synchronized (count)
{
for (int i = 0; i < 500000; i++)
count++;
}
}
public Integer getCount() {
return count;
}
}
I know I can synchronize by (this), or add and sinchronize by private static final Object lock= new Object();
, but can't understand why this solution incorrect