}\r
\r
public boolean offer(Runnable o) {\r
- if (parent != null && parent.getPoolSize() < parent.getMaximumPoolSize())\r
- return false; //force creation of new threads by rejecting the task\r
- else\r
- return super.offer(o);\r
+ //we can't do any checks\r
+ if (parent==null) return super.offer(o);\r
+ //we are maxed out on threads, simply queue the object\r
+ if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);\r
+ //we have idle threads, just add it to the queue\r
+ //this is an approximation, so it could use some tuning\r
+ if (parent.getActiveCount()<(parent.getPoolSize())) return super.offer(o);\r
+ //if we have less threads than maximum force creation of a new thread\r
+ if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;\r
+ //if we reached here, we need to add it to the queue\r
+ return super.offer(o);\r
}\r
}\r
\r
}
public boolean offer(Runnable o) {
- if ( parent != null && parent.getPoolSize()<parent.getMaximumPoolSize() ) return false;//force creation of new threads
- else return super.offer(o);
+ //we can't do any checks
+ if (parent==null) return super.offer(o);
+ //we are maxed out on threads, simply queue the object
+ if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
+ //we have idle threads, just add it to the queue
+ //this is an approximation, so it could use some tuning
+ if (parent.getActiveCount()<(parent.getPoolSize())) return super.offer(o);
+ //if we have less threads than maximum force creation of a new thread
+ if (parent.getPoolSize()<parent.getMaximumPoolSize()) return false;
+ //if we reached here, we need to add it to the queue
+ return super.offer(o);
}
}