- create a thread instance using the inherited Thread method
- when overriding the run () method, modify the run () method with the synchronized keyword
- the result is that the threads are not carried out by Synchronize, and there is a problem with the calculation result
public class MyThreadDemo9 extends Thread{
private static int count = 0;
public String status;
public MyThreadDemo9(String status){
this.status = status;
}
@Override
public synchronized void run() {
for(int i = 0; i < 100; iPP) {
countPP;
}
}
public static void main(String[] args) throws InterruptedException {
for(int i = 0; i < 100; iPP) {
MyThreadDemo9 m8 = new MyThreadDemo9("Thread status - " + i);
m8.start();
}
Thread.sleep(3000);
System.out.println(MyThreadDemo9.count);
}
}
what is the reason for this? can"t you modify the run () method with the synchronized keyword