Saturday, 8 October 2016

Yield Vs Join and Sleep Vs Wait

In this post, I am going to discuss about some of the important methods of Thread class and their usage.These methods are also actively asked in interviews.There is very little chance that you would have had the experience of actually using them at the starting level.It is expected from all the interviewee to have basic idea about these methods. Before diving straight into the topic, first let us gather some background information which will help us to understand them better.

Understanding thread priorities and thread scheduling

JVM is a preemptive, priority-based scheduler for threads.Each java thread has a well defined priority.A developer can change the priority of the thread as per the requirements.If the user doesn't set the priority of the thread, a default priority is set for each thread which is same for all in a particular java application.This priority value has its significance in multi threaded environment. It helps the JVM to decide which thread to execute when more than one threads are waiting for a common resource.This is why it is called priority based scheduler. The operating system generally chooses the thread with the highest priority. If any high priority thread is ready for the execution then the scheduler interrupts (preempts) the low priority thread. However, there is a catch, the OS may choose to wait the high priority thread and continue wit the lower priority thread(Ya ya... I know, OS some time can be moody -_- ).

  • Remember that all the threads carry normal priority when a priority is not specified.
  • Priorities can be specified from 1 to 10. 10 being the highest, 1 being the lowest priority and 5 being the normal priority.
  • Remember that the thread with highest priority will be given preference in execution. But there is no guarantee that it will be in running state the moment it starts.
  • It can be assumed that the currently executing thread might have the higher priority when compared to the threads in the pool who are waiting for their chance. However as we have learned this is not 100% guaranteed.
  • It is the thread scheduler which decides what thread should be executed.
  • setPriority(Thread.MIN_PRIORITY) method can be used to set the priority of the thread.
  • Remember that the priorities should be set before the threads start method is invoked.
  • Thread class provides some predefined constants for setting the priority, MIN_PRIORITY,MAX_PRIORITY and NORM_PRIORITY.

sleep() is a method which is used to hold the process for the time you wanted but in case of wait() method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().

The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution, generally.

Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. 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. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

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 lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the “wait list” associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.


yield() is defined as following in Thread.java.

/**
  * A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore
  * this hint. Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilize a CPU.
  * Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.
  */

public static native void yield();

A yielding thread tells the virtual machine that it’s willing to let other threads be scheduled in its place. This indicates that it’s not doing something too critical. Note that it’s only a hint, though, and not guaranteed to have any effect at all.

join() method

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, the currently running thread will block until the Thread instance has finished executing.


Giving a timeout within join(), will make the join() effect to be nullified after the specific timeout. When the timeout is reached, the main thread and taskThread are equally probable candidates to execute. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.

Like sleep, join responds to an interrupt by exiting with an InterruptedException.

Thursday, 6 October 2016

The Two Synchronized method Problem

The Two Synchronized method Problem.

Problem Statement: A class has two synchronized (non-static)methods M1 and M2. Two different threads T1 and T2 simultaneously trying to access the methods M1 and M2 respectively.
Is is possible?


import java.util.concurrent.TimeUnit;

