How to set or change priority of Main Thread?

As already specified, you need to use setPriority() and getPriority() methods of Thread class to change and to retrieve priority of a Thread. However, first we need to get the Thread object of main Thread. This can be achieved using Thread.getCurrentThread(), which returns Thread object of the current Thread. Below is snippet of code public … Continue reading How to set or change priority of Main Thread?

Can Thread join() be used between two child Threads

Refer purpose of join method in multi threading , for basic details on join() method. Generally a parent Thread invokes join() method on one or more child threads, that means parent thread gets suspended, until its one or more of its child threads completes execution. As shown in below example it is possible to use … Continue reading Can Thread join() be used between two child Threads

purpose of join() method in Thread

join() method is defined in Thread class. join() method waits for a thread to die. That means, it causes the currently running threads to stop executing until the thread it joins with completes its task. class NewThread1 implements Runnable{ String name; //name of Thread Thread t; NewThread1(String threadname){ name=threadname; t = new Thread(this,name); System.out.println(“New thread:”+t); … Continue reading purpose of join() method in Thread

How to Set priority to a Thread

By default all the threads created, get medium priority, unless a specific priority explicitly is set to the thread. Below are the priority related constants provided in Thread class. Thread.MIN_PRIORITY (value is 0) Thread.NORM_PRIORITY (value is 5, which is default priority) Thread.MAX_PRIORITY (value is 10) Any numeric value between 0 and 10 can be set … Continue reading How to Set priority to a Thread

In Thread, purpose of join() and isAlive() methods

As known below are the methods available in Thread class setName() getName() setPriority() getpriority() sleep() start() join() isAlive() When to use join() method. Before we try to understand join(), lets see what is Daemon thread. A Daemon thread is a thread, which keeps on running in the background, even though its parent , has already … Continue reading In Thread, purpose of join() and isAlive() methods

How deadlock occurs in Threads?

In multithreading deadlock situation may occur, when two or more threads compete for two or more resource. Below is simple example, in which threads t1 and t2 compete to lock two objects obj1 and obj2, which end up in deadlock situation. After t1 and t2 threads are started, deadlock occurs in following way 1. Thread … Continue reading How deadlock occurs in Threads?

Create Thread using Anonymous inner class

As briefed, a Thread is light weight process. Below is an example, on how to create and start Thread, using Anonymous inner class. Create Anonymous inner class, which extends from java.lang.Thread and override public void run() method. public class ThreadAnonymousInnerClass { public static void main(String args[]) { Thread t1=new Thread(){ public void run(){ for(int i=0;i

What is Thread?

A Thread is a 1.Light Weight Process. 2.Flow of execution. 3.a Task which runs along with other Tasks. A Process under execution has below segments in memory Code, Data, Stack, Heap. But a thread may have only Stack or Heap, and other segments are shared with the Main thread. As already said, Thread is a … Continue reading What is Thread?