两个文件
第一个package shop.ljsp.learn.onebase.hunsafethreadprintln;public class MyThread extends Thread {
private int i=5; @Override
public void run() {
System.out.println("i="+(i--)+" threadName="+Thread.currentThread().getName());
}
}第二个文件主文件package shop.ljsp.learn.onebase.hunsafethreadprintln;public class run {
public static void main(String[] args) {
MyThread run =new MyThread();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
Thread t3=new Thread(run);
Thread t4=new Thread(run);
Thread t5=new Thread(run);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
运行第二个主文件后输出结果:
i=5 threadName=Thread-2
i=4 threadName=Thread-3
i=3 threadName=Thread-1
i=2 threadName=Thread-5
i=1 threadName=Thread-4
书里边显示的输出结果
i=5 threadName=Thread-1
i=2 threadName=Thread-5
i=3 threadName=Thread-4
i=4 threadName=Thread-3
i=4 threadName=Thread-2
请问这是什么原因造成的
第一个package shop.ljsp.learn.onebase.hunsafethreadprintln;public class MyThread extends Thread {
private int i=5; @Override
public void run() {
System.out.println("i="+(i--)+" threadName="+Thread.currentThread().getName());
}
}第二个文件主文件package shop.ljsp.learn.onebase.hunsafethreadprintln;public class run {
public static void main(String[] args) {
MyThread run =new MyThread();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
Thread t3=new Thread(run);
Thread t4=new Thread(run);
Thread t5=new Thread(run);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
运行第二个主文件后输出结果:
i=5 threadName=Thread-2
i=4 threadName=Thread-3
i=3 threadName=Thread-1
i=2 threadName=Thread-5
i=1 threadName=Thread-4
书里边显示的输出结果
i=5 threadName=Thread-1
i=2 threadName=Thread-5
i=3 threadName=Thread-4
i=4 threadName=Thread-3
i=4 threadName=Thread-2
请问这是什么原因造成的
i=5 threadName=Thread-1
i=4 threadName=Thread-3
i=3 threadName=Thread-4
i=2 threadName=Thread-2
i=1 threadName=Thread-5
又来一次
i=5 threadName=Thread-1
i=3 threadName=Thread-2
i=4 threadName=Thread-4
i=2 threadName=Thread-5
i=1 threadName=Thread-3