一度に 1 つずつ開始できる一連の実行可能なスレッドを用意しようとしています。何かのようなもの
First(new Thread() {
public void run() {
//do something
}
});
私がやろうとしていることは不可能ですか?
一度に 1 つずつ開始できる一連の実行可能なスレッドを用意しようとしています。何かのようなもの
First(new Thread() {
public void run() {
//do something
}
});
私がやろうとしていることは不可能ですか?
シングルスレッドの Executor を使用できます
ExecutorService service = Executors.newSingleThreadedPool();
service.submit(runnable1);
service.submit(runnable2);
service.submit(runnable3);
1 つのスレッドに複数のランナブルが必要です。彼らはさまざまな時期にさまざまなことをするでしょう。
これは私には悪い設計のように聞こえます。クラスが異なる時間に異なることをしている場合は、異なるクラスに分割する必要があります。
同じバックグラウンドスレッドを再利用してさまざまなことを行うことについて話している場合は、@Peter の回答のように単一のスレッド プールを使用します。
private ExecutorService threadPool = Executors.newSingleThreadedPool();
...
threadPool.submit(new First());
threadPool.submit(new Second());
threadPool.submit(new Third());
...
// when you are done submitting, always shutdown your pool
threadPool.shutdown();
、、First
およびクラスは を実装しSecond
ます。一部の状態を共有する必要がある場合は、コンストラクター引数を取ることができます。Third
Runnable
はい、複数のプライベートメソッドがあります。
public class FirstCaller {
private void method1() { }
private void method2() { }
private void method3() { }
public void someMethod() {
First(new Thread() {
public void run() {
//do something
method1();
method2();
method3();
}
});
}
}
またはTedHoppが指摘したように
public class FirstCaller {
public void someMethod() {
new First(new Thread() {
private void method1() { }
private void method2() { }
private void method3() { }
public void run() {
//do something
method1();
method2();
method3();
}
});
}
}
同時にいくつかのスレッドを開始したい場合CountDownLatch
は、必要なものです。http://www.javamex.com/tutorials/threads/CountDownLatch.shtmlの例を参照してください。
単一のスレッドで複数のランナブルを順番に実行しようとしていますか? 次から次へ?
public class MultiRunnable implements Runnable {
private Runnable runnable1;
private Runnable runnable2;
public MultiRunnable(Runnable runnable1, Runnable runnable2) {
this.runnable1 = runnable1;
this.runnable2 = runnable2;
}
@Override
public void run() {
runnable1.run();
runnable2.run();
}
}
その後、呼び出すことができます(new Thread(new MultiRunnable(... , ...))).start();
これにより、最初の Runnable が最初に実行され、それが完了すると 2 番目が実行されます。
または、より多くの Runnable に一般化します。
import java.util.Arrays;
import java.util.List;
public class MultiRunnable implements Runnable {
private List<Runnable> runnables;
public MultiRunnable(Runnable... runnables) {
this.runnables = Arrays.asList(runnables);
}
public MultiRunnable(List<Runnable> runnables) {
this.runnables = runnables;
}
@Override
public void run() {
for(Runnable runnable : runnables)
runnable.run();
}
}
最も簡単な方法は、いくつかのThread
サブクラス インスタンスを定義し、目的に応じて適切なインスタンスを呼び出すことです。
Thread
ただし、状況によって動作が異なる単一のオブジェクトが本当に必要な場合は、その動作Thread
を制御するための状態変数を持つサブクラスを定義できます。
class MyThread extends Thread {
public enum Action { A, B, C }
private Action mAction;
public void run() {
if (mAction == null) {
throw new IllegalStateException("Action must be specified");
}
switch (mAction) {
case A:
methodA();
break;
case B:
methodB();
break;
case C:
methodC();
break;
}
}
public void setAction(Action action) {
if (action == null) {
throw new IllegalArgumentException("Action cannot be null");
}
mAction = action;
}
private void methodA() { ... }
private void methodB() { ... }
private void methodC() { ... }
}
次に、スレッドを作成し、 を呼び出す前にstart()
、 を呼び出しsetAction
て値の 1 つを渡すことができAction
ます。
状態変数の代わりに、run()
メソッドは外部変数を調べてアクションの選択を決定することができます。これが理にかなっているかどうか (そしてそれがより良いかどうか) は、アプリケーションによって異なります。