Почему в run нужно заводить отдельную переменную для текущего потока? с использование target (переменной класса) для вывода имени тек. нити в консоль "fifth" выводится дважды:
@Override
public void run() {
while (!target.isInterrupted()) {
System.out.println(target.getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
first
first
second
second
second
third
fifth
fifth
выводится как надо по заданию:
public class TaskManipulator implements Runnable, CustomThreadManipulator {
private Thread target;
@Override
public void run() {
Thread current = Thread.currentThread();
while (!current.isInterrupted()) {
System.out.println(current.getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
@Override
public void start(String threadName) {
target = new Thread(this, threadName);
target.start();
}
@Override
public void stop() {
target.interrupt();
}
}