public class TaskManipulator implements Runnable, CustomThreadManipulator {
private Thread current;
@Override
public void run() {
do{
System.out.println(current.getName());
try {
current.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while(current.isAlive());
}
@Override
public void start(String threadName) {
current = new Thread(threadName);
current.start();
}
@Override
public void stop() {
current.interrupt();
}
}
package com.codegym.task.task25.task2508;
/*
No goofing off
*/
public class Solution
{
/*
Output:
first
first
second
second
second
third
fifth
*/
public static void main(String[] args) throws InterruptedException {
CustomThreadManipulator manipulator = new TaskManipulator();
manipulator.start("first");
Thread.sleep(150);
manipulator.stop();
manipulator.start("second");
Thread.sleep(250);
manipulator.stop();
manipulator.start("third");
Thread.sleep(50);
manipulator.stop();
manipulator.start("forth");
manipulator.stop();
manipulator.start("fifth");
Thread.sleep(1);
manipulator.stop();
}
}