How to Create a thread inside Executor?

Source: Internet
Author: User

For Thread, it is not only a unit of work (including the run method), but also an execution mechanism (start method ). Executor framework decouples the two to separate the work units and execution mechanisms. Executor is responsible for executing tasks. runnable and callable represent business logic units (the former has no return value, and the latter has a return value ). With the executor framework, we do not need to manually create a Thread because the executor framework has an interface dedicated to Thread creation-ThreadFactory, which declares only one method-newThread and returns one Thread. A simple implementation is: [java] class SimpleThreadFactory implements ThreadFactory {@ Override public Thread newThread (Runnable r) {return new Thread (r );}} if we use the executors tool class to create a specific executor, if the custom ThreadFactory is not explicitly told, the default thread factory is used: [java] static class DefaultThreadFactory implements ThreadFactory {private static final AtomicInteger poolNumber = new AtomicInteger (1); private final ThreadGroup group; private final OmicInteger threadNumber = new AtomicInteger (1); private final String namePrefix; DefaultThreadFactory () {SecurityManager s = System. getSecurityManager (); group = (s! = Null )? S. getThreadGroup (): Thread. currentThread (). getThreadGroup (); namePrefix = "pool-" + poolNumber. getAndIncrement () + "-thread-";} public Thread newThread (Runnable r) {Thread t = new Thread (group, r, namePrefix + threadNumber. getAndIncrement (), 0); if (t. isDaemon () t. setDaemon (false); if (t. getPriority ()! = Thread. NORM_PRIORITY) t. setPriority (Thread. NORM_PRIORITY); return t; www.2cto.com} if you want to customize the thread factory, you need to provide a class that implements ThreadFactory (SimpleThreadFactory above), and then when we create a specific executor: [java] ExecutorService executor = Executors. newCachedThreadPool (new SimpleThreadFactory (); ExecutorService executor = Executors. newSingleThreadExecutor (new SimpleThreadFactory (); ExecutorService executor = Executors. newFixedThread Pool (10, new SimpleThreadFactory (); ExecutorService executor = Executors. newScheduledThreadPool (10, new SimpleThreadFactory (); in this way, a custom thread factory is used to create a thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.