對不起,如果這是一個基本的問題,但我一直在考慮做多個Sprite循環,並且第一次嘗試在while(true)循環中創建兩個主線程的線程。我的意圖是:讓兩個線程同時循環。但是,當我運行該程序時,它似乎中斷了執行流程,並且第二個循環沒有在新線程中執行,而是停在程序停留在線程的第一個無盡while()循環中。我認爲它仍然只是執行主線程,而不是開始一個新線程,然後繼續。創建多線程循環
我已經試過了兩種方式:
與主題一旦:
public class Zzz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
r1 r = new r1();
r2 a = new r2();
r.start();
a.start();
}
}
public class r1 extends Thread {
@Override
public void start() {
while(true) {
System.out.println("r1");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
}
public class r2 extends Thread {
@Override
public void start() {
while(true) {
System.out.println("r2");
try {
this.sleep(100);
} catch (Exception ex) {
}
}
}
}
一旦與Runnable接口:
public class Zzz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
r1 r = new r1();
r2 a = new r2();
r.run();
a.run();
}
}
public class r1 implements Runnable {
@Override
public void run() {
while(true) {
System.out.println("r1");
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
}
public class r2 implements Runnable {
@Override
public void run() {
while(true) {
System.out.println("r2");
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
}
,但無濟於事。它總是卡在R1處。任何想法的人?我用google搜索了一下線程,並且在任何地方都找不到它。