A Listener - a bean which will have a method annotated with and will assist in defining the task to be executed when a specific event is receivedīy default the listener method is executed synchronously, but can easily be made asynchronous by also adding the annotation.Īnother way to make the listener asynchronous is to add in the configuration a bean with a SimpleApplicationEventMulticaster and assign a TaskExecutor to it.A publisher - a bean which will publish the event using ApplicationEventPublisher bean.An Event - any object that extends ApplicationEvent can be used.Using Spring Events for async implementation is a step forward which also offers decoupling and an easy way to add new functionality without changing the existing one. Instead, we can specify how many threads it can have and it will reuse these threads during the lifetime of the application. They use different ThreadPools and are used because there’s no need to manually create a thread. However, when we do a thread.start() a new thread will be created.įor a better management of threads in JDK 1.5 we can find Executors. The difference is when the run method is called directly from a Runnable there won’t be a new thread created, instead it will run on the thread which is calling. Any class can implement Runnable and override the run() method or can extend Thread and do the same. The first way to implement async in Java is to use the Runnable interface and Thread class which is found from JDK 1.0. They can do it at the same time, neither they have to wait for one to finish in order for the other one to start. Multi-thread: I hire 2 cooks and they will boil the egg and toast the bread for me.In asynchronous I don’t have to wait for a task to be done in order to start one other task. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |