Friday, October 29, 2010

What is Multithreading in java examples

Multithreading,as the name itself tells that it is regarding,multi tasks(thread).In short Multithreading is a specialized form of multi tasking.
what is a thread?
Threads are separate parts of execution which are functionally independent of each other.

What is multitasking?
Single user multi process processed by a single processor.
Now what is a thread in java ?
In simple terms, a thread is a program's path of execution.Programs written without multithreading  run as a single thread, causing problems when multiple events or actions need to occur at the same time.
For instance, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. 
How to create a thread in java ?

Team who developed Java graciously designed two ways of creating threads
  1. By implementing  Runnable interface,in program.
  2. By extending Thread class,in your program.
Easiest  way to create a thread is to create a class that implements Runnable interface.Runnable abstracts a unit of execution code.Any way I will give you both methods,you can choose from them.
How to implement Runnable ?
Just you need to implement Runnable using implement,and add run(),

 public void run();

Inside run() you will define code that constitutes new thread.But it not that run() is only for that you can add other things as variable declaration,calling other methods.Thread end when run() returns.

 After creating a class that implements Runnable, second steps is...
 Instantiate an object of type Thread.Thread defines several constructors one we use is..

Thread(Runnable threadOb,String threadName)
threadOb ---> is an instance of a class that implements Runnable.
threadName ---> the name of new thread.
After creating new thread,it will not start running until you call its start().
Lets see an example so that you will understand better.

/* this is sample Thread */
class A implements Runnable{                                 //here I am implementing
Thread t;                                                                     //initializing object of type Thread
t=new Thread(this,"Demo thread");                       //creating new thread
t.start();                                                                      //calling start()
public void run(){
for(int i=0;i<8;i++){
catch(InterruptedException e){
System.out.println("Out of loop");
class MultiThrd0{
public static void main(String ea[]){
new A();
System.out.println("This is main()");

It is completed now compile it run it.
I guess no need of explaining,but let me do about few things..
Here in program every thing is clear first implement Runnable,then create Thread using constructor and then call start().
In Thread(this,"Demo thread"); "this" keyword used for new thread to call the run() on this object.
And start() starts the run().

Now coming to next method,i.e.. extending  Thread.I will give you a sample method..As it is not practiced more..
class A extends Thread{
super("Demo");//In place of "Demo" any string can be written.
System.out.println("Child thread" + this);
}public void run(){
public static void main(String jkkl[]){
new A();
Above you can add try and catch blocks...In main()..

In a Multithreaded program,often the main thread must be the last to finish running.In fact,for some older JVMs,if main thread finishes before a child thread,then Java run time system may hang.

Other posts--->  Simple Applet  ,  How to import packages ,   Robot java class      Total Java tutorial

No comments:

Post a Comment