2011-08-10 6 views
0
public void setGifImage(InputStream inputStream) { 
     checkWidget(); 
     if (thread != null) { 
      thread.stopRunning(); 
      try { 
       thread.join(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

     ImageLoader loader = new ImageLoader(); 

     try { 
      loader.load(inputStream); 
     } catch (Exception e) { 
      this.image = null; 
      return; 
     } 

     if (loader.data[0] != null){ 
      System.out.println("set to new picture"); 
      this.image = new Image(this.getDisplay(), loader.data[0]); 
     } 

     if (loader.data.length > 1) { 
      System.out.println("start animation"); 
      thread = new GifThread(loader); 
      thread.start(); 
     }else{ 
      System.out.println("paint static picture"); 
     } 

     redraw(); 
    } 

スレッドに参加:
Javaのスレッドは問題

private class GifThread extends Thread { 

     private int imageNumber = 0; 
     private ImageLoader loader = null; 
     private boolean run = true; 

     public GifThread(ImageLoader loader) { 
      this.loader = loader; 
     } 

     public void run() { 
      while (run) { 
       int delayTime = loader.data[imageNumber].delayTime; 
       try { 
        Thread.sleep(delayTime * 10); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       if (!GifCLabel.this.isDisposed()) { 
        // if a asynchronous thread is running, this new runnable will be queued 
        GifCLabel.this.getDisplay().asyncExec(new Runnable() { 
         public void run() { 
          if (!GifCLabel.this.isDisposed()) { 
           imageNumber = imageNumber == loader.data.length - 1 ? 0 : imageNumber + 1; 
           if (!GifCLabel.this.image.isDisposed()) 
            GifCLabel.this.image.dispose(); 
           ImageData nextFrameData = loader.data[imageNumber]; 
           System.out.println("set to frame " + imageNumber); 
           GifCLabel.this.image = new Image(GifCLabel.this.getDisplay(), nextFrameData); 
           GifCLabel.this.redraw(); 
          } else 
           stopRunning(); 
         } 
        }); 
       } else 
        stopRunning(); 
      } 
     } 

     public void stopRunning() { 
      run = false; 
     } 
    } 

出力:

2011-08-10 03:44:24 DEBUG - 日志服务Ready! 
2011-08-10 03:44:28 DEBUG - current is null 
2011-08-10 03:44:28 DEBUG - found : [email protected] 
2011-08-10 03:44:28 DEBUG - can go back ? false 
2011-08-10 03:44:28 DEBUG - can go forward ? false 
set to new picture 
start animation 
2011-08-10 03:44:28 DEBUG - 尝试连接至 - jdbc:mysql://localhost:3306/credit 驱动配置:MySQL 驱动类:[email protected] 连接属性:{user=root, password=root} 
set to frame 1 
set to frame 2 
set to frame 3 
set to frame 4 
[[email protected], [email protected]] 
set to new picture 
paint static picture 
set to frame 5 

私はスレッドがまだthread.join後に実行なぜだろうか()?私が知っているように、thread.join()はスレッドを待つが、出力の最後の行を見ることができます...

+0

あなたが参加しているスレッドがあなたからのログを見ているのと同じスレッドであることを確認していますか?つまり、余分なスレッドが作成されていないということですか? (あなたはメソッドを同期させる必要があります) – SJuan76

+0

ありがとうございました。どのメソッドを同期させるのですか?それは私の問題をマックスの答えとして修正するだろうか?私の問題は修正されていますが、私はスレッディングについてもっと学びたいと思っています:D – CaiNiaoCoder

+0

Maxと私はあなたが参加するスレッドが仕事をしているということではないという考え方の両方にあります。あなたのsetGifImageメソッドが私の考えで同時にアクセスされていました。スレッドが2回結合され、結合されていない1つのスレッドが作成されることがあります。メソッドが同時に呼び出されないことが確実な場合は、何も変更する必要はありません。他の場所で同期させることができます。 – SJuan76

答えて

3

スレッドは、基本的にUIにイベントを送信するgetDisplay().asyncExec()スレッドを実行して、"set to frame "を出力するRunnableを実行します。

syncExec()を使用し、runランタイム内でも可変状態をチェックする方が良い。

もう一つのヒント:

  1. それはInterruptedExceptionをキャッチし、何もしないのは良い考えではありません。
  2. コールinterrupt
  3. stopRunnable()内部チェックrun変数status sleep後に終了します。

乾杯、 マックス

+0

"Runnable内で変数ステータスをチェックする"が機能します! – CaiNiaoCoder