public?class?SecondThread?implements?Runnable?{
????int?i;
????@Override
????public?void?run()?{
????????for(;i<20;i++){
????System.out.println(Thread.currentThread().getName()+"-"+i);
}
????}
????public?static?void?main(String[]?args)?{
for(int?i=0;i<20;i++){
????System.out.println(Thread.currentThread().getName()+"-"+i);
????if(i==10){
????????SecondThread?st=new?SecondThread();
????new?Thread(st,"線程1").start();
????new?Thread(st,"線程2").start();
????}
}
????}
}輸出結果中 線程1和線程2后面的i應該是共享的并且連續(xù)的,如線程2-0、線程1-1、線程1-2、線程2-3、線程1-4、線程1-5、線程2-6...但我跑了很多次,發(fā)現(xiàn)每次線程1和線程2都會輸出0:線程1-0,線程2-0。但后面的數字就是連續(xù)交替的的,為什么偏偏這個0都會輸出一次?
添加回答
舉報
0/150
提交
取消