Basic of Multithreading programming

Multithreading is one the most interesting topics in computer computer science and programming. Of course it is also difficult and cumbersome for programmers even professional ones. Doing multithreading programming requires some prior knowledge regarding the thread and process. This post dedicates to Java multithreading programming and I tried as much details as I could here. Now let’s proceed.

In computer each program basically runs as one process and in modern computers multi processing is possible because of advancement in CPUs capabilities which more scientifically they are called multiprocessors CPUs. For instance, a core i7 CPU consists of 4 cores, this means 4 processes can be executed by the CPU concurrently.

But the question is prior to multiprocessors CPUs how computer could run multi programs?

The answer is so simple, using multithreading. So what is thread?

Process is a heavy and big unit so scientists they have broken the process unit to smaller one which is called thread. In other words, one process is consist of multi threads and thread is so called the smallest executable and lightest portion of a program. But even in the real environment there is nothing such as multithreading on a single core CPU. It is only time scheduling of the CPU that switches between threads very fast and make this illusion as if threads are running at the same time. In conclusion the introduction, a single CPU processor is only able to run a single thread at the same time but since thread is very lightweight in comparison with process, the act of switching between threads is done very fast in such a way that delay cannot be recognizable.

Now let’s focus on Java as programming language for doing multithreading programming. The first question to ask is whether Java supports multiprocessing and multithreading?

The answer is no. Java does not support multiprocessing, it only supports multithreading. In fact, all Java programs are running in a single process which is JVM. This fact implies that if the main (JVM) process terminated for any reasons all Java applications would be terminated as well.

There are many resources for Java multithreading which I have listed some at the end of this post, but the best practice to learn anything is to consider a real world problem and try to resolve it by programming. For multithreading I have considered about queue processing system with creating the following case study.

There is a need to implement snail postal system emulator. In this emulator the system (server) receives messages (mails) from various sources in a single queue (mailbox) and after categorizing messages each category should be delivered by the relevant post person to relevant recipients. The categorization algorithm is done based on the address of the recipients and those that are in the same postal code area. The postmen work in the same time but on different category, however, they must share the same excel file to mention number of letter they have delivered, this is shared among them but one person can only edit it at the same time. Additionally, after finishing the entire job, they should all back to the office and then security person in charge in the office can close the office.

The above example is the real world problem which multithreading can be highly applied on it. Now lets discuss the above scenario in more technical way.

All mails are going to the same channel but categorization should be done to assign each letter to correct postal code, this area can be done with multithreading but let’s keep the first example simple and do this portion with single threading and do the rest of the scenario with multithreading.

After categorizing the letters, each postman should do his job separately which means they are independent from each other just they share a file in cloud. Based on what mentioned this part of scenario refers to multi threading and locking and unlocking (excel file). Each postman work can be implemented in a separate thread.

And the last section refers to make security person depends/wait to other postmen to close the office. This is also part of multithreading which is called join/dependency and notify.

That is all for today, in the next post I will add the source code of the scenario for better understanding. For time being please refer to following resources to know more about multithreading.

References

http://www.tutorialspoint.com/java/java_multithreading.htm

http://beginnersbook.com/2013/03/multithreading-in-java/

http://docs.oracle.com/javase/tutorial/essential/concurrency/

http://www.javatpoint.com/multithreading-in-java

http://tutorials.jenkov.com/java-concurrency/index.html

https://www.udemy.com/java-multithreading/

https://howtoprogramwithjava.com/java-multithreading/

Send your idea and information to kasra@madadipouya.com

1 Comment

Leave a Reply