2012-04-29 15 views
-2

私はrunnableを実装するJavaプログラムを持っています。本文では、私はThread animator = new Thread();を持っていて、次にanimator.start();です。問題は、私のプログラムのrun()メソッドが実行されないということです。 が見つかりませんか?スレッドが実行されていません

+4

コードを投稿してください –

+2

実際のコードを十分に投稿して、すべての部品がどのように定義され呼び出されているかを確認してください。それがなければ、これは本当の質問ではありません。 –

+2

runnableを実装するオブジェクトを新しいThreadコンストラクタに渡していますか? – pimaster

答えて

2

あなたがrunnableを実装しているjavaプログラムを言ったように。そのクラスで

(名前はアニメーターを言う)あなたは私が間違っ

パス実行可能なクラスのインスタンスはないです場合は、ここで私がスレッド

を作成しながら、それは thisことだと思う

Thread animator = new Thread(); 
animator.start(); 

を書かれているボディ

Thread animator = new Thread(this); 
animator.start(); 
1

この方法で試すことができます

public class BackgroundActivity { 

/** 
* Attempts to execute the user activity. 
* 
* @return The thread on which the operations are executed. 
*/ 
public Thread doWork() { 
    final Runnable runnable = new Runnable() { 
     public void run() { 
      System.out.println("Background Task here"); 
     } 
    }; 

    // run on background thread. 
    return performOnBackgroundThread(runnable); 
} 

/** 
* Executes your requests on a separate thread. 
* 
* @param runnable 
*   The runnable instance containing mOperations to be executed. 
*/ 
private Thread performOnBackgroundThread(final Runnable runnable) { 
    final Thread t = new Thread() { 
     @Override 
     public void run() { 
      try { 
       runnable.run(); 
      } finally { 
      } 
     } 
    }; 
    t.start(); 
    return t; 
} 

}

最後にdoWork()メソッドあなたの主な方法

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    BackgroundActivity ba = new BackgroundActivity(); 
    Thread thread = ba.doWork(); 

    //You can manages thread here 
} 

希望から、それはあなたを助けるでしょう。

関連する問題