2012-06-06 258 views
3

對不起,如果這是一個基本的問題,但我一直在考慮做多個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搜索了一下線程,並且在任何地方都找不到它。

回答

2

你需要重寫run方法&在運行的情況下,你需要創建一個線程的實例

public class MyThread extends Thread{ 
    @Override 
    public void run() { 
     while(true) { 
      System.out.println("My Thread running"); 
     } 

} 

併爲Runnable

class MyRunnable implements Runnable{ 

      public void run(){ 
         System.out.println("I am executing by Thread: " + Thread.currentThread().getName()); 
      } 
} 

Thread mythread = new MyThread(); 
    mythread.setName("T1"); 
    Thread myrunnable = new Thread(new MyRunnable()); 
    myrunnable.start(); 
2

的情況下要開始線程,您需要創建兩個Thread s fr OM的Runnable S和啓動:

Thread t1 = new Thread(r); 
Thread t2 = new Thread(a); 
t1.start(); 
t2.start(); 
2

定義類r1和r2爲:

public class Thread1 extends Thread { 

@Override 
public void run() { 
    System.out.println("r1"); 
    try { 
     this.sleep(100); 
    } catch (Exception ex) { 

    }   
} 

} 

public class Thread2 extends Thread { 

@Override 
public void run() { 
    System.out.println("r2"); 
    try { 
     this.sleep(100); 
    } catch (Exception ex) { 
    } 
} 
} 


public class ThreadTester { 

    public static void main(String[] args) { 
     Thread1 r = new Thread1(); 
     Thread2 a = new Thread2(); 
     r.start(); 
     a.start(); 
    } 
} 

使用Runnable接口:

public class HelloRunnable implements Runnable { 

    public void run() { 
     System.out.println("Hello from a thread!"); 
    } 

    public static void main(String args[]) { 
     (new Thread(new HelloRunnable())).start(); 
    } 

} 

檢查​​更多信息