Friday, February 13, 2009

Thread creation

Multitasking:
* Executing several tasks simultaneously is the concept of Multitasking.
* The main objective of multitasking is to decrease response time of the system, so that the performance of the system will increase.

Process Based Multitasking:-

* Executing several tasks simultaneously where each task is a independent program.
* Example typing a java program in the Editor, playing MP3 player, Downloading a file from the internet
* All the tasks are independent of each other and executes simultaneously.
* This type of multitasking is useful at OS level.
* CGI fallows process based multitasking.

Thread Based Multitasking:-

* Executing several tasks simultaneously, where each task is a separate inependent part of the same program. That independent part is called the Thread.
* Servlets fallowThread Based MultiTasking.
* Java itself provide support for multithreading by introducing several library classes.
* We can use in games/animation programme.
* Creating a thread in two ways.

1. By extending Thread
2. By implementing Runnable

Methods used to prevent a thread from execution:

* yield()
* join()
* sleep()
* Synchronization
* InterThread communication (wait, notify, notifyAll)

Defining, Instantiating and starting Thread by extending Thread class:
class MyThread extends Thread
{
public void run()
{
System.out.println("child job”);
}
}
Here run method is the heart of thread where you have to define the job.
class Sample
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start(); //starting a thread
System.out.println(“Main thread job”);
}
}
The above program executes well.it gives the output mainthread job,child job,.........
Difference between t.start() & t.run()?
• If we call t.start(), it will create a new Thread and that thread is responsible for execution of run().
• If we call t.run(), mo new thread will create, the main thread only will execute run(), just like a normal method.
• In case of t.start() output we can’t expect
• In case of t.run() output Child Thread 10 times fallowed by main thread 10 times.

Thread class start():-
public void start()
{
1. Registering our thread with the thread scheduler then only thread scheduler allocate CPU and memory type of resources for this new thread also.
2. calls run()
}
If we over ride start method in the above example:
--->public void start()
{
System.out.println(“start method”);// start method 1 time, main thread 10 times
}
--->public void start()
{
super.start();
System.out.println(“start method”);// start method child & main thread alternatively
}
class MyThread extends Thread
{
public static void main(String a[])
{
MyThread t=new MyThread();
t.start();
}
}
• Thread class run() has empty implementation that’s why we are getting no output.
Defining, Instantiating and starting Thread by implementing Runnable
Ex: class MyRunnable implemts Runnable
{
public void run()
{
for(int i=0; i<=10 ;i++)
System.out.pritnln(“child thread”);
}
}
class ThreadDemo
{
public static void main(String a[])
{
MyRunnable r=new MyRunnable();
r.start(); // compile time errror
Thread t=new Thread(r); ---> Target Runnable
for(int i=0 ; i<=10 ; i++)
{
System.out.println(“main thread”);
}
}
} In the above example instead of r.start we place r.run(); //childThread (no new thread will create ) In the above example we place the

* t.start(); //valid (new thread will create child thread)
* t.run(); // child thread (no new thead will create)

• Among the above two probabilities of creating thread it is highly recommended to use implements Runnable mechanism.
• In the first case ( extends thread), our thread class extends Java.Lang.Thread, hence no chance of extending any thing else. Hence we are missing, the key benefit of oop’s concept ie., inheritance.
Lifecycle of Thread:





• Once the thread is started, there is no chance of starting the same thread once again.
Violation leads to RTE saying Illegal Thread State Exception”.
• Thread t= new Thread();
t.start(); //no o/p

Thread class Constructors:
1. Thread();
2. Thread(Runnable r);
3. Thread(String name);
4. Thread(Runnable r, String name);
5. Thread(ThreadGroup g, String name);
6. Thread(ThreadGroup g, Runnable r);
7. Thread(ThreadGroup g, Runnable r, String name);
Ex:- // Test king
class MyThread extends Thread
{
public void run()
{
System.out.println(“run”);
}
}
class ThreadDemo
{
public static void main(String a[])
{
MyThread t= new MyThread();
Thread t1=new Thread(t);
t1.start();
// t1.run(); ----> It is just like a method no new thread will create
}
}
Setting & getting the name of a thread:
Thread class contain the fallowing methods for setting & getting the name of the Threads..

* public final String getName();
* public final void setName(String s);

0 comments:

Post a Comment