The differences between a process and Thread are:  A process is a program in execution and a thread is a single execution sequence within the process. For example when you open MS-Word for writing some document, you are actually executing a single process. Where the auto saves after a fixed   interval or spell checker are individual running threads...

Read More

As like other elements Threads are also Object in java. We have 2 ways to create a Thread. By extending the Thread class and overriding the run() method to specify its behavior. Creating a class that implements the Runnable interface and then creating an object of the Thread class by passing the Runnable object as a parameter to thread class constr...

Read More

In the following example we are going to create a thread by extending the Thread class. class TestThread extends Thread { private String name; /*calling the constructor of Thread class to set the name of the Thread */ public TestThread(String name) { super(name); } // overriding the run method public void ru...

Read More

First we will create a class by implementing the Runnable interface and override the run() method to specify the behavior. public class RunnableThread implements Runnable { private String name; public String getName() { return this.name; } public RunnableThread(String n) { this.name = n; } ...

Read More

The Thread class saves some information attributes that can help us to identify a thread, know its status, or control its priority. These attributes are:  Thread Id : A unique identifier for each Thread.  Name : This attribute store the name of Thread.  Prioroty : This attribute stores the priority of the Thread objects. Threads can have a priority...

Read More

Thread Id is a unique identifier to identify a Thread. While creating a new Thread the Thread class assigns a unique id to each Thread. Let us have a look into the implementation in Thread Class: /* * Thread ID: assigned to each Thread */ private long tid; /* for generating thread ID. Incremented and assigned * to each Thread at Creatio...

Read More

This attribute stores the name of Thread. While creating a Thread we can assign a name to the Thread. But some constructors in Thread class can create a Thread without taking a name as parameter. public Thread(){} public Thread(Runnable target) {} public Thread(ThreadGroup group, Runnable target) {} In these above cases Java assigns a name to t...

Read More

This attribute stores the priority of the Thread objects. Threads can have a priority between one and 10, where 1 is the lowest priority and 10 is the highest one. It’s not recommended to change the priority of the threads, but it’s a possibility that you can use if you want. In Thread class the min and max priority are defined as: /** * The mi...

Read More

The different states of a thread can be described by the following diagram.

Read More

New state – A Thread is considered to be in new state when created and initialized. Each Thread class constructors call the init() method to initialize a Thread. The method is defined as: /** * Initializes a Thread. * * @param g the Thread group * @param target the object whose run() method gets called * @param name...

Read More

Daemon threads are very low priority threads and normally only executes when no other thread of the same program is running. When there are no user’s threads running [threads where isDaemon() returns false] and all the other threads running are daemon threads; the JVM ends the program finishing these threads. The daemon threads are normally used as...

Read More

Synchronized keyword in Java is used to provide mutually exclusive access to a shared resource with multiple threads in Java. Synchronization in Java guarantees that no two threads can execute a synchronized method or block which requires the same lock simultaneously or concurrently Synchronization is built around an internal entity known as the in...

Read More

In multi-threaded environment several threads can access an Object to perform some tasks. Consider the standard producer consumer problem, where we have a list of items. We have a producer thread which produces and insert an item into that list and we have a consumer thread that consumes and remove the item from that list. So here the list is a sha...

Read More

The synchronized locking mechanism suffices for keeping threads from interfering with each other, but we also need a mechanism to communicate between threads. For this purpose we have 3 methods wait(), notify() and notifyAll() in java.lang.Object class and thus inherited by all classes. The wait() method lets one thread wait until some condition oc...

Read More

Yes, we can call run() method of a Thread class but then it will behave like a normal method. To actually execute it as a Thread, we need to start it using Thread.start() method which in turns call the run method.

Read More

We can use Thread.sleep() method to pause the execution of Thread for certain time. Note that the thread keeps the monitors it has acquired, once the thread awake from sleep, its state gets changed to runnable and based on thread scheduling the thread gets executed. As the thread keeps the monitor so no other thread can enter the synchronized block...

Read More

The differences between wait() and sleep() are as following: wait sleep Difference between wait() and sleep() object.wait() sends the current thread into the “Not Runnable” state, like sleep (), but with a twist. Wait is called on an object, not a thread; we call this object the “lock object.” Before wait () is called, the current thread must acqui...

Read More

wait(),notify() and notifyAll() may throw IllegalMonitorStateException – if the current thread is not the owner of this object’s monitor.

Read More

Static methods can also be declared synchronized. A Class is associated with every class. A static synchronized method acquires the lock of the Class object for its class. Two threads cannot execute static synchronized methods of the same class at the same time, just as two threads cannot execute synchronized methods on the same object at the same ...

Read More

The start () method starts the execution of the new thread and calls the run () method. The start () method returns immediately and the new thread normally continues until the run () method returns. The Thread class’ run () method does nothing so sub-classes should override the method with code to execute in the second thread. If a Thread is instan...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself