Saturday 29 October 2011

Java/J2EE interview questions: - How would you create a thread pool in Java/J2EE??

While facing Java/J2EE interview questions you may come across this question by the interviewer. So you can your start with the answer as follows.

public class ThreadPool implements ThreadPoolInt.

This class is an generic implementation of a thread pool, which takes the following input: -
  • Size of the pool to be constructed.
  • Name of the class which implements Runnable and constructs a thread pool with active threads that are waiting for activation. Once the threads have finished processing they come back and wait once again in the pool.
This thread pool engine can be locked i.e. if some internal operation is performed on the pool then it is preferable that the thread engine be locked. Locking ensures that no new threads are issued by the engine. However, the currently executing threads are allowed to continue till they come back to the passive Pool.

Interested to learn more, then view the following detailed video on Java Builder pattern which is also asked during Java/J2EE interview questions: -



Visit for more Java/J2EE interview questions series.

Regards,

Also visit author’s blog for more Java/J2EE interview questions

Monday 24 October 2011

Java/J2EE interview questions: – Distinguish between State and Strategy pattern .

This is one of the most asked JAVA/J2EE interview questions.

So one can differentiate this design pattern and answer this as follows: -

Strategy
State
Family of algorithms is interchangeable which means that every algorithm does the same thing but in different way.States are not interchangeable.
Encapsulate each algorithm into a class.We encapsulate each state into a class in state pattern.
Descendants should prepared for most possible events.
Can only process a few events according to the state machine model.
Concrete strategies usually don’t know the others.
Usually concrete states know other concrete states.
More dynamic modeling.More static modeling.

Also see the following video on Buider Design Pattern for the preparation of JAVA/J2EE interview questions: -


Visit for Java/J2EE interview questions

Regards,

Visit for more author's other blogs on Java/J2EE interview questions


Monday 17 October 2011

Java/J2EE interview questions: - Is using StringBuffer always yields better performance than concatenation using +?

No, String Buffer need not yield a good performance over String concatenation using "+".

The type of concatenation plays a major role in deciding performance. If the concatenation can be completely resolved at coimpile time then String concatenation using + will yield better results
On the other hand if concatenation is done at run time using StringBuffer is more advisable, since StringBuffer objects are resolved at run time

Following are the two programs one showing compile time resolution and other run time and thus showing where StringBuffer will be useful.

Compile time resolution: 


This program takes String literals and performs concatenation using + as well as StringBuffer. Since the String literals could be resolved at compile time itself, this indicates a case of compile time resolution.


Code is self explanatory with proper comments given where ever required.
On executing the above code we get following result

Result:


Concatenation using + takes 0 millisecs
Concatenation using StringBuffer takes 63 millisecs
Performance using concatenation using + better than StringBuffer concatenation.

Conclusion: Since the above case if of compile time resolution performance using + concatenation yields a better result.

Run Time resolution:


Just a small modification to compile time code shown above can give us run time case for further analysis.

We change the concatenation used above to String conResult =""; conResult = conResult +"Welcome to the World of Java" +"Performance" +"@"+"questpond";

a similar change is done in StringBuffer . code snippet after change will look like





Result :


Concatenation using + takes 47 millisecs
Concatenation using StringBuffer takes 32 millisecs
Performance using concatenation using StringBuffer better than + concatenation.



Conclusion : the above case clearly shows StringBuffer giving more performance as compared to + concatenation at run time.

Overall final conclusion: Prefer StringBuffer whenever run time resolution concatenation is involved
View the following video on J2EE Design Pattern - Service Locator: -



Get more Java/J2EE interview questions