Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
In the previous Threads in Java we discussed about what are threads and how to implement them. In this weblog we will discuss why should we use threads and what problems may arise in a multithreaded programs.

Definition

Multithreading means starting multiple threads of execution in a single Java program.

Advantages of Multithreading:

Speed: threads increase performance by allowing your code to be executed in all the available processors in a multiprocessor machine.

Responsiveness: In a single processor machine, tasks requiring long waiting times (eg. network access, disk read, floating point calculation) can be placed on a separate thread. Thus the processor need not have to wait and response time of the application increases.

Problem:

Synchronization Problem

Multi threading may result in data corruption if multiple threads are manipulating the same please of data simultaneously.

Deadlock

Deadlock is a condition where two or more threads are blocked waiting for each other in such a way that none of them can proceed.

Livelock

Livelock is a condition where two or more threads are unable to make any progress because they are too busy responding to each other. Solution (Thread Synchronization): To prevent the synchronization problem you need to identify which block of code might be accessed by multiple threads simultaneously causing data inconsistencies and then you need to protect the code using a synchronized block as shown below.

synchronized block:



In Java synchronization problem is prevented by monitors. For every object there is a monitor associated with it. A thread that needs access to an object's fields has to obtain the object's associated monitor before it can access it, and when the thread is done with the operations on the object it has to release the monitor. A monitor thus blocks a second thread until the first thread releases it.

synchronized methods:



On invocation of a synchronized method a thread automatically acquires monitor for the method's object till the method returns. The monitor of method's object is released when the method returns (even if the return was caused by an uncaught exception).

Using synchronized keyword with a constructor definition is a syntax error. Constructors are by default synchronized as the thread that creates an object only has the access to the object while it is being created.

Reentrant Synchronization

A problem may occur if a synchronized code, directly or indirectly reinvokes a method that also contains synchronized code, and both sets of code use the same monitor. A thread would therefore remain waiting for ever for the monitor acquired by itself. Thus a thread is allowed to reacquire the same monitor (or lock) multiple number of times to achieve reentrant synchronization.

Interprocess Communication

Join

Using join method a thread can wait for the completion of another thread. If rt is the currently running thread, then rt.join(); causes the thread (containing the code) to pause until rt's thread terminates execution.

Interrupt

Interrupts are the way to indicate a thread that it should stop it current task and do something else.

If rt is the currently running thread, then

rt.interrupt();

causes interrupt in the rt thread. To query whether the thread is being interrupted use the following method.

rt.isInterrupted();

4 Comments