From 6cd1c61d677aa80f0d0d59e1b2ff8791be62fdf0 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Fri, 16 Nov 2018 21:44:11 +0800 Subject: [PATCH] ThreadFactory --- .../javase/thread/ExcutorsSample.java | 35 ------------ .../javase/thread/ExecutorsSample.java | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 35 deletions(-) delete mode 100644 whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExcutorsSample.java create mode 100644 whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExecutorsSample.java diff --git a/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExcutorsSample.java b/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExcutorsSample.java deleted file mode 100644 index 6a73d1b9..00000000 --- a/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExcutorsSample.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.hongxi.whatsmars.javase.thread; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -/** - * Created by shenhongxi on 15/8/13. - */ -public class ExcutorsSample { - - public static void main(String[] args) { - ExecutorService threadPool = Executors.newCachedThreadPool(); - threadPool = Executors.newFixedThreadPool(10); - threadPool = Executors.newSingleThreadExecutor(); - - - - - - - - Executors.newScheduledThreadPool(3).scheduleAtFixedRate( - new Runnable() { - @Override - public void run() { - System.out.println("bombing!"); - - } - }, - 6, - 2, - TimeUnit.SECONDS); - } -} diff --git a/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExecutorsSample.java b/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExecutorsSample.java new file mode 100644 index 00000000..5e7bde72 --- /dev/null +++ b/whatsmars-javase/src/main/java/org/hongxi/whatsmars/javase/thread/ExecutorsSample.java @@ -0,0 +1,55 @@ +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 ExecutorsSample { + + public static void main(String[] args) { + ExecutorService threadPool = Executors.newCachedThreadPool(); + 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( + new Runnable() { + @Override + public void run() { + System.out.println("bombing!"); + + } + }, + 6, + 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; + } + } + +} -- GitLab