Thread
Java assigns to each thread that determines how that thread should be treated with respect to others. Thread priorities are integers that specify relative priority of one thread to another. As an absolute value a priority is meaningless. A higher priority thread doesn’t run any faster than a lower priority thread if it is the only thread running. Instead a thread’s priority is used to decide when to switch from one running thread to next. This is called context switch.Rules of Context switching:
- A thread can voluntarily relinquish control. This is done by explicitly yielding, sleeping or blocking on pending IO. In this scenario all other threads are examined and the highest priority thread that is ready to run is given the CPU.
- A thread can be pre-empted by a higher priority thread. In this case a lower priority thread that doesn’t yield the processor is simply pre-empted no matter what it is doing by a higher priority thread. Basically in a pre-emptive multi-tasking as soon as a higher priority thread wants to run, then it does.
How many ways a thread can be created in Java?
In Java, we create a thread by instantiating an object of type Thread. Java defines two ways in which this can be accomplished.1. Implementing Runnable:
The easiest way to create a thread is to create a class that implements Runnable interface. We can construct a thread in any object that implements Runnable. To implement Runnable, a class need only implement. A single method run() which is declared as follows:
public void run()
Inside run() we define the code that constitutes the new thread. run() establishes the entry point for another concurrent thread within the class.
Thread defines several constructors like Thread(Runnable Thread-Ob , String Thread-Name).After the new thread is created, it’ll not start running until we call it’s start() method which is declared within the thread. i.e. – start() executes a call to run().
class NewThread implements Runnable
{
Thread t;
NewThread()
{
t=new Thread(this,”Demo
Thread”);
System.out.println(“Child
Thread “+t);
t.start();
}
public void run()
{
try
{
for(i=5;i>0;i--)
{
System.out.println(“Child
Thread “+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println(“Child
Interrupted”);
}
System.out.println(“Exiting
Child Thread”);
}
}
class ThreadDemo
{
public static void main(String
args[])
{
new NewThread();
try
{
for(i=5;i>0;i--)
{
System.out.println(“Main Thread
“+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(“Main
Interrupted”);
}
System.out.println(“Exiting
Main Thread”);
}
}
2. Extending Thread:
The second way to create a thread is to create a new class that extends thread and then to create an instance of that class. The extending class must override the run() method which is the entry point for the new thread. It must also call the start() to begin the execution of the new thread.
class NewThread extends Thread
{
NewThread()
{
super(“Demo Thread”);
System.out.println(“Child
Thread”+this);
start();
}
public void
run()
{
try
{
for(i=5;i>0;i--)
{
System.out.println(“Child Thread
“+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println(“Child
Interrupted”);
}
System.out.println(“Exiting Child
Thread”);
}
}
class ExtendThread
{
public static void main(String args[])
{
new NewThread();
try
{
for(i=5;i>0;i--)
{
System.out.println(“Main Thread “+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(“Main
Interrupted”);
}
System.out.println(“Exiting Main Thread”);
}
}
- Choosing an approach:
Many Java programmers think that classes should be extended only when they are being enhanced or modified in some way. So, if we’re not overriding threads other method than run(), then it’s probably best to simply implement Runnable. Also implementing Runnable allows the class to inherit a different class.When we extend Thread class, each of our thread creates unique object and associate with it. When we implement Runnable, it shares the same objects to multiple threads. It is up to programmer to choose the approach for creating a thread.
Multithreading
Java provides built in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such program is called a thread. And each thread defines a separate path of execution. Thus multithreading is a specialised form of multi-tasking. Thread based multi-tasking is a type of multi-tasking. In thread based multi-tasking environment a thread is the smallest unit of dispatch able code.Advantages:
- In this environment a single program can perform two or more tasks simultaneously. For instance, a text editor can format text at the same time it is printing. As long as these two actions are being performed by two separate threads.
- Multi-tasking thread require less overhead than multi-tasking processes.
- Unlike processes, threads are not heavy weight tasks that require their own separate address space.
- Inter-process communication is expensive and limited. Whereas inter-thread communication is inexpensive.
- Context switching from one process to another is costly. Threads on the other hand are light weight.
No comments:
Post a Comment