5つのスレッドのプールを作成するという要件が1つあり、これらの5つのスレッドから1つのスレッドをスレッドとして作成し、daemon
その特定の1つのスレッドがデーモンスレッドになると、それにタスクを割り当てたいと思います。 Javaプログラムが終了したときに、ウィンドウタスクマネージャーで特定のデーモンスレッドがまだそのタスクを実行していることを確認できるような、任意のサービスに関連するデーモンスレッド。私はこれに固執しているので..!
以下は私のコードです...
public class StoppingThread extends Thread //extend thread class
{
// public synchronized void run()
//synchronized (this)
private volatile boolean Completed = false;
public void setCompleted() {
Completed = true;
}
public void run()
{
for(int i=0;i<20 && !Completed;++i) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(500);
System.out.print(i +"\n"+ "..");
} catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String... a)
{
StoppingThread x = new StoppingThread();
StoppingThread y = new StoppingThread();
x.start();
x.setName("first");
x.setCompleted(); // Will complete as soon as the latest iteration finishes means bolean variable value is set to true
y.start();
y.setName("second");
}
}
これで、Yスレッドをデーモンスレッドにして、それにタスクを割り当てたいと思います