提交 6cd1c61d 编写于 作者: 武汉红喜's avatar 武汉红喜

ThreadFactory

上级 51bd8858
......@@ -2,22 +2,29 @@ package org.hongxi.whatsmars.javase.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by shenhongxi on 15/8/13.
*/
public class ExcutorsSample {
public class ExecutorsSample {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool = Executors.newFixedThreadPool(10);
threadPool = Executors.newSingleThreadExecutor();
threadPool = Executors.newFixedThreadPool(5, new NameThreadFactory());
for (int i = 0; i < 10; i++) {
final int j = i;
threadPool.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + j);
}
});
}
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
......@@ -32,4 +39,17 @@ public class ExcutorsSample {
2,
TimeUnit.SECONDS);
}
private static class NameThreadFactory implements ThreadFactory {
private static AtomicInteger idx = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("MARS-thread-" + idx.getAndIncrement());
return thread;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册