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 proceeding through a point of execution. So if you initialise a countdownlatch with 3, three threads need to reach a point in execution call countDown() to bring the counter to 0 and then proceed through the await() method.
Similar to the CountDownLatch, we have the CyclicBarrier class which awaits for the threads too, but comes with the added feature of resetting the counter and causing a BarrierBroken Exception. Also it comes with an overloaded constructor which takes a Runnable reference, which runs once the counter has been brought down to Zero.
Phaser class, as by the intuitively given name is useful for dynamically registering parties and synchronizing the threads at specific points so that operations are completed in phases. Generally this is a useful class when the functionality achieves the target in phased manner.
Exchanger is a synchronization utility which is a point at which threads can pair and swap elements within pairs.So in simple terms 2 threads come to this point and exchange their data with one another.
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...
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...
Comments
Post a Comment