73 |
|
* |
74 |
|
* <dd>New threads are created using a {@link |
75 |
|
* java.util.concurrent.ThreadFactory}. If not otherwise specified, a |
76 |
< |
* {@link DefaultThreadFactory} is used, that creates threads to all |
76 |
> |
* {@link Executors#defaultThreadFactory} is used, that creates threads to all |
77 |
|
* be in the same {@link ThreadGroup} and with the same |
78 |
|
* <tt>NORM_PRIORITY</tt> priority and non-daemon status. By supplying |
79 |
|
* a different ThreadFactory, you can alter the thread's name, thread |
322 |
|
handler.rejectedExecution(command, this); |
323 |
|
} |
324 |
|
|
325 |
– |
/** |
326 |
– |
* The default thread factory used to create new threads. This |
327 |
– |
* factory creates all new threads used by the Executor in the |
328 |
– |
* same {@link ThreadGroup}. If there is a {@link |
329 |
– |
* java.lang.SecurityManager}, it uses the group of {@link |
330 |
– |
* System#getSecurityManager}, else the group of the thread |
331 |
– |
* creating the Executor. Each new thread is created as a |
332 |
– |
* non-daemon thread with priority |
333 |
– |
* <tt>Thread.NORM_PRIORITY</tt>. New threads have names |
334 |
– |
* accessible via {@link Thread#getName} of |
335 |
– |
* <em>pool-N-thread-M</em>, where <em>N</em> is the sequence |
336 |
– |
* number of this factory, and <em>M</em> is the sequence number |
337 |
– |
* of the thread created by this factory. |
338 |
– |
*/ |
339 |
– |
protected static class DefaultThreadFactory implements ThreadFactory { |
340 |
– |
private static final AtomicInteger poolNumber = new AtomicInteger(1); |
341 |
– |
private final ThreadGroup group; |
342 |
– |
private final AtomicInteger threadNumber = new AtomicInteger(1); |
343 |
– |
private final String namePrefix; |
344 |
– |
|
345 |
– |
/** |
346 |
– |
* Create a new DefaultThreadFactory that will create Threads |
347 |
– |
* with the group of the current System SecurityManager's |
348 |
– |
* ThreadGroup if it exists, else the group of the |
349 |
– |
* thread invoking this constructor. |
350 |
– |
*/ |
351 |
– |
public DefaultThreadFactory() { |
352 |
– |
SecurityManager s = System.getSecurityManager(); |
353 |
– |
group = (s != null)? s.getThreadGroup() : |
354 |
– |
Thread.currentThread().getThreadGroup(); |
355 |
– |
namePrefix = "pool-" + |
356 |
– |
poolNumber.getAndIncrement() + |
357 |
– |
"-thread-"; |
358 |
– |
} |
359 |
– |
|
360 |
– |
/** |
361 |
– |
* Create and return a new Thread with ThreadGroup established |
362 |
– |
* in constructor, with non-daemon status, with normal |
363 |
– |
* priority, and with name displaying the factory and thread |
364 |
– |
* sequence numbers. |
365 |
– |
* @param r a runnable to be executed by new thread instance |
366 |
– |
* @return constructed thread |
367 |
– |
*/ |
368 |
– |
public Thread newThread(Runnable r) { |
369 |
– |
Thread t = new Thread(group, r, |
370 |
– |
namePrefix + threadNumber.getAndIncrement(), |
371 |
– |
0); |
372 |
– |
if (t.isDaemon()) |
373 |
– |
t.setDaemon(false); |
374 |
– |
if (t.getPriority() != Thread.NORM_PRIORITY) |
375 |
– |
t.setPriority(Thread.NORM_PRIORITY); |
376 |
– |
return t; |
377 |
– |
} |
378 |
– |
} |
325 |
|
|
326 |
|
|
327 |
|
/** |
633 |
|
|
634 |
|
/** |
635 |
|
* Creates a new <tt>ThreadPoolExecutor</tt> with the given |
636 |
< |
* initial parameters. It may be more convenient to use one of |
637 |
< |
* the {@link Executors} factory methods instead of this general |
638 |
< |
* purpose constructor. |
636 |
> |
* initial parameters and default thread factory and handler. It |
637 |
> |
* may be more convenient to use one of the {@link Executors} |
638 |
> |
* factory methods instead of this general purpose constructor. |
639 |
|
* |
640 |
|
* @param corePoolSize the number of threads to keep in the |
641 |
|
* pool, even if they are idle. |
660 |
|
TimeUnit unit, |
661 |
|
BlockingQueue<Runnable> workQueue) { |
662 |
|
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, |
663 |
< |
new DefaultThreadFactory(), defaultHandler); |
663 |
> |
Executors.defaultThreadFactory(), defaultHandler); |
664 |
|
} |
665 |
|
|
666 |
|
/** |
729 |
|
BlockingQueue<Runnable> workQueue, |
730 |
|
RejectedExecutionHandler handler) { |
731 |
|
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, |
732 |
< |
new DefaultThreadFactory(), handler); |
732 |
> |
Executors.defaultThreadFactory(), handler); |
733 |
|
} |
734 |
|
|
735 |
|
/** |
1015 |
|
* Sets the core number of threads. This overrides any value set |
1016 |
|
* in the constructor. If the new value is smaller than the |
1017 |
|
* current value, excess existing threads will be terminated when |
1018 |
< |
* they next become idle. |
1018 |
> |
* they next become idle. If larger, new threads will, if needed, |
1019 |
> |
* be started to execute any queued tasks. |
1020 |
|
* |
1021 |
|
* @param corePoolSize the new core size |
1022 |
|
* @throws IllegalArgumentException if <tt>corePoolSize</tt> |
1030 |
|
try { |
1031 |
|
int extra = this.corePoolSize - corePoolSize; |
1032 |
|
this.corePoolSize = corePoolSize; |
1033 |
< |
if (extra > 0 && poolSize > corePoolSize) { |
1033 |
> |
if (extra < 0) { |
1034 |
> |
Runnable r; |
1035 |
> |
while (extra++ < 0 && poolSize < corePoolSize && |
1036 |
> |
(r = workQueue.poll()) != null) |
1037 |
> |
addThread(r).start(); |
1038 |
> |
} |
1039 |
> |
else if (extra > 0 && poolSize > corePoolSize) { |
1040 |
|
Iterator<Worker> it = workers.iterator(); |
1041 |
|
while (it.hasNext() && |
1042 |
< |
extra > 0 && |
1042 |
> |
extra-- > 0 && |
1043 |
|
poolSize > corePoolSize && |
1044 |
< |
workQueue.remainingCapacity() == 0) { |
1044 |
> |
workQueue.remainingCapacity() == 0) |
1045 |
|
it.next().interruptIfIdle(); |
1093 |
– |
--extra; |
1094 |
– |
} |
1046 |
|
} |
1096 |
– |
|
1047 |
|
} finally { |
1048 |
|
mainLock.unlock(); |
1049 |
|
} |