public class TwoSyncMethods {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  
  final TwoSyncMethods tsm = new TwoSyncMethods();
  new Thread(new Runnable() {
   
   @Override
   public void run() {
    try {
     tsm.m1();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }, "One").start();
  
   new Thread(new Runnable() {
   
   @Override
   public void run() {
    try {
     tsm.m2();//Calling other synchronized method on same object
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    
   }
  }, "Two").start();
  
 }

 public synchronized void m1() throws InterruptedException{
  System.out.println("Acquired by "+Thread.currentThread().getName());
  TimeUnit.SECONDS.sleep(2);  
  System.out.println("Released by "+Thread.currentThread().getName());
  
  
 }
  
 public synchronized void m2() throws InterruptedException
        {
   System.out.println("In Sync "+Thread.currentThread().getName());
   TimeUnit.SECONDS.sleep(5);
   System.out.println("In Sync After time out "+Thread.currentThread().getName());
  
 }
 
}

Answer. No,this is not possible.Two different methods can not access the two different synchronized method of the same class simultaneously.

Explanation : Every object has a monitor associated with it.For a thread to enter the synchronized block (also known as critical section), the thread must be the owner of the lock on the monitor. In other words, a thread must acquire a lock on the monitor if it wants to enter critical section.Now, since both the methods are non-static, therefore if they are called on the same object, the 1st thread, requesting the lock on monitor,will acquire the lock on the monitor while the 2nd thread will have to wait for lock to be released.

How to make it possible? As stated above, since same object is used to call both the methods, therefore once the lock is acquired by any one of the threads will cause the other thread to wait. So the solution to this to make two different objects of the class and then call the two methods simultaneously from the different threads using the different objects. This way, both the threads will acquire lock on the monitor of two different objects.


import java.util.concurrent.TimeUnit;

public class TwoSyncMethods {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  
  final TwoSyncMethods tsm1 = new TwoSyncMethods();
                final TwoSyncMethods tsm2 = new TwoSyncMethods();
  new Thread(new Runnable() {
   
   @Override
   public void run() {
    try {
     tsm1.m1();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }, "One").start();
  
   new Thread(new Runnable() {
   
   @Override
   public void run() {
    try {
     tsm2.m2();//Calling synchronized methods on different objects
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    
   }
  }, "Two").start();
  
 }

 public synchronized void m1() throws InterruptedException{
  System.out.println("Acquired by "+Thread.currentThread().getName());
  TimeUnit.SECONDS.sleep(2);  
  System.out.println("Released by "+Thread.currentThread().getName());
  
  
 }
  
 public synchronized void m2() throws InterruptedException
        {
   System.out.println("In Sync "+Thread.currentThread().getName());
   TimeUnit.SECONDS.sleep(5);
   System.out.println("In Sync After time out "+Thread.currentThread().getName());
  
 }
 
}

What is the state of the thread waiting for other thread to exit critical section?
Blocked

public class StateCheck {

    public synchronized void m1() {
        try { Thread.sleep(2000); }
        catch (InterruptedException ie) {}
    }

    public synchronized void m2() {
        try { Thread.sleep(2000); }
        catch (InterruptedException ie) {}
    }

    public static void main(String[] args) throws InterruptedException {
        final StateCheck t = new StateCheck();
        Thread t1 = new Thread() { public void run() { t.m1(); } };
        Thread t2 = new Thread() { public void run() { t.m2(); } };

        t1.start();
        Thread.sleep(500);

        t2.start();
        Thread.sleep(500);

        System.out.println(t2.getState());
    }
}
A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

Sunday, 25 September 2016

repeatFront

Home
Goto Problem
Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).
repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI"

public String repeatFront(String str, int n) 
{
     String res=str.substring(0,n);
     for(int i=1;i<n;i++)
     {
            res=res+str.substring(0,n-i);
     }
     return res;
}

Saturday, 10 September 2016

loneSum

Goto Problem

Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0

public int loneSum(int a, int b, int c) {
if(a==b) { if(a==c) return 0; else return c; }
if(a==c) { return b; } if(b==c) return a; else return a+b+c; }

Monday, 5 September 2016

repeatSeparator

Home
Goto Problem

Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"

 public String repeatSeparator(String word, String sep, int count) 
 {
    String res=word;
    if(count==0) 
       return "";
   for(int i=1;i<count;i++)
   {
      res=res+sep;
      res=res+word;
   }
   return res;
}

Sunday, 28 August 2016

prefixAgain

Home
Goto Problem
Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().
prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false

public boolean prefixAgain(String str, int n)
{
   String prefix=str.substring(0,n);
   return(str.substring(n).contains(prefix));

}

Friday, 26 August 2016

getSandwich

Home
Goto Problem

A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.
getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""

public String getSandwich(String str)

{
  if(str.indexOf("bread")==str.lastIndexOf("bread")) 
      return "";
  return(str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread")));
}