import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class DaemonTheadFactory implements ThreadFactory {
public Thread newThread (Runnable r) {
Thread t=new Thread(r);
t.setDaemon(true);
return t;
}
}
public class DaemonFactory implements Runnable {
public void run() {
try{
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(Thread.currentThread()+" : "+this);
}
}catch(InterruptedException e) {
System.out.println("Interrupted!");
}
}
public static void main(String[] args) {
try {
ExecutorService exec=Executors.newCachedThreadPool(new DaemonTheadFactory());
for(int i=0;i<10;i++)
exec.execute(new DaemonFactory());
System.out.println("All daemonthreads start!");
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(DaemonFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
import java.util.logging.Level;
import java.util.logging.Logger;
class DaemonTheadFactory implements ThreadFactory {
public Thread newThread (Runnable r) {
Thread t=new Thread(r);
t.setDaemon(true);
return t;
}
}
public class DaemonFactory implements Runnable {
public void run() {
try{
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(Thread.currentThread()+" : "+this);
}
}catch(InterruptedException e) {
System.out.println("Interrupted!");
}
}
public static void main(String[] args) {
try {
ExecutorService exec=Executors.newCachedThreadPool(new DaemonTheadFactory());
for(int i=0;i<10;i++)
exec.execute(new DaemonFactory());
System.out.println("All daemonthreads start!");
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(DaemonFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
}