Core Java - Multithreading - 2.
Saravanan B
Posted on March 1, 2023
Using Runnable interface --
public class RunImpl implements Runnable {
@Override
public void run() {
System.out.println("Processed the checks");
}
public static void main(String[] args) {
RunImpl ri = new RunImpl();
Thread t = new Thread(ri);
t.start();
}
}
By using the runnable interface, we can extend the other class if need no need for implementation of thread class.
Yield Method --
public class ThreadYield extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Child Thread");
Thread.yield();
}
}
}
public class ThreadYieldImpl {
public static void main(String[] args) {
ThreadYield ty = new ThreadYield();
ty.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread");
}
}
}
It will wait for main thread to executed and execute the thread.
Thread Intrrupt -- It will intrrupt the thread.
public class Interuppted extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("I am a lazy Thread");
Thread.sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Got Interupted");
}
}
}
public class InteruptedImpl {
public static void main(String[] args) {
Interuppted ip = new Interuppted();
ip.start();
ip.interrupt();
System.out.println("End of main method");
}
}
output --
End of main method
I am a lazy Thread
Got Interupted
We can only interrupt the thread in a sleep mode.
Synchronization - If two thread is trying to access same object there might be a chance of data getting corrupted.
In Synchronization the thread trying to access first thread will get access, second thread has to wait till the first thread completes the task.
Class level lock - If we mark a synchronized block as static and the block is class level locked.
Interthread Communications - wait () notify () notify all ().
T1 invokes wait method it will wait and t2 starts work on thread and notify once work done and again t1 completes its work.
public class ThreadInter extends Thread {
int total;
@Override
public void run() {
System.out.println("Child thread is calculating sum");
synchronized (this) {
for (int i = 0; i < 10; i++) {
total += i;
}
this.notify();
}
}
}
public class InterImpl {
public static void main(String[] args) throws InterruptedException {
ThreadInter ti = new ThreadInter();
ti.start();
synchronized (ti) {
System.out.println("Main Thread is waiting");
ti.wait();
System.out.println("Main Thread notified");
System.out.println(ti.total);
}
}
}
Thread group is something we can set values and do operation on parent thread and child will get the values.
public class Threadgroup {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
ThreadGroup parent = new ThreadGroup("Parent");
System.out.println(parent.getName());
ThreadGroup child = new ThreadGroup(parent, "child");
System.out.println(child.getParent().getName());
System.out.println(child.getName());
}
}
Output --
main
system
Parent
Parent
child
Here in this example, we are going to set child priority as 4 and the class which inherits child as get priority 4.
ThreadGroup parent = new ThreadGroup("Parent");
System.out.println(parent.getName());
ThreadGroup child = new ThreadGroup(parent, "child");
System.out.println(child.getParent().getName());
System.out.println(child.getName());
Thread thread1 = new Thread(child, "Thread1");
child.setMaxPriority(4);
Thread thread2 = new Thread(child,"Thread2");
System.out.println(thread1.getThreadGroup().getName());
System.out.println(thread1.getPriority());
System.out.println(thread2.getPriority());
Here the child thread priority is set to 4 after thread1 creation so the thread1 still have the default priority and thread2 will have the updated priority.
Daemon Thread -- Runs in background and assists other thread example garbage collector runs in background and cleanup the objects its not using.
isDeamon method is used to check and returns true if true.
setDeamon is used to set thread as deamon.
main thread cannot be set as deamon thread.
Deadlock -- Multiple thread waiting on each other resources. Example method1 access and do task same time method2 access and do task in method 1 both methods are synchornized.
Posted on March 1, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.