我决定面对这个叫做并发的怪物并拓宽我的线程知识,所以在我阅读 Joshua Bloch 的书之前,我决定随机编写一些代码来帮助我理解我在阅读这本书之前可能遇到的问题,希望我能来我的代码并进行更正,但后来我陷入了这个坑,我希望有人能解释一下。
我有以下内容:
public class Arithmetic implements Runnable {
    int number;    
    public  Arithmetic(int x){      
      this.number = number + x;
    }
    @Override
    public void run() {         
        Thread.currentThread().setName("thread-"+number +" > " + "number = "+getNumber());
        System.out.println(Thread.currentThread().getName());        
    }    
        public  int getNumber() {
        return number;
    }
    public  void setNumber(int number) {
        this.number = getNumber() + number;
    }
}
然后是一个主类:
public class Main {  
    public static void main(String[] args) {    
      System.out.println(Thread.currentThread().getName());     
      for (int x=0; x<=5; x++){    
           Thread thread = new Thread(new Arithmetic(x));           
           thread.start();       
      }
    }
}
有了这个,我得到以下输出:
run:
main
thread-0 > number = 0
thread-1 > number = 1
thread-2 > number = 2
thread-3 > number = 3
thread-5 > number = 5
thread-4 > number = 4
注意:5 在 4 之前
但是后来我将我的主要课程更改为:
public class Main {  
    public static void main(String[] args) {    
      System.out.println(Thread.currentThread().getName());     
      for (int x=0; x<=5; x++){    
           Runnable runnable  = new Arithmetic(x);           
          runnable.run();      
      }
    }
}
我得到下面的输出:
run:
main
thread-0 > number = 0
thread-1 > number = 1
thread-2 > number = 2
thread-3 > number = 3
thread-4 > number = 4
thread-5 > number = 5
注意:正确的顺序
我希望两个 main(s) 都会产生不稳定的结果(如线程实现),然后我会使用一些线程安全措施,如同步访问等,但为什么 Runnable 调用就像Arithmetic线程安全一样?
IMO,扩展 Thread 类和实现 Runnable 之间的区别是出于解耦目的。抱歉,如果这是一个重复的问题,我似乎找不到答案。
提前致谢。