Wednesday, June 15, 2016

Multiple Threads example in java

Multiple Threads Acting on Single Object:

Yes multiple threads acting on single objects can give you unreliable results.Let see how?

/**
 * @author Abhinaw.Tripathi
 *
 */
class Reserve implements Runnable
{
int avilable=1;
int wanted;

public Reserve(int i)
{
wanted=i;
}

@Override
public void run()
{
System.out.println("Avaiable births: " +avilable);
if(avilable >=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted + " Berths reserved for" + name);
try
{
Thread.sleep(1500);
avilable=avilable-wanted;
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

public class UnsafeApp
{
/**
* @param args
*/
public static void main(String[] args)
{
       Reserve obj=new Reserve(1);
       Thread t1=new Thread(obj);
       Thread t2=new Thread(obj);
     
       t1.setName("First Person");
       t2.setName("Second Person");
     
       t1.start();
       t2.start();
}

}

Output:

Avaiable births: 1
Avaiable births: 1
1 Berths reserved forFirst Person
1 Berths reserved forSecond Person

And it absurd result.We can make it safe by making it synchronize.

What is Thread synchronization?

Ans: when a thread is already acting on an object preventing any other thread from acting on the same object is called Thread Synchronization or Thread Safe . The object on which the threads are synchronized is called synchronized object.

So, how can we synchronize the object?There are two ways of doing this.
  • Using synchronized block: 
synchronized(object)
{
  statements;
}

  • Using synchronized keyword:
synchronized void display()
{
  statements;
}

What is difference between synchronized block and synchronized keyword?
Ans: Synchronized block is useful to synchronize a block of statement.Synchronized keyword is useful to synchronize an entire method.

For Example:

/**
 * 
 */

/**
 * @author Abhinaw.Tripathi
 *
 */
class Reserve implements Runnable
{
int avilable=1;
int wanted;

public Reserve(int i)
{
wanted=i;
}

@Override
public void run()
{
synchronized (this)
{
System.out.println("Avaiable births: " +avilable);

if(avilable >=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted + " Berths reserved for" + name);
try
{
Thread.sleep(1500);
avilable=avilable-wanted;
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}

public class SafeApp
{
/**
* @param args
*/
public static void main(String[] args)
{
       Reserve obj=new Reserve(1);
       Thread t1=new Thread(obj);
       Thread t2=new Thread(obj);
     
       t1.setName("First Person");
       t2.setName("Second Person");
     
       t1.start();
       t2.start();
}


}

Output:

Avaiable births: 1
1 Berths reserved forFirst Person
Avaiable births: 0

Thread Class Methods:

Listing out some important methods of java.lang.Thread class:
To create a Thread ,we can use the following forms:

Thread t1=new Thread();
Thread t2=new Thread(object);
Thread t3=new Thread(object,thread name);
  • To know the currently running thread:
        Thread t=Thread.currentThread();
  • To start a thread
       t.start();
  • To stop execution of a thread for a specified time:
       Thread.sleep(milliseconds);
  • To get the name of the Thread
        String name=t.getName();
  • To set a new Name to a Thread:
         t.setName("new name");

  •    To get priority:

           int priority=t.getPriority();
  • To set priority of a Thread:
      t.setPriority(int priority);

  • To test if a thread is still alive:
        t.isAlive();
  • To wait till a thread dies:
      t.join();

Notes on Thread Priority:

  1. Max priority=10
  2. Min priority=1
  3. Normal Priority=5





No comments: