| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Girish
Title :
ThreadPoolExecutor Example code
Description :
Explained with question answer More...
|
Written By : Sameer1
Title :
Some Questions on
Description :
Java J2EE Technology More...
|
| Tags/Keywords : interview questions Java, Java Questions Author : Girish Date (Year/Month/Date): 2009-03-15
ThreadPoolExecutor-Example-code | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
Explaining ThreadPoolExecutor from java.util.concurrent package
newly introduced in 1.5 version of JDK.
As of JDK 1.5, ThreadPoolExecutor has Four constructors such as
ThreadPoolExecutor with five arguments in constructor, such as
1. Core Pool Size with int data type.
Number of sub threads available at any given point of time
including all idle threads.
2. Maximum Pool Size with data type as int.
maximum number of threads allowed in this pool executor
3. Keep thread alive time with data type as long.
Any thread that has count more than the core pool size of this
thread pool, and is idle/completed execution of any task,
to be kept alive for this (keep alive time) period, before
terminating/not alive state.
4. Time unit as data type TimeUnit from java.util.concurrent package
Time format to be used for keep alive time value, such as
Micro seconds, milli seconds, nano seconds, seconds.
5. queue of all threads put in it and is having data type as BlockingQueue
from java.util.concurrent package.
This queue can hold all the runnable objects those will be executed
along with any of the thread present inside the pool of threads in this thread
pool executor.
Question: Is it neccessary to have all the task provided to thread pool executor be
implementing Runnable?
Answer: No, Task can be implmenting Runnable or Callable (interface from
java.util.concurrent package).
Question: What are the differences between Task implementing Runnable and Task
implementing Callable interface?
Answer: As we know that Runnable interface has a method run with no argument
and that doesnot throw any checked exception, but Callable interface has a method
call that throws Exception (any checked exception) and has a return type as Object
(so any Object type can be returning with appropriate type casting).
This means by using Callable call method, one can have some return comming out of
Task to the caller program, can be used to return any desired status or return value
and the throwing of checked exception makes it easier for the Task to throw application
specific checked exception back to the caller program, in case there is any exceptional
condition arises.
Question: Do you mean to say that there will be difficulty in handing checked exception
in case of using Runnable Task with ThreadPoolExecutor?
Answer: No, there is a callback handler in ThreadPoolExecutor that notifies any
checked or unchecked or runtime exception happening during execution , but this happens
only after the Task execution completes/abruptly temninates because of any exception,
and on afterExecute callback method by passing the Task and any exception
along with it to the caller program.
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
|
|
Commented By -> Amit | One question was keep knocking my mind as to what would be starting point for
Strategizing methods for laying down high level architectural guidelines for
developing a Multi-Thread based Java application.
Like for example if I am given a job of identifying Framework for a web application
That should adhere to principles of MVC (Model View Controller paradigm), then what
Will be yours/anyone who has already worked on projects/products using Struts Framework
, recommendation, I guess it will be Struts or JSF.
Then for a Java command line based application with multi-Thread capability, what
Would be preferred Framework to start with (if any)??? Is a question came to my mind
Sometime back till JDK 1.5 version released.
Now with JDK 1.5 version, the way ThreadPoolExecutor provides some higher level of
Abstraction in terms of handling low level API of managing lifecycle of child threads,
Callback methods in case of specific events registered for, clean shutdown of sub-threads.
And many more, I must say I would always recommend using ThreadPoolExecutor over
traditionally handling Thread class and Runnable interfaces to start with.
ThreadPoolExecutor as the name implies, has pool of threads and whose execution is
controlled by this class.
There is a variant or subclass of ThreadPoolExecutor, available for scheduling capability
is named as ScheduledThreadPoolExecutor.
Question:
What is the condition that makes a ThreadPoolExecutor rejects a new task Thread?
There are two parameters core pool size, and max pool size, passed to ThreadPoolExecutor
as argument, and these two parametes values contributes in deciding factor for a new
Task Thread to be rejected, as explained below:
1. if there are fewer task threads in ThreadPoolExecutor than value of core pool size, then
the new task thread will be accepted.
2. If a new task thread is required , whose count is more than core pool size,
but less than max pool size, then this new task thread is accepted.
3. But once all the running Task Thread count is equal to Max pool size, then this
new Task thread will be rejected.
If anyone is having any difference of opinion, please let me know
by providing your valuable comment here. |
| | |
Commented By -> Amit | Question
while using ThreadPoolExecutor, what are the ways by which all the Tasks
and active threads can be shutdown in an orderlt manner?
Answer
There are two methods available in ThreadPoolExecutor, such as shutdown
and ShutdownNow,
shutdown method waits for all the previously submitted Tasks and related
threads to complete their tasks and then upon completion, initiates shut
down process for these Tasks, while stops accepting any new Task.
shutdownNow initiates or attempts to initiate process of shut down of
all actively running or executing Tasks and halts processing of any waiting
Tasks in queue. This method returns list of all the waiting Tasks, those were
not processed.
|
| | Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Interview Questions on Java >>> ThreadPoolExecutor Example code >>> Explained with question answer |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|