Saturday, 13 July 2019

Thread Concepts

To Find the Thread is alive or not, we can use the below method.

final boolean isAlive() 
To determine whether a thread has finished, first you call isAlive() on thread, that will more commonly use to wait for a thread to finish is called join().

final void join() throws Interrupted Exception.
This will make the thread to finish its process.

notify()
wakes up thread that called wait() on the same object.

notify all()
wakes up all the thread that called wait on the same object.

What is a Race Condition in Thread.

A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data.

Below is the example for the Race Conditions in the thread

package com.searchendeca.thread.sample;

class MultiThreadNonSyncDemo {


//For Sync Use the Synchronized Keyword Here
 void call(String pName){
System.out.print("[" + pName);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("Interrupted");
e.printStackTrace();
}
System.out.println("]");
}

}
class Caller implements Runnable {
String msg;
MultiThreadNonSyncDemo target;
Thread t;
public Caller(MultiThreadNonSyncDemo targ, String s) {
target = targ;
msg = s;
if(s.equals("Hello3")) {
t = new Thread(this);
t.start();
t.setPriority(1);
}
else {
t = new Thread(this);
t.start();
}
}
public void run() {
target.call(msg);

}
}
class MainMultiSyncJoin {

public static void main(String args[]) {
MultiThreadNonSyncDemo target = new MultiThreadNonSyncDemo();
Caller m1= new Caller(target, "Hello1");
Caller m2= new Caller(target, "Hello2");
Caller m3= new Caller(target, "Hello3");
try {

m1.t.join();
m2.t.join();
m3.t.join();
System.out.println("**One is Alive::"+m1.t.isAlive());
System.out.println("**Two is Alive:"+m2.t.isAlive());
System.out.println("**Three is Alive:"+m3.t.isAlive());
}catch(InterruptedException IE) {
IE.printStackTrace();
}
}
}

Output:

[Hello1[Hello2[Hello3]
]
]
**One is Alive::false
**Two is Alive:false
**Three is Alive:false


Here all the three threads start and execute and wanting to prove among them self.

synchronized: This comes to play as a solution for the race conditions.

To avoid the race condition we can use the below two methods .

1. using the synchronized methods.
2. using synchronized statements.

1. using the synchronized methods.
//For Sync Use the Synchronized Keyword Here
synchronized void call(String pName){
System.out.print("[" + pName);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("Interrupted");
e.printStackTrace();
}
System.out.println("]");
}

Update the following snippet int the above program.

Output:

[Hello1]
[Hello3]
[Hello2]
**One is Alive::false
**Two is Alive:false
**Three is Alive:false

2. using synchronized statements.
In the above demo program defined alter the run method like this.Update the following snippet int the above program.

public void run() {
synchronized (target) {
target.call(msg);
}

Output:

[Hello1]
[Hello3]
[Hello2]
**One is Alive::false
**Two is Alive:false
**Three is Alive:false

No comments:
Write comments