See my latest #vlog on #Python #programming - Multithreading in Python - Creating and Managing Threads
seen from China

seen from United States

seen from Malaysia
seen from United States
seen from United Kingdom

seen from South Korea
seen from China

seen from United States

seen from Italy
seen from China

seen from Malaysia
seen from Netherlands

seen from Malaysia

seen from United States
seen from Thailand

seen from United States
seen from Yemen

seen from Thailand

seen from Malaysia

seen from Thailand
See my latest #vlog on #Python #programming - Multithreading in Python - Creating and Managing Threads
Unnecessary multi threading? Sure. But, the threads are happy to do it! LOL. Via https://www.reddit.com/user/rzyua
Multi Threading
Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.
Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.
Life Cycle of a Thread
A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.
Read More
Following are the stages of the life cycle
New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.
Thread Priorities
Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).
Read More
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.
Create a Thread by Implementing a Runnable Interface
If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −
Step 1
As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method −
public void run( )
Step 2
As a second step, you will instantiate a Thread object using the following constructor −
Thread(Runnable threadObj, String threadName);
Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread.
Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method
Multi Threading
Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.
Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.
Life Cycle of a Thread
A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.
Read More
Following are the stages of the life cycle
New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.
Thread Priorities
Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).
Read More
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.
Create a Thread by Implementing a Runnable Interface
If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −
Step 1
As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method −
public void run( )
Step 2
As a second step, you will instantiate a Thread object using the following constructor −
Thread(Runnable threadObj, String threadName);
Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread.
Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method −
How to use ForkJoinPool in Java
Many of you have gone through google to find out on how to use ForkJoinPool for many use cases. But it’s hard to find the use case for a DB query. Here’s the simple example on how to use it.
Create a Recursive Task class like the following.
public class MongoQueryForkJoinTask<T> extends RecursiveTask<List<T>> {
private final int pageSize; private int pageIndex; private final Long totalCount; private final Query query; private final MongoTemplate mongoTemplate; private final Class clazz;
private static final Logger LOG = Logger.getLogger(MongoQueryForkJoinTask.class);
public MongoQueryForkJoinTask(int pageSize, int pageIndex, Long totalCount, Query query, MongoTemplate mongoTemplate, Class clazz) { this.pageSize = pageSize; this.pageIndex = pageIndex; this.totalCount = totalCount; this.query = query; this.mongoTemplate = mongoTemplate; this.clazz = clazz; }
@Override protected List<T> compute() { if(totalCount <= pageSize ){ LOG.info("Fetching records for Class ["+clazz.getSimpleName()+"] with pageSize ["+pageSize+"] and pageIndex ["+pageIndex+"], totalCount ["+totalCount+"]"); Pageable pageRequest = new PageRequest(pageIndex, pageSize); query.with(pageRequest); return mongoTemplate.find(query, clazz); } List<T> returnList = new ArrayList<>(); LOG.info("Executing all the tasks..."); for (int i=0;i<pageIndex;i++){ final MongoQueryForkJoinTask<T> task = new MongoQueryForkJoinTask<>(pageSize, i, Long.valueOf(pageSize), query, mongoTemplate, clazz); task.fork(); returnList.addAll(task.join()); } return returnList; } }
And use the same task to invoke via ForkJoinPool.
pool.invoke(new MongoQueryForkJoinTask<Employee>(1000, 10000, count, q, mongoTemplate, Employee.class));
Where q is the Mongo Query that we construct as per our requirements.
If we don’t bother about the pagination and all those stuff, this works good else we loose the parallelism as we did here. We are just forking it and immediately we are joining it.
PSA:: This blog loves to multi thread.
Using Spring Integration Futures
Using Spring Integration Futures #spring #java #springintegration #springframework
This past week I had a real world use case for using Spring Integration Futures. I was looking into optimizing the checkout experience for a large ecommerce website. Consider what happens in a large scale website when an order is submitted. Typically, you will see a process something like:
Validate the place order form information
Verify the address against an address service
Verify the credit…
View On WordPress