1. public class X implements Runnable(
2. private int x;
3. private int y;
4.
5. public static void main(String[]args)
6. { X that = new X();
7.   (new Thread(that)).start();
8.   (new Thread(that)).start();
9. }
10.
11. public void run() {
12. for (;;) {
13. x++;
14. y++;
15. System.out.printIn(“x=” + x + “, y = ” + y);
16. }
17. }
18. }
请问输出为什么是如下的答案:
D. The program prints pairs of values for x and y that are always the same on the
same line (for example, “x=1, y=1”. In addition, each value appears only for
once (for example, “x=1, y=1” followed by “x=2, y=2”).我个人感觉是X和Y的值有可能是不一样的

解决方案 »

  1.   

    其实自始至终一直都是在第7行的线程再执行,它是死循环,所以第8行的线程在一直等待,你说的x和y的值一样是因为,x和y的值都默认为0。
    我不明白的是:
    class X 
    {
     public static void main(String[] args)
     { 
       int x;
       int y;
       System.out.println("x=" + x + ", y = " + y);
     }
    }
    //在这个程序中,编译会出现x和y没有初始化的异常,但是:
    class X 
    {
     private static int x;
     private static int y;
     public static void main(String[] args)
     { 
       
       System.out.println("x=" + x + ", y = " + y);
     }
    }//它编译可以通过。这个程序中为什么不需要初始化,
      

  2.   

    楼上的解释有错误,最好你自己也测试一下,两个线程都会启动。至于楼主的疑问
    可以由程序里得到结果,确实会有不同的值产生。
    public class X
        implements Runnable {
      private int x;
      private int y;
    int i = 0;
      public static void main(String[] args) {
        X that = new X();
        (new Thread(that)).start();
        (new Thread(that)).start();
      }  public void run() {
        System.out.println(i++ + "++++++++++++++++++++++");
          for (; ; ) {
            x++;
            y++;
            if(x != y)
            System.out.println("x=" + x + ", y = " + y);
        }
      }
    }楼上的问题,我问了下同事,他说对于全局变量系统会有一个默认的初始值,对于局部变量JAVA会强制性的需要赋初始值,否则编译不通过
      

  3.   


    x=180037114, y = 180037120x=180037115, y = 180037121x=180037116, y = 180037122x=180037117, y = 180037123x=180037118, y = 180037124x=180037119, y = 180037125x=180037120, y = 180037126x=180037121, y = 180037127x=180037122, y = 180037128x=180037123, y = 180037129x=180037124, y = 180037130x=180037125, y = 180037131x=180037126, y = 180037132x=180037127, y = 180037133x=180037128, y = 180037134x=180037129, y = 180037135x=180037130, y = 180037136x=180037131, y = 180037137x=180037132, y = 180037138x=180037133, y = 180037139x=180037134, y = 180037140x=180037135, y = 180037141x=180037136, y = 180037142x=180037137, y = 180037143x=180037138, y = 180037144x=180037139, y = 180037145x=180037140, y = 180037146x=180037141, y = 180037147x=180037142, y = 180037148x=180037143, y = 180037149x=180037144, y = 180037150x=180037145, y = 180037151x=180037146, y = 180037152x=180037147, y = 180037153x=180037148, y = 180037154x=180037149, y = 180037155x=180037150, y = 180037156x=180037151, y = 180037157x=180037152, y = 180037158x=180037153, y = 180037159x=180037154, y = 180037160x=180037155, y = 180037161x=180037156, y = 180037162x=180037157, y = 180037163x=180037158, y = 180037164x=180037159, y = 180037165x=180037160, y = 180037166x=180037161, y = 180037167x=180037162, y = 180037168x=180037163, y = 180037169x=180037164, y = 180037170x=180037165, y = 180037171x=180037166, y = 180037172x=180037167, y = 180037173x=180037168, y = 180037174x=180037169, y = 180037175x=180037170, y = 180037176x=180037171, y = 180037177x=180037172, y = 180037178x=180037173, y = 180037179x=180037174, y = 180037180
    这是部分结果