问题描述
- 使用继承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()方法吗
大声告诉我:
synchronized
的作用对象是什么?!