Java Made Simple: What is Thread? Why threads required?


What is Thread? Why threads required?

Summary: By the end of this tutorial "What is Thread? Why threads required?", gives meaning of thread and why a thread is required in computers?

1. What is a Process?

A program under execution is known as process. A program (or a process) contains number of statements (called instructions).

2. When an execution of a process can stop?

While the process execution is going on, if some obstacle comes, the whole process execution stops for a while. The obstacles may arise for many reasons like downloading files, transferring data to a printer etc. Another common obstacle from the actual user perspective (view) is keyboard input. If the keyboard input (like scanf()) is not given, the whole execution stops. Now what microprocessor is doing (think no other process in on hand to processor)? It is simply idle without any work. Okay, so what? The result is user gets late output. As an Architect of a language, what can be done (to get earlier output)? The only answer is THREADS.

3. What is thread? Explain non-technically in simple English?

This concept is very confusing for a very fresher. I get simply in my own style, just follow. We know a program contians number of statements and if an obstacle like keyboard input comes (if input is not given immediately), the whole process execution is blocked (cursor blinking). Now what shall I do? I purposefully divide the statements into a number of blocks. For example, if the program contains 100 statements, I divide them into three blocks containing 33, 33 and 34 statements each. How to divide we see later. So, now the program contains three blocks of statements. I would like to appiont a supervisor to supervise the execution or control the block statements. For three blocks, I want three supervisors. Right, I appointed. I want to give a designation for the supervior. The designation is "Thread". Now, I got "three threads" to supervise "three blocks" of statements.


What is Thread? Why threads required?

A thread is a part of a process. A thread lives within a process. No process, no thread. To understand better, we cannot keep our brain outside for sometime and place it back; it is because brain is a part of human body. Similarly, a thread cannot be there without a process. Infact, a process is divided into a number of threads.

Now let us define a thread; "a thread is a single sequential flow of control within a process". It is something confusing. Let us be clear. As said earlier, a thread comprises of a block statements. These statements are executed one-by-one sequentially. Every thread maintains its own execution context within a process context area. We can say, a thread is a sub process. For execution, one thread does not depend on other; every thread is independent of other. This tutorial gives you how to divide a process into a number of threads or to say, how to create threads, how to assign different tasks, priorities and synchronization

4. What is Thread? Why threads required? Explain technically?

The above explanation is given in a layman type of English. Now let us talk in a technically way being a computer language learner.

  1. A process can be divided into a number of blocks of statements called threads.
  2. A thread responsible for the execution of its block of statements.
  3. A thread lives within a process. That is, if no process, no threads.
  4. We can treat a thread as a subprocess within a process.
5. What is the advantage of threads? Why threads required in Java?

If no threads exist in a process, if some interruption comes, the whole process stops. This type of programming is known as monolithic programming. If threads exist, if one thread stops its execution, the microprocessor takes other thread for execution without sitting idle. Okay; if that thread also stops, the processor goes to another thread. The advantage of shifting like this, between the threads, is simply reducing the microprocessor idle time. The result is earlier output to the user.

6. What is Thread? Why threads required? Give some realtime examples where multithreading is used?
  1. Best example is Railway reservation where number of passengers booking tickets accessing the same server.
  2. Downloading a file while typing a MS-Word document.
  3. Listening to music while a graphic image is being edited.
  4. Doing some activity on the system while a large document is being printed.

For more explanation with figures refer:

1. Multithreading – Concepts
2. Java Thread Basics

Leave a Comment

Your email address will not be published.