Java | Multithreading

05 min read

Multithreading:

Process:

  • Any program under execution.
  • All the programs that we write are saved in secondary memory. When we give the command to execute any particular program, it is loaded from the secondary memory into main memory and is now ready for execution.

In the current computing environment almost all the Operating Systems, support loading multiple programs into the main memory. All the programs being loaded into the main memory will be in ready state (ie., ready for execution). The CPU scheduler (again a software) will decide which job should be given the CPU, for execution, among the several programs that have been loaded. Its decision is based on various criteria such as the priority of the task, completion time, and so on. The program which is allotted the CPU time will move from ready state running state and start execution. During its execution, it may be blocked and later on once again moved to the ready state, due to various reasons. The program which is currently being executed is called the process.

 

Multiprocessing :

  • The capability to execute multiple processes at the same time. This is termed as concurrency.
  • In a multiprocessor environment, each process may execute one process and thus multiple processes may be executed concurrently.
  • This way of running several processes at the same time (concurrently) in a multiprocessor environment is referred to as physical concurrency.

(eg) Listening to music using VLC player & editing the MS word document, at the same time.

 

Multiprogramming:

  • How is concurrency achieved in a Uniprocessor Environment?

When a process is being executed by the single processor available, it may be blocked due to I/O operations (because 1/0 operations are much slower than computations) and till the process resumes its execution after finishing the 1/0 operation, the CPU has to be idle.

This idle time of the CPU can be made use of, for executing another process which is in the ready state. So, even before the program finishes its execution, CPU switches to another process, to avoid the idle time. This process of CPU being switched from one process to another to utilize the idle time of the CPU efficiently is called context switching.

Thus logical concurrency at least (though not physical concurrency) can be achieved even in a Uniprocessor system.

 

Logical Concurrency:

  • The frequent context switching between the processes to keep the CPU busy always may appear as if multiple processes are executed concurrently, although they are not executed at the same time, in reality.

 

Multithreading:

  • What is a thread?

A thread is a lightweight process. [Light weight processes are the ones which share the same address space — same heap memory and only the stack memory will be different for lightweight processes. Thus inter-process communication and context switching involves lesser overhead compared to heavyweight processes].

Thus thread-based multitasking will be more efficient than process-based multitasking.

In process-based multitasking, multiple processes are executed at the same time. In thread-based multitasking, if a single process contains 2 or more portions that can be run concurrently, then each portion is called a thread and all the threads can execute concurrently. This way of executing multiple threads of the same program concurrently is also called ( logical or physical concurrency).

This way of executing multiple threads of the same program concurrently is also called multithreaded programming.

 

How is multithreaded programming supported in Java?

  • Two or more threads can be spawned (created) from within the same program and all these threads can be made to execute simultaneously.

Creation of thread: A thread can be created in two ways. Either by Implementing Runnable Interface or by Extending Thread Class.

A runnable interface contains a single method run( ) which can be defined in the class which implements this. If the class is not going to include any additional functionality, it is better to implement the Runnable interface than to extend the Thread class.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error