使用继承Thread方法创建线程实例
重写run()方法的时候,用synchronized关键字修饰run()方法
结果是线程之间不是同步进行,计算结果出现问题
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; i++) {
count++;
}
} public static void main(String[] args) throws InterruptedException { for(int i = 0; i < 100; i++) {
MyThreadDemo9 m8 = new MyThreadDemo9("Thread status - " + i);
m8.start();
}
Thread.sleep(3000);
System.out.println(MyThreadDemo9.count);
}
}这个是什么原因呢,不能使用 synchronized 关键字修饰run()方法吗
青春有我
慕雪6442864
随时随地看视频慕课网APP
相关分类