有些时候,多线程只是追求并行操作,此时就需要指定线程完成特定任务了。
2.代码线程池代码:
public class ThreadManager {
private static int corePoolSize = Runtime.getRuntime().availableProcessors();
private static ThreadFactory namedFactory = new ThreadFactoryBuilder().setNameFormat("ods-%d").build();
public static ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, corePoolSize + 1, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000), namedFactory, new ThreadPoolExecutor.AbortPolicy());
}
修改线程名称,并指定线程执行任务:
// 获取所有线程 Setthreads = Thread.getAllStackTraces().keySet(); List threadsNameList = threads.stream().map(Thread::getName).collect(Collectors.toList()); // 修改线程名【可根据任务的某些参数对线程进行重命名】 if (!threadsNameList.contains("threadName")) { Thread.currentThread().setName("threadName"); } // 根据线程名称指定任务 if (Thread.currentThread().getName().equals("threadName")) { // 业务代码 }



