Posts

Showing posts from February, 2016

Java Interview : Multithreading Part Three

Image
Part three of Java Interview : Multi-threading Java 1.5 onwards a lot of useful utility classes tailor made to counter a lot of specific scenarios were introduced, i will write about some of the commonly used ones. Classes like Semaphore, CountDownLatch, CyclicBarrier, Phaser, Exchanger and the Executor Framework are the most famous ones. Semaphore class is modeled around the concept of the counting semaphore, where a set of permits are maintained. We have the acquire() method which tries to get a permit and blocks until it gets one. The release() method puts the permit back to the pool, here by increasing the permit count. Methods like tryAcquire() provide non-blocking alternative to get the lock. Binary semaphores have only one permit and thus a thread can either be in one state on or off at a single time. CountDownLatch is a utility that provides methods like await(), basically signalling the threads to wait at a point till the count comes down to 0, before proceedi...

Java Interview : Multithreading Part Two

This is the second post in the series  Java Interview : Multithreading. Critical section or a part of code which if processed by multiple threads at a time may have uncalled of consequences, forms the basis of synchronization utilities provided in Java programming language. The initial offering mostly worked around  synchronized, wait, notify, notifyAll, sleep, yield and interruption . In Java, synchronized is used to control access to a piece of code to keep the software behavior on expected lines. This keyword can be applied to a block of code or a method. It works something like this, if thread  alpha  is executing a piece of code guarded by synchronized, either a method or a block, and another thread  beta  wants to execute that code too,  beta   will be blocked and will need to get the lock to execute that code. Think like a two guys trying to get coffee from a counter, one will get blocked till other is served and if there is a to...

Java Interview : Multithreading Part One

This post is part of the Java Interview notes series. In Java there are 2 ways to create threads, one way is  to extend  Thread  class and objects of such a class are threads and run in a execution path of their own, you just override the  run()  method and call the  start()  method. Similarly another way to create a thread is implement  Runnable  interface and provide this reference to a Thread class object and call the start() method. ThreadFactory  interface implements the factory method and gives you a way to get a thread. You need to provide an implementation of the  newThread () method which returns a thread reference.You can provide custom behaviour in this factory to get statistics of the number of threads created and even control the number of threads which can be created using such a factory. Threads have getter/setters to work with thread related information like  ID , name ,  priority  and  st...

Java Interview Notes : Collection Part One

Image
In Java Collections are very important to understand and analysed  this will surely help you in completing your development tasks and also get through interviews. Collection and Map both with their generic versions are the most important interfaces and all other interfaces extend them to get the basic behavior up and running. Set, Queue and List extend Collection and even inherit Iterable interface, all the three can be “foreached”. Map even though a collection does not extend Collection, and is a Key/Value grouping. Iterator is the interface whose reference is received by Interfaces which extend Collection interface, hence you can call next(), hasNext() and remove() on ArrayList iterators etc. Iterator ConcurrentModificationException  is thrown by these iterators whenever they detect that the collection from which they were derived has been structurally changed hence the general-purpose Collections Framework iterators are known as fail-fast. So ...

Spring Framework Interview Notes : Part Two Wiring

Image
The Spring container is the guy responsible to get the train moving when working with the Spring framework.  The dependency magic gets the things working by somehow getting all the stuff up and running. Now somehow we need to indicate to the spring container as to where and how to “wire” these beans. Remember these beans are also known as components. Components combine to form the structure and thus make the functionality. The concept of component scanning or component finding comes into the picture, literally searching the applications to find the beans required to create these beans. @ComponentScan is the annotation used by giving the the base packages where these components are defined using annotation or xml . Spring Framework Interview Wiring You can also use Autowiring with its attributes, which finds the beans by-name or by-type and injects them appropriately. @Component, @ComponentScan, @Autowired, @Named,  @Bean  with requires and quali...

Spring Framework Interview Notes : Part One Core

Image
These is part of a set of posts which might help in getting up and running with Spring framework. I will try to be lucid and do away with all the complex junk the authors throw at you to make you buy their books. Apparently, Spring framework was the answer to the questions posed by complex world of Java enterprise programming brought to you by frameworks like struts and EJB. It was meant to make it easy to code large projects and thus take away the frustration of the beloved Java programmer. Now i am not sure how much you enjoyed copying and pasting the context files and hand-rolling those smoking hot beans but after some time you i found it similar to EJB 1.2 and Struts roll in one. A Spring component was the good old POJO without  sprinkled with any Spring framework classes, like Struts. Although now they have changed that by using the great new feature called Java Annotation, @Bean. We will not make your POJO depend on us has been changed to use us now and you cannot run...

Java Interview : Threads

Java came up with multi threading long time back, still the kind of response it invokes when this topic raises its head during interviews is not very encouraging. If you have attended an java interview, a couple of suppose to be very smart guys also know as lead developers will shower you with questions on multi threading, grilling you down to make sure what you are talking about, even asking you to write fork-join snippets to solve hadoop use cases. But if you are hired and you write a piece code using executor framework, you will be seen with suspicion generally reserved for people with casual shoes at cocktail parties. So here is post to start out thinking about threads. So a path of execution in java is known as a thread, appropriately an instance of Thread class. The main method runs in its own thread and starts up the program execution. If you implement the Runnable interface, you can run in a thread of you own. The Runnable interface has a run() method you implement your...

Starting Selenium Testing with Java and JUnit

Image
This is interesting way of testing web applications, most of the web applications are constantly changed due to new requirements wanted by your beloved business analyst, as most of these guys are technologically handicap changes are asked for even at the UAT stage. The victims of such foolish actions are the hardworking developers, who need to make sure the functionalities are coded and tested. The testing teams do not report bugs coming out of these changes as they do not complete the regression tests on the previously verified functionalities. So to done away with those saturday evening call that things are not working when sanity done by the users after production deployment selenium can be very helpful. In this post i will let you show you how to setup the selenium testing framework on your machine and run your first JUnit test. a) We need to download some files to get the framework up and running, click to this  link  to get to the selenium download page. Seleniu...

Java Interview : Whats the intern method in the String Class in Java?

In any programming language strings are the most used representation for data. From first name to feedback of users all are used in strings. So strings take a lot of memory and repeated use of same string leads to a state where applications might become slower or even start throwing OutOfMemory Errors. So why not keep these strings in a pool where if a string which is equal to a string in the pool can just use reference to the original string without creating a new string object. So comes the idea of a global String pool, a pool of strings kept in the Permgen space of the memory. Interning a string simply says that a string is put in a global pool. String first = "Alpha " + "Beta" ; String second = "Alpha " + "Beta" ; System . out . println ( first == second ) ; The above code returns true, compiler optimizing the code by pointing both the string reference to the same object. So what does the intern method does, Intern fi...