Wednesday, December 12, 2012

Multithreading in Java !!!!!


A multiprocessor system can actually do more than one thing at at time but with Java threads, it can appear that you are doing several things simultaneously. Execution can move back and forth between stacks so rapidly that you feel as though all stacks are executing at the same time. 

Thread t = new Thread(r);

Thread is a class in the java.lang package. And this package is imported by default even without package declaration.    A Thread object represents a thread of execution, you will create an instance of class Thread each time you want to start up a new thread of execution.


Three states of new thread


  • When thread is new !

A thread instance has been created but not started. In other words, there is a Thread object but no threa of execution.


  • When thread is in Runnable state !  (t.start();)

When you start the thread, it moves into the runnable state. This means the thread is ready to run and just waiting for its chance to be selected for execution. 


  • When thread is in Running state !
Only the JVM thread scheduler can make that decision. You can sometimes influence that decision but you can not force a thread to move from runnable to running. 


The thread scheduler can move a running thread into blocked state for a variety of reasons. The scheduler will move the thread out of the running state until something becomes available. It can put the thread in sleep by sleep(). You can force the currently running thread to leave the running state and give chance to another  thread to run. In the sleep() method, that's for sure that sleeping thread will not become the currently running thread before the length of sleeping time has expired. 

And how will you apply the sleep() method, that also simple, just write down Thread.sleep(1000); This mean the current thread will not be execute till 1 second. Everything we write down in this method is in milliseconds. 

When you pass a Runnable to a Thread constructor, you are really just giving the Thread a way to get to a run() method. You are giving the Thread its job to do.


___________________________________________

Here I am giving the example of Thread which is taken from Head First Java book for your reference : 

In this example we took two thread and give there name and also set small scenario like what happen if one of the thread sleep or wake up that is runnable and what happen if there is not enough money for them in bank account.



class BankAccount{

private int balance = 100;

public int getBAalance(){
return balance;
}
public void Withdraw (int amount){
balance = balance - amount;
}
}



public class RyanAndMonicaJob implements Runnable {

private BankAccount account = new BankAccount();

public static void main(String[] args) {


RyanAndMonicaJob theJob = new RyanAndMonicaJob();
Thread one = new Thread(theJob);
Thread two = new Thread(theJob);

one.setName("Ryan");
two.setName("Monica");
one.start();
two.start();

}

public void run(){
for(int x = 0; x<10;x++){
makeWithdrawl(10);
if(account.getBAalance() < 0){
System.out.println("Overdrawn!");
}
}
}

private void makeWithdrawl(int amount){

if(account.getBAalance() >=amount){
System.out.println(Thread.currentThread().getName()+" is about to withdrawl.");
try{
System.out.println(Thread.currentThread().getName()+ " is going to sleep.");
Thread.sleep(500);
}catch (InterruptedException ex){ex.printStackTrace();}
System.out.println(Thread.currentThread().getName()+" woke up.");
account.Withdraw(amount);
System.out.println(Thread.currentThread().getName()+" completes the withdrawl.");
}
else{
System.out.println("Sorry,not enough for " + Thread.currentThread().getName());
}
}
}


_____________________________________________

Output:


Ryan is about to withdrawl.
Monica is about to withdrawl.
Ryan is going to sleep.
Monica is going to sleep.
Ryan woke up.
Monica woke up.
Ryan completes the withdrawl.
Monica completes the withdrawl.
Ryan is about to withdrawl.
Monica is about to withdrawl.
Ryan is going to sleep.
Monica is going to sleep.
Ryan woke up.
Ryan completes the withdrawl.
Ryan is about to withdrawl.
Ryan is going to sleep.
Monica woke up.
Monica completes the withdrawl.
Monica is about to withdrawl.
Monica is going to sleep.
Ryan woke up.
Ryan completes the withdrawl.
Ryan is about to withdrawl.
Ryan is going to sleep.
Monica woke up.
Monica completes the withdrawl.
Monica is about to withdrawl.
Monica is going to sleep.
Ryan woke up.
Ryan completes the withdrawl.
Ryan is about to withdrawl.
Ryan is going to sleep.
Monica woke up.
Monica completes the withdrawl.
Monica is about to withdrawl.
Monica is going to sleep.
Ryan woke up.
Ryan completes the withdrawl.
Ryan is about to withdrawl.
Ryan is going to sleep.
Monica woke up.
Monica completes the withdrawl.
Sorry,not enough for Monica
Sorry,not enough for Monica
Sorry,not enough for Monica
Sorry,not enough for Monica
Sorry,not enough for Monica
Ryan woke up.
Ryan completes the withdrawl.
Overdrawn!
Sorry,not enough for Ryan
Overdrawn!
Sorry,not enough for Ryan
Overdrawn!
Sorry,not enough for Ryan
Overdrawn!
Sorry,not enough for Ryan
Overdrawn!

_____________________________________________

No comments:

Post a Comment