2015-06-15 30 views
-3

我希望此代碼運行順序一二三,一二三等,但他們運行在隨機順序我嘗試過很多辦法,如設置優先級,但似乎沒有任何工作以隨機順序運行所有三個線程這不是我想要的輸出我想線程工作的順序創造,但我不能這樣做使用此代碼的建議,請

class thrd extends Thread 
{ 
Thread t; 

thrd(String name) 
    { 
    t=new Thread(this,name); 
    System.out.println(name +":Created"+t); 
    } 

@Override 
public void run(){ 
    try 
    { 
     for(int i=1;i<=5;i++) 
     { 
      System.out.println(t.getName()+": "+i); 
      Thread.sleep(500); 
     } 
    } 
    catch(InterruptedException e) 
    { 
     System.out.println("Interrupted"+e); 
    } 
} 

} 

public class threadse { 

public static void main(String[] args) 
    { 
    thrd o1=new thrd("One"); 
    thrd o2=new thrd("Two"); 
    thrd o3=new thrd("Three"); 

    o1.t.start(); 
    o2.t.start(); 
    o3.t.start(); 

    System.out.println("Thread One alive: "+o1.t.isAlive()); 
    System.out.println("Thread Two alive: "+o2.t.isAlive()); 
    System.out.println("Thread Three alive: "+o3.t.isAlive()); 

     try 
     { 

     System.out.println("Waiting"); 
     o1.t.join(); 
     o2.t.join(); 
     o3.t.join(); 

     } 
    catch(InterruptedException e) 
     { 
     System.out.println("Interrupted: "+e); 
     } 

    System.out.println("Thread one alive: "+o1.t.isAlive()); 
    System.out.println("Thread Two alive: "+o2.t.isAlive()); 
    System.out.println("Thread Three alive: "+o3.t.isAlive()); 
    } 
} 
+6

線程的重點本質上是它們獨立運行。如果你想按照某個順序完成某些操作 - 「做Foo,酒吧,然後Baz」 - 那麼最簡單的方法就是在一個線程上運行它們。 – yshavit

+2

這不是線程的工作方式。如果你想順序執行使用單個線程。否則,你需要使用幾種機制之一來控制線程的執行,但是這太寬泛了,不能在這裏回答(但你可以通過搜索找到大量的例子)。 – Kayaman

+0

感謝您的幫助 – VIPER

回答

-1

你只需要維持一個程序線程執行工作,使這項工作成爲關鍵部分。臨界區是幾行代碼上只有一個線程可以同時運行,但這裏你想嚴格序貫模式線程,這樣就只需要讓它確保::

在for循環只有那個線程執行輪到那裏使用sequenceCount變量。請參閱/ *註釋* /內部代碼以獲取更多說明。

class thrd extends Thread{  

    /* A sequence counter to make execution of threads sequentially */ 
    static int sequenceCount=0; 
    Thread t; 

    public thrd(String name){ 
     t=new Thread(this,name); 
     System.out.println(name+" Created"); 
    } 

    @Override 
    public void run(){ 
     try{ 

      for(int i=0;i<5;i++){ 
       /* Wait here if it is not current thread's turn */ 
       while(true){ 
        /* if it is 1st thread's turn and current thread is First Thread then break */ 
       if((sequenceCount==0)&&(t.getName().equals("One")))break; 
       /* In this way other threads will wait it it is not their turn */ 
       else if((sequenceCount==1)&&(t.getName().equals("Two")))break; 
       else if((sequenceCount==2)&&(t.getName().equals("Three")))break; 
      }  
      System.out.println(t.getName()+" : "+i); 
      Thread.sleep(500); 
      /* If it was last thread which printed out then move to 1st thread.. */ 
      if(sequenceCount==2)sequenceCount=0; 
      /* OtherWise make it turn of next Thread */ 
      else sequenceCount++; 
     } 

    }catch(Exception er){er.printStackTrace();} 
} 

} 

public class threadse { 

    public static void main(String[] args) 
    { 
    thrd o1=new thrd("One"); 
    thrd o2=new thrd("Two"); 
    thrd o3=new thrd("Three"); 

    o1.t.start(); 
    o2.t.start(); 
    o3.t.start(); 
    } 
} 
+0

爲什麼要投入正確的答案??這是一個非常便宜的事情,沒有解釋。 – BrahmaSUR

+0

我懷疑下來的選民不知道從哪裏開始描述原因。實際上你的答案有很多問題。線程內部創建的線程,不規則的等待機制以及代碼完全不是線程安全的。 – sstan

+0

1.>線程內創建的線程??你在這裏寫什麼垃圾......所以你的意思是你可以創建一個沒有父線程的子線程。讀代碼兩次新線程是子線程的一部分。 – BrahmaSUR

相關問題