Understanding Multithreading in Java

Java provides built in support for multithreaded programming. A Multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a Thread. Each thread defines a separate path of execution. Multithreading is a specialized form of Multitasking.

Multitasking is divided into two types. They are

(1) Process based: Here two or more programs runs concurrently. You can run Windows calculator and a Text editor (Notepad) at the same time.

(2) Thread based: A single program can perform two or more tasks simultaneously. For example, text editor can print while formatting is being done.

Multithreading enables you to write very efficient programs that make maximum use of CPU, because idle time can be kept to minimum. This is especially important for the interactive networked application because idle time is common. In a traditional single threaded environment, your program has to wait for each of the tasks to finish before it can proceed to the next one. This will result in idle time. Multithreading lets you gain access to this idle time and put it to good use.

Java’s Model of Thread

One thread can pause without stopping other parts of your Program. Multithreading allows animation loops to sleep for a second between each frame without causing the whole system to pause. When a thread blocks in a java program, only the single thread that is blocked pauses. All other threads continue to run.

Also note the following points regarding threads.

(1) A thread can be running. It can be ready to run as soon as it begins execution
(2) A running thread can be suspended which temporarily suspends its activity
(3) A suspended thread can be resumed
(4) A thread can be blocked when waiting for a resource. (sleeps for a while)

At any time a thread can be terminated, which means the thread stops. Once stopped it cannot be resumed.

Thread priorities

Thread priorities are integers that specify the relative priority of one thread to another. Higher priority thread doesn’t run any faster than a lower priority thread. Instead a thread’s priority is used to decide when to switch from one running thread to the next This is called as context switch. A thread can have a maximum priority of 10 and a minimum of 1. Normal Priority is 5.

Thread class Methods

start() –> Starts execution of the thread.
stop() –> Terminates the current thread.
suspend() –> Suspends the thread.
resume() –> Restarts the suspended thread.
sleep() –> This method suspends execution of the executing thread for the specified number of milliseconds. It can throw an interrupted Exception.

getName() –> Obtains a thread name.
getPriority() –> Obtain a thread’s priority.
Isalive() –> Determine if a thread is still running.
wait() –> Waits for a thread to terminate.
run() –> Entry point for the Thread.

When a java program starts up, one thread begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. Main thread will be the last thread to finish execution. When the main thread stops
your program terminates.

Creation of a thread

You can create a thread in one of the following ways

(1) You can implement the Runnable Interface and apply its run() method.

(2) You can extend the Thread class itself.

(1) Implementing Runnable Interface: The easiest way to create a thread is to create a class that implements the Runnable interface. To implement Runnable, a class need only implement a single method called run(). The Format of that function is public void run().

2. Extending Thread: The second way to create a thread is to create a new class that extends the Thread class and then to create an instance of this class. This class must override the run() method which is the entry point for the new thread.

Synchronization in Java

1. Two or more threads accessing the same data simultaneously may lead to loss of data integrity. In order to avoid this java uses the concept of monitor. A monitor is an object used as a mutually exclusive lock.

2. At a time only one thread can access the Monitor. A second thread cannot enter the monitor until the first comes out. Till such time the other thread is said to be waiting.

3. The keyword Synchronized is use in the code to enable synchronization and it can be used along with a method.

Inter Thread Communication

There may be cases where one of the created threads require data from another thread. Java achieves this through the usage of wait(), notify(), notifyAll() methods of the object class.

wait() –> This method waits indefinitely until it receives a notification
notify() –> This wakes up a single thread waiting on the objects monitor.
notifyAll() –> This wakes up all the threads waiting on the objects monitor.

4 Comments

  1. RerbPremexpek

Leave a Comment