0

我有一个非常简单的 Java 代码:

public class Main {

    public static void main(String[] args) {

        int c = 0;

        while (c < 10);
        {
            System.out.println(c);
            c++;
        }

        System.out.println("exit");
    }

}

问题是,while 循环没有启动,程序只是在第 7 行(在 while 循环的开头)一直等待。

我在 Windows 上使用Oracle Java JDK 1.6u33Xubuntu 12.04.代码完美运行。有人可以告诉我,我该怎么做才能在 Ubuntu 上正确运行此代码?

我做了什么(和线程转储):

gopher@orion:~/Projects/test/src$ javac Main.java
gopher@orion:~/Projects/test/src$ jar cvfm test.jar manifest.txt Main.class
added manifest
adding: Main.class(in = 489) (out= 338)(deflated 30%)
gopher@orion:~/Projects/test/src$ java -jar test.jar
^\2012-08-04 10:26:23
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.8-b03 mixed mode):

"Low Memory Detector" daemon prio=10 tid=0x00007fbf080b4800 nid=0xa26 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x00007fbf080b2800 nid=0xa25 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x00007fbf080af800 nid=0xa24 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x00007fbf080ad800 nid=0xa23 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x00007fbf08091000 nid=0xa22 in Object.wait() [0x00007fbf0d1f3000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000f5b61300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
    - locked <0x00000000f5b61300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=0x00007fbf0808f000 nid=0xa21 in Object.wait() [0x00007fbf0d2f4000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000f5b611d8> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
    - locked <0x00000000f5b611d8> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0x00007fbf08007800 nid=0xa1d runnable [0x00007fbf0eb4b000]
   java.lang.Thread.State: RUNNABLE
    at Main.main(Main.java:8)

"VM Thread" prio=10 tid=0x00007fbf08088800 nid=0xa20 runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007fbf0801a800 nid=0xa1e runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007fbf0801c000 nid=0xa1f runnable 

"VM Periodic Task Thread" prio=10 tid=0x00007fbf080bf000 nid=0xa27 waiting on condition 

JNI global references: 887

Heap
 PSYoungGen      total 9216K, used 317K [0x00000000f5b60000, 0x00000000f65a0000, 0x0000000100000000)
  eden space 7936K, 4% used [0x00000000f5b60000,0x00000000f5baf628,0x00000000f6320000)
  from space 1280K, 0% used [0x00000000f6460000,0x00000000f6460000,0x00000000f65a0000)
  to   space 1280K, 0% used [0x00000000f6320000,0x00000000f6320000,0x00000000f6460000)
 PSOldGen        total 21056K, used 0K [0x00000000e1200000, 0x00000000e2690000, 0x00000000f5b60000)
  object space 21056K, 0% used [0x00000000e1200000,0x00000000e1200000,0x00000000e2690000)
 PSPermGen       total 21248K, used 2774K [0x00000000dc000000, 0x00000000dd4c0000, 0x00000000e1200000)
  object space 21248K, 13% used [0x00000000dc000000,0x00000000dc2b5bb8,0x00000000dd4c0000)
4

2 回答 2

4

你有一个;在一段时间结束时,删除它,它会工作

while (c < 10);

以上将执行单行 while() 并且因为c从不增加它是一个无限循环。把它改成

while (c < 10) // no ; at the end
于 2012-08-04T08:34:14.553 回答
0

消除 ; 从while循环。如下

while (c < 10)
于 2012-08-04T08:35:26.630 回答