wait()
, notify()
and notifyAll()
will be in thread class, then each thread has to be aware of status of another thread and that does not make sense as each thread runs independent of other thread and has no specific knowledge about other thread.wait()
is called, so that thread can wait on some condition. When condition is met, then thread has to give up the lock.IllegalMonitorStateException
.sleep()
and yield()
method on current executing thread. If there is in wait state, you can not call these methods.run()
method of a Thread class but it will behave like a normal method and a new thread will not be created to execute the run()
method. In this case the run()
method will be executed in the same thread which called the run method. To actually execute it in a new Thread, we need to start it using Thread.start()
method. setDaemon()
and isDaemon()
.start()
method on the thread object that causes the thread to begin execution and it's the Java Virtual Machine that calls the run method of the thread.wait()
, notify()
, and notifyAll()
methods.start()
method.start()
method. However, the thread is not yet selected by the thread scheduler.run()
method exits.public class ClassLevelLockExample
{
public void classLevelLockMethod()
{
synchronized (ClassLevelLockExample.class)
{
//DO your stuff here
}
}
}
public class ObjectLevelLockExample
{
public void objectLevelLockMethod()
{
synchronized (this)
{
//DO your stuff here
}
}
}
join()
method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. Join method is overloaded in Thread class in the following ways.join()
method is invoked, the current thread stops its execution and the thread goes into the wait state. The current thread remains in the wait state until the thread on which the join()
method is invoked has achieved its dead state. If interruption of the thread occurs, then it throws the InterruptedException.public final void join() throws InterruptedException
sleep()
method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time. There are two methods of doing so.public static void sleep(long milliseconds)throws InterruptedException
public static void sleep(long milliseconds, int nanos)throws InterruptedException
Working of sleep() method
sleep()
method, it pauses the execution of the current thread for the given time and gives priority to another thread(if available). Moreover, when the waiting time completed then again previous thread changes its state from waiting to runnable and comes in running state, and the whole process works so on till the execution doesn't complete.public class Multithread1 extends Thread
{
public void run()
{
try {
System.out.println("thread is executing now........");
} catch(Exception e) {
}
}
public static void main (String[] args) {
Multithread1 m1= new Multithread1();
m1.start();
m1.start();
}
}​
thread is executing now........
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at Multithread1.main(Multithread1.java:13)
synchronized (object)
{
//statement to be synchronized
}
java.util.Timer
is a utility class that can be used to schedule a thread to be executed at a certain time in future. Java Timer class can be used to schedule a task to be run one-time or to be run at regular intervals.java.util.TimerTask
is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.public void addShutdownHook(Thread hook){}
Runtime r=Runtime.getRuntime();
r.addShutdownHook(new MyThread());
finalizer()
because there are very fewer chances that shutdown hooks not run.halt(int)
method of Runtime class.put()
method unless it gets full and consumer thread takes resources from the queue using take()
method until it gets empty. But if a thread tries to dequeue from an empty queue, then a particular thread will be blocked until some other thread inserts an item into the queue, or if a thread tries to insert an item into a queue that is already full, then a particular thread will be blocked until some threads take away an item from the queue. public class ThreadLocalExp
{
public static class MyRunnable implements Runnable
{
private ThreadLocal<Integer> threadLocal =
new ThreadLocal<Integer>();
@Override
public void run() {
threadLocal.set( (int) (Math.random() * 50D) );
try
{
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(threadLocal.get());
}
}
public static void main(String[] args)
{
MyRunnable runnableInstance = new MyRunnable();
Thread t1 = new Thread(runnableInstance);
Thread t2 = new Thread(runnableInstance);
// this will call run() method
t1.start();
t2.start();
}
}​
10
33
10 33
public interface Callable<V>
compute()
, forEach()
, forEachEntry()
, forEachKey()
, forEachValue()
, merge()
, reduce()
and search()
methods.newWorkStealingPool()
method to create a work-stealing thread pool using all available processors as its target parallelism level.