You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

package world;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class ThreadWeaver {
private final List<Runnable> runners = new ArrayList<Runnable>();
private final List<Thread> managed = new ArrayList<Thread>();
public void addRunners(Runnable... runners){
}
public void weave(){
for(Runnable r: runners){
managed.add(new Thread(r));
}
}
public void run(){
for(Thread t : managed){
t.start();
}
}
public void recover(){
for(Thread t : managed){
try {
t.wait();
}catch(InterruptedException ie){
System.out.println(ie.getMessage());
}
}
termina();
}
public void recover(Long timeout){
for(Thread t : managed){
try {
t.wait(timeout);
}catch(InterruptedException ie){
System.out.println(ie.getMessage());
}
managed.remove(t);
}
termina();
}
public void termina(){
for(Thread t : managed){
System.out.println("Thread "+t.getName()+" has not stopped being cleaned up");
t.interrupt();
}
managed.clear();
runners.clear();
}
}