2016-03-19 3 views
0

私は数多くのスレッドでBufferedImageをレンダリングしています。私がこれらのスレッドを開始するとき、私はEvent Dispatch Threadでwaitを呼び出して、すべてのレンダリングスレッドが通知信号を送信したときにのみ実際にイメージを描画するようにします。イベントディスパッチスレッドを待たずにイメージをレンダリングしますか?

synchronized (this) 
    { 
     while(threadsCompleted<RENDERING_THREADS){ 
      try{ 
       this.wait(); 
      }catch (InterruptedException e){ 
       e.printStackTrace(); 
      } 
     } 
     threadsCompleted = 0; 
    } 
g2.drawImage(image); 

、これは、スレッドの実行が終了するときに何が起こるかです::

これは、EDTで何が起こるかである

synchronized (MyPanel.this){ 
      threadsCompleted++; 
      MyPanel.this.notify(); 
     } 

私の質問は:上の呼び出しを作るための方法がありますdrawImageは実際にEDTの作業を停止せずにスレッドが終了するのを待ちますか?これらのミリ秒でレンダリングが進行するように、ユーザーはUIと実際にやりとりすることができるので、Event Dispatch Thread?

+0

移動を独自のスレッドに、 SwingUtilities invokeLater Runnable runメソッドを使用してdrawImageメソッドを呼び出します。 –

答えて

0

最も簡単な方法は、このように、java.util.concurrentで並行処理の構文を使用すると、完了するために、他のすべてのを待つスレッドを使用することです:最初の同期コード\

public class ImageRenderer implements Runnable 
{ 
    private final CountDownLatch latch; 

    public ImageRenderer(CountDownLatch latch) 
    { 
     this.latch = latch; 
    } 

    @Override 
    public void run() 
    { 
     try 
     { 
      // code... 
     } finally 
     { 
      latch.countDown(); 
     } 
    } 
} 

public class Waiter implements Runnable 
{ 
    private final CountDownLatch latch; 

    public Waiter(CountDownLatch latch) 
    { 
     this.latch = latch; 
    } 

    @Override 
    public void run() 
    { 
     try 
     { 
      latch.await(); 
      // rendering has now finished. 
      SwingUtilities.invokeLater(() -> { 
       // do something with the image on the EDT 
      } 
     } catch (InterruptedException e) 
     { 
      Thread.currentThread().interrupt(); 
     } 
    } 
} 
関連する問題