29/12/2019

Multithreading in Java :Studywow | Easy and Important Notes

Multithreading in Java :Studywow

Read Java Multithreading Concepts by Studywow.

Q1. What do you mean by multithreaded programming in Java?

Ans. In Java, multi-threading is a process of executing multiple threads simultaneously. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread and each thread defines a separate path of execution.
It enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum. They are mostly used in games, animation etc.

Q2. Explain the thread concepts in multithreading programming?

Or How does multithreading improve the performance of Java?

Ans. In multithreading programming, we need to follow the following concepts:
1. Thread synchronisation.
2. Interthread communication.
3. Thread deadlock.
4. Thread control.

1. Thread Synchronisation :

When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this synchronisation is achieved is called thread synchronisation.
The synchronised keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.
Syntax: 
synchronised(object) {
// statements to be synchronised

2. Interthread Communication :

Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via following methods :
(a) wait(): This method tells the calling thread to give up the monitor and go to sleep until
some other thread enters the same monitor and calls notify().
(b) notify(): This method wakes up the first thread that called wait() on the same object.
(c) notifyAll(): This method wakes up all the threads that called wait() on The highest priority thread will run first.
These methods are implemented as final methods in object, so all classes have them. All three methods can be called only from within a synchronised context.

3. Thread Deadlock:

A special type of error that you need to avoid that relates specifically to multitasking is deadlock, which occurs when two threads have a circular dependency on a pair of synchronised objects. For example, suppose one thread enters the monitor on object X and another  thread enters the monitor on obiect Y. If the thread in X tries to call any synchronised met
ead enters the monitor on object X. If the thread in X tries to call any synchronised method on Y, it will block as expected. However, if the thread in Y, in turn, tries to call any synchronised method on X, the thread waits forever, because to access X. it would have to release its own lock on Y so that the first thread could complete.

4. Thread Control:

Suspend, stop and resume while the suspend(), resume () and stop() methods defined by Thread class seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs and obsolete in newer versions of Java.
The following example illustrates how the wait() and notify() methods that are inherited from object can be used to control the execution of a thread.
Let us consider the program in which the new thread class contains a boolean instance variable named suspendFlag, which is used to control the execution of the thread. It is initialised to false by the constructor.
The run() method contains a synchronised statement block that checks suspendFlag. If that variable is true, the wait () method is invoked to suspend the execution of the thread. The mysuspend() method sets suspendFlag to true. The myresume () method sets suspendFlag to false and invokes notify() to wake up the thread. Finally, the main () method has been modified to invoke the mysuspend() and myresume () methods.
For example:
public class SimpleThread extends Thread {
private int count Down = 5;
private static int threadCount = 0;
public SimpleThread() {
super("" + ++threadCount);   // Store the thread name start();
}
public String toString() {
return "#" + getName() + ": "+count Down;
}
public void run() {
while(true) {
System.out.println(this);
if(--count Down == 0) return;
}}
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new SimpleThread();
}}


Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @studywow
(click on it or search "studywow" on facebook)
Twitter = @studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!

Multithreading in Java.