2012-09-03 130 views
2
import java.util.logging.Level; 
import java.util.logging.Logger; 


public class test { 

    private static void m1(final String a) { 
     Thread t1 = new Thread(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         Thread.sleep(1000); 
         System.out.print(a); 
        } catch (InterruptedException ex) { 
         Logger.getLogger(test.class.getName()).log(Level.SEVERE, 
                    null, ex); 
        } 
       } 
      }); 
     t1.start(); 
    } 

    private static void m2(final String a) { 
     Thread t2 = new Thread(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         Thread.sleep(1000); 
         System.out.print(" " + a); 
        } catch (InterruptedException ex) { 
         Logger.getLogger(test.class.getName()).log(Level.SEVERE, 
                    null, ex); 
        } 
       } 
      }); 
     t2.start(); 
    } 

    public static void main(String[] args) { 
     for (int i = 0; i < 10; i++) { 
      m1("Mihi"); 
      m2("Cherub"); 
      System.out.println(""); 
     } 
    } 
} 

我想要得到的輸出 MihiCherub MihiCherub同樣10倍Java的方法同步

但現在我的輸出是 「MihiMihi天使CherubMihi CherubMihiMih」。我想同步我的兩個方法,並希望得到mihicherub的結果。請幫助..

+0

如果這是'[功課]',請標記爲這樣的,那麼我們就知道你不能改變的要求,即使他們沒有任何意義。 ;) –

+1

在你的情況下,你可以分別在't1.start()'和't2.start()'後面加上't1.join()'和't2.join()' – cubanacan

回答

2

如果要同步行爲,最好的辦法是使用一個線程。

線程是爲相對獨立的任務而設計的,越獨立越好。

你可以做你想要同步的內容,等待和通知,但會比較複雜(和毫無意義)

在你的情況下,println("")之前的任何其他打印的將執行()。這是否滿足?

+0

對我來說,純粹是關於同步邏輯。然而,這個例子確實不是很合適,對你來說更適合開始尋找生產者/消費者,這更容易理解和理解! –

0

隨着CountDownLatch的使用,你可以做到這一點,但它不是一個有效的方法。我已經修改你的代碼如下。

import java.util.concurrent.CountDownLatch; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class test { 

private static void m1(final String a, final CountDownLatch sharedCountDownLatch, 
     final CountDownLatch masterCountDownLatch, 
     final CountDownLatch doneCountDownLatch) { 
    Thread t1 = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       sharedCountDownLatch.countDown(); 
       sharedCountDownLatch.await(); 
       System.out.print(a); 
       masterCountDownLatch.countDown(); 

      } catch (InterruptedException ex) { 
       Logger.getLogger(test.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } finally { 
       doneCountDownLatch.countDown(); 
      } 
     } 
    }); 
    t1.start(); 
} 

private static void m2(final String a, final CountDownLatch sharedCountDownLatch, 
     final CountDownLatch masterCountDownLatch, 
     final CountDownLatch doneCountDownLatch) { 
    Thread t2 = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       sharedCountDownLatch.countDown(); 
       sharedCountDownLatch.await(); 
       masterCountDownLatch.await(); 
       System.out.print(" " + a + " "); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(test.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } finally { 
       doneCountDownLatch.countDown(); 
      } 
     } 
    }); 
    t2.start(); 
} 

public static void main(String[] args) throws InterruptedException { 
    for (int i = 0; i < 10; i++) { 
     CountDownLatch doneCountDownLatch = new CountDownLatch(2);//CountDownLatch which will be to ensure that both threads executed. 
     CountDownLatch sharedCountDownLatch = new CountDownLatch(2);//CountDownLatch that will be shared between both threads so that it will ensure that both threads are at the same execution point 
     CountDownLatch masterCountDownLatch = new CountDownLatch(1);//CountDownLatch which will be used when master thread completes it work 
     m1("Mihi", sharedCountDownLatch, masterCountDownLatch, 
       doneCountDownLatch); 
     m2("Cherub", sharedCountDownLatch, masterCountDownLatch, 
       doneCountDownLatch); 
     System.out.println(""); 
     doneCountDownLatch.await(); 
    } 
} 
} 

,其輸出爲:

Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub