スレッドはArrayList
に格納されているため、後で名前で動的に設定することができます。インターネット上には多くの例がありますが、これについてはすべて理解できますが、私はうまくいかないので、選択したスレッドは停止しません。 私のせいで何ができますか?arrayList内のスレッドは割り込みを知らない
public class Szal {
static ArrayList<MyThread> myThread;
static String[] names;
public Szal() {
myThread = new ArrayList<MyThread>();
names = new String[]{"EZ", "AZ"};
for (int i = 0; i < names.length; i++) {
MyThread t = new MyThread(names[i]);
myThread.add(t);
t.start();
}
}
public static void main(String[] args) {
new Szal();
Thread[] thread = new Thread [Thread.activeCount()];
int m = Thread.enumerate (thread);
for (int i = 0; i < m; i++) {
System.out.println (thread[i].getName());
}
// Why is this not working?
for (Thread t : myThread) {
if (t.getName().equalsIgnoreCase("EZ")) {
t = Thread.currentThread();
t.interrupt();
myThread.remove(t);
}
}
Thread[] threads = new Thread [Thread.activeCount()];
int c = Thread.enumerate (threads);
for (int i = 0; i < c; i++) {
System.out.println (threads[i].getName());
}
}
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
public void run() {
while (true) {
}
}
}
}
ここではどのスレッドが中断されていますか? –
なぜ 't = Thread.currentThread();'をやっているのですか?あなたがそれを修正して 't'を中断した後でさえ、あなたはそれが他の何かを実際に行うためにフラグを設定するためにスレッドがインターハンド状態になる必要があります。 http://idownvotedbecau.se/nodebugging/ – Oleg