Tuesday, 31 January 2012

Thread.yield() in Java Thread

The Thread.yield() method is essentially used to notify the system that the current thread is willing to "give up the CPU" for a while. The thread scheduler may select another runnable thread with the same or greater priority to run instead of the current thread.

What yield() is supposed to do is make the currently running thread move back to runnable to allow other threads of the same priority(and those of greater priority) to get their turn.

So the intention to use yield() is that equally distribute the CPU time among equal-priority threads.But there is no guarantee for that ,because the yielding thread itself may be selected to run again.i.e the yielding thread is eligible to compete with other equal-priority threads for CPU time.

A yield() won't ever cause a thread to go to the waiting/sleeping/ blocking
state. At most, a yield() will cause a thread to go from running to runnable, but
again, it might have no effect at all.

Sunday, 29 January 2012

join() method in Java Thread with Examples

The join() method of a Thread instance can be used to "join" the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If join() is called on a Thread instance t, the currently running thread will block until the thread instance t has finished execution. 
See one example 
class MyThread extends Thread {

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("From mythread i=" + i);
        }
    }
}

public class Main {
    public static void main(String args[]) {
        MyThread mythread = new MyThread();
        System.out.println("Before starting mythread");

        mythread.start();
        try {
            mythread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

Thread.sleep() method in Java with Examples

Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Thread.sleep() sends the current thread into the "
Not Runnable" state for a specified amount of time.Also,if another thread calls t.interrupt(), it will wake up the sleeping thread.

sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method).It is a common mistake to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

One thread can't make sleep another thread t using t.sleep method.Only the current thread which is executing the statement 't.sleep' will go for sleep.

Sleep doesn’t cause the thread to lose ownership of the acquired monitors.The thread keeps the monitors it has acquired i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method.

Thread.sleep method is available in two different formats as given below

Uses of Thread.sleep() in Java with Examples


Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Thread.sleep can also be used to implement some graphical functionalities i.e.to make some delay in displaying some texts,frames,windows etc.

Consider one real-time scenario, you and your friend has got a box of apples,both of you are started to eat apples.Suppose you can eat more fast than your friend,what will happen ?.Your friend won't get enough apples.

This situation is called 'Starvation' in Computer Science,where some threads won't get enough CPU time compared to some other concurrent threads.See the code given below


Saturday, 28 January 2012

Inter Thread Communication in Java with Example

I hope you have gone through  my previous post  Inter-Thread Communications in Java  before reading it.

We have a classic 'Producer-Consumer' problem to explain the use of Inter-Thread communications in java,where one thread is producing some data and another is consuming it .The producer has to wait until the consumer is finished before it generates more data.

Let us start with an incorrect implementation of  Producer-Consumer problem, where we are just using the mercy of synchronized method .


// An incorrect implementation of a producer and consumer.
class Q {
    int n;

    synchronized int get() {
        System.out.println("Got: " + n);
        return n;
    }

    synchronized void put(int n) {
        this.n = n;
        System.out.println("Put: " + n);
    }
}