Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在执行两个线程的情况下获取输出_Java_Multithreading_Scjp - Fatal编程技术网

Java 在执行两个线程的情况下获取输出

Java 在执行两个线程的情况下获取输出,java,multithreading,scjp,Java,Multithreading,Scjp,以下是来自SCJP dump的一个问题: public class Threads1 { int x=0; public class Runner implements Runnable{ public void run(){ int current=0; for (int i=0; i<4; i++){ current = x;

以下是来自SCJP dump的一个问题:

public class Threads1 {
     int x=0;
     public class Runner implements Runnable{
           public void run(){
               int current=0;
               for (int i=0; i<4; i++){
                   current = x;
                   System.out.print(current + ',');
                   x=current +2;
               }
           }
     }

     public static void main(String[] args){
          new Threads1().go();
     }

     public void go(){
          Runnable r1 = new Runner();
          new Thread(r1).start();
          new Thread(r1).start(); 
     }
}
公共类线程1{
int x=0;
公共类运行器实现可运行{
公开募捐{
int电流=0;

对于(int i=0;iA-可能,如果一个线程打印0,2,则两个线程都将4保存到当前,并将6保存到x(两者),然后两个线程都将6保存到当前,第二个线程将结束-打印6,8,10,然后第一个线程打印6。

因为有两个线程同时运行

It might result in the following at every iteration you are adding +2
thread 1- 0 2 4 6 8 10
thread 2-     4        6