Posts

Java @Async

In this article, we’ll explore the asynchronous execution support in Spring – and the @Async annotation. Simply put – annotating a method of a bean with @Async will make it execute in a separate thread i.e. the caller will not wait for the completion of the called method. Enable Async Support Let’s start by enabling asynchronous processing with Java configuration – by simply adding the @EnableAsync to a configuration class: @Configuration @EnableAsync public class SpringAsyncConfig { ... } The enable annotation is enough, but as you’d expect, there are also a few simple options for configuration as well: annotation – b y default, @EnableAsync detects Spring’s @Async annotation and the EJB 3.1 javax.ejb.Asynchronous ; this option can be used to detect other, user-defined annotation types as well mode – indicates the type of advice that should be used – JDK proxy-based or AspectJ weaving proxyTargetClass – indicates the type of proxy that sho...

Java Async Tasks

Asynchronous programming is very popular these days, primarily because of its ability to improve the overall throughput on a multi-core system. Asynchronous programming is a programming paradigm that facilitates fast and responsive user interfaces. The asynchronous programming model in Java provides a consistent programming model to write programs that support asynchrony. This article discusses the best practices that should be followed while writing code to perform asynchronous operations in Java. What Is Asynchrony and Why Is It Needed? Asynchronous programming provides a non-blocking, event-driven programming model. This programming model leverages the multiple cores in your system to provide parallelization by using multiple CPU cores to execute the tasks, thus increasing the application's throughput. Note that throughput is a measure of the amount of work done in unit time. In this programming paradigm, a unit of work would execute separately from the main ap...

Multithreading

Multithreading in java is a process of executing multiple threads simultaneously. Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation etc. Advantages of Java Multithreading 1) It doesn't block the user because threads are independent and you can perform multiple operations at same time. 2) You can perform many operations together so it saves time . 3) Threads are independent so it doesn't affect other threads if exception occur in a single thread. Life Cycle of a Thread A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java ...