我有一个非常简单的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循环的开头)。
我在Oracle Java JDK 1.6u33
上使用Xubuntu 12.04.
在Windows上,此代码可以正常运行。有人可以告诉我,我该怎么做才能在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)
答案 0 :(得分:4)
在结束时你有一个;
,删除它并且它会起作用
while (c < 10);
上面的内容将在()中显示单行,并且因为c
从未增加,所以它是无限循环。将其改为
while (c < 10) // no ; at the end
答案 1 :(得分:0)
删除;来自while循环。如下:
while (c < 10)