次のコードで実行可能なクラスを実装するクラスがある場合:
public class MyRunnable implements Runnable {
public Thread t;
// Other variables;
public MyRunnable() {
t = new Thread(this, "MyRunnable Thread");
// Initialise other variables.
}
public void run() {
//Do something.
}
}
そして、私は次の方法で上記のクラスのインスタンスを作成しています:
public class MyFunc () {
satic void main (String ards[]) {
MyRunnable mr = new MyRunnable();
mr.t.start();
while (true) {
Thread.sleep(10000);
if (!mr.isAlive()) {
//Execute mr again.
// How to do it ?
}
}
}
}
どうすればいいですか?
私は2つの方法を念頭に置いていますが、どちらが正しいかわかりません。
1.mr.t.start ();
2. MyRunnable mr = new MyRunnable(); mr.t.start();
mr の新しいインスタンスを作成する必要がありますか?
または、既存のインスタンスまたは mr を使用する必要がありますか?