444 |
|
* next task in queue, or if there is none, the given task. |
445 |
|
* @param firstTask the task the new thread should run first (or |
446 |
|
* null if none) |
447 |
< |
* @return null on failure, else the first task to be run by new thread. |
447 |
> |
* @return 0 if a new thread cannot be created, a positive number |
448 |
> |
* if firstTask will be run in a new thread, or a negative number |
449 |
> |
* if a new thread was created but is running some other task, in |
450 |
> |
* which case the caller must try some other way to run firstTask |
451 |
> |
* (perhaps by calling this method again). |
452 |
|
*/ |
453 |
< |
private Runnable addIfUnderMaximumPoolSize(Runnable firstTask) { |
453 |
> |
private int addIfUnderMaximumPoolSize(Runnable firstTask) { |
454 |
|
Thread t = null; |
455 |
< |
Runnable next = null; |
455 |
> |
int status = 0; |
456 |
|
final ReentrantLock mainLock = this.mainLock; |
457 |
|
mainLock.lock(); |
458 |
|
try { |
459 |
|
if (poolSize < maximumPoolSize) { |
460 |
< |
next = workQueue.poll(); |
461 |
< |
if (next == null) |
460 |
> |
Runnable next = workQueue.poll(); |
461 |
> |
if (next == null) { |
462 |
|
next = firstTask; |
463 |
+ |
status = 1; |
464 |
+ |
} else |
465 |
+ |
status = -1; |
466 |
|
t = addThread(next); |
467 |
|
} |
468 |
|
} finally { |
469 |
|
mainLock.unlock(); |
470 |
|
} |
471 |
|
if (t == null) |
472 |
< |
return null; |
472 |
> |
return 0; |
473 |
|
t.start(); |
474 |
< |
return next; |
474 |
> |
return status; |
475 |
|
} |
476 |
|
|
477 |
|
|
876 |
|
return; |
877 |
|
if (workQueue.offer(command)) |
878 |
|
return; |
879 |
< |
Runnable r = addIfUnderMaximumPoolSize(command); |
880 |
< |
if (r == command) |
879 |
> |
int status = addIfUnderMaximumPoolSize(command); |
880 |
> |
if (status > 0) // created new thread |
881 |
|
return; |
882 |
< |
if (r == null) { |
882 |
> |
if (status == 0) { // failed to create thread |
883 |
|
reject(command); |
884 |
|
return; |
885 |
|
} |
886 |
< |
// else retry |
886 |
> |
// Retry if created a new thread but it is busy with another task |
887 |
|
} |
888 |
|
} |
889 |
|
|