Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/ on line 2
Free Online Thread Model Tutorials in Multi Threading,Thread Model in Multi Threading
Search Site

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/ on line 2
J2SE [Java 2 Standard Edition]
Core Java Introduction
Exception Handling
Collection & Util Classes
Input & Output Streams
Adwance Windows Toolkit (AWT)
Swings (Adwance GUI)
Event Handling
Java Networking
Remote Method Invocation (RMI)
Java Database Connectivity (JDBC)
J2EE [Java 2 Enterprise Edition]
Java 2 Enterprise Edition Intoduction (J2EE)
Server Side Programming (Servlet)
Java Server Pages (JSP)
Enterprise Java Bean
MVC Framework (Struts)
Advance Framework (Springs)
Object-relational mapping (Hibernate)
eXtensible Markup Language (XML)
Asynchronous JavaScript and XML (AJAX)
Monthly News Letter
Free SCJP Samples
Interviews Questions For Freshers
Interviews Questions For Experinced
Most Visited Links
Most Freaky Links

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/vir007/public_html/ on line 2

What is a Thread

Thread is a light weighted process or we can say that thread is a path of execution before understanding thread it is important to understand the concept of multitasking which can be process based or thread based.

What is Multitasking
Multitasking is a way to perform more than one task at the same time.In multitasking, only one processor is involved but it switches from one process to another so quickly that it gives the appearance of executing all of the processes at the same time.
There are two basic types of multitasking: preemptive and cooperative. In preemptive multitasking, the O.S parcels out processor time slices to each process . In cooperative multitasking, each process can control the processor for as long as it needs it. If a process is not using the processor , however, it can allow another process to use it temporarily.

There are two types of multitasking
Thread based- In thread based multitasking, a thread is the smallest unit of code that can be dispatched by the scheduler .This means that a single program can have two or more tasks which can be executed simultaneously .Multithreading is a  Thread based Multi Tasking
Process based- A Process is a program which is under execution Process based multitasking allows to execute two or more programs concurrently. In process based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.

What is Threading
A Thread is an independent, concurrent path of execution through a program.Threading is a facility to allow multiple activities to execute simultaneously within a single process.Some threads can run for the entire life of the applet, while others are alive for only a few milliseconds .A thread also creates a new thread or kills an existing one. Threads run in methods or constructors.The applet methods start(), paint(), and so on are all called by underlying threads. These threads are created by the Web browser. When there is a mouse click or a repaint() has been called, the underlying thread calls the appropriate thread in the applet.

Uses  Of Threads
1. Threads are used in applets that may take a while to initialize. The applet may need to do something like wait for an image to be loaded. A thread is used so that the system thread can handle other events.
2. Threads are used to do tasks that happen repetitively. A common example of this is found in animations. Every few milliseconds a new frame is shown. A thread displays a frame, sleeps for a while, and then repeats the process.
3. Threads are used to handle events. An example of this is a mouse click. If the user clicks the mouse, a new thread is created to render a new frame in an image.
4. Threads are used to do more than one thing at once. One thread controls an animation, while another does a computation

Why Do We Need MultiThreading

Make the UI more responsive - Event-driven UI toolkits, such as AWT and Swing, have an event thread that processes UI
events such as keystrokes and mouse clicks.
AWT and Swing programs attach event listeners to UI objects. These listeners are notified when a specific event occurs, such as a button being clicked. Event listeners are called from within the AWT event thread.
If an event listener were to perform a lengthy task, such as checking spelling in a large document, the event thread would be busy running the spelling checker, and thus would not be able to process additional UI events until the event listener completed. This would make the program appear to freeze, which is disappointing for the user.
To avoid stalling the UI, the event listener should hand off long tasks to another thread so that the AWT thread can continue processing UI events (including requests to cancel the long-running task being performed) while the task is in progress.

Take advantage of multiprocessor systems -
Multiprocessor systems are much more common than they used to be. Once they were found only in large data centers and scientific computing facilities. Now many low-end servers systems -- and even some desktop systems -- have multiple processors. Modern operating systems, including Linux, Solaris, and Windows NT/2000, can take advantage of multiple processors and schedule threads to execute on any available processor.
The basic unit of scheduling is generally the thread; if a program has only one active thread, it can only run on one processor at a time. If a program has multiple active threads, then multiple threads may be scheduled at once. In a well-designed program, using multiple threads can improve program throughput and performance.

Simplify modeling -
In some cases, using threads can make your programs simpler to write and maintain. Consider a simulation application, where you simulate the interaction between multiple entities. Giving each entity its own thread can greatly simplify many simulation and modeling applications.
Another example where it is convenient to use separate threads to simplify a program is when an application has multiple independent event-driven components, for example, an application might have a component that counts down the number of seconds since some event and updates a display on the screen. Rather than having a main loop check the time periodically and update the display, it is much simpler -- and less error-prone -- to have a thread that does nothing but sleep until a certain amount of time has elapsed and then update the on-screen counter. This way the main thread doesn`t need to worry about the timer at all.

Perform asynchronous or background processing -
Server applications get their input from remote sources, such as sockets. When you read
from a socket, if there is no data currently available, the call to will block until data is available. If a single-threaded program were to read from the socket, and the entity on the other end of the socket were never to send any data, the program would simply wait forever, and no other processing would get done. On the other hand, the program could poll the socket to see if data was available, but this is often undesirable for performance reasons.
If, instead, you created a thread to read from the socket, the main thread could perform other tasks while the other thread waited for input from the socket. You can even create multiple threads so you can read from multiple sockets at once. In this way, you are notified quickly when data is available (because the waiting thread is awakened) without having to poll frequently to check if data is available. The code to wait on a socket using threads is also much simpler and less error-prone than polling would be.

Implementing Multithreading

Multithreading can be implemented using Thread class or implementing Runnable interface.
Java cannot support the mltiinheritence.if use the Thread class to create ant thread at the same time u cant extends the any other class. Suppose if u developed any application using AWT or SWING u must be extends JFrame class at the same time u cant extends thread class .so at that time we can go to the Runnable interface. Whereas Thread class provide us more methods which are defined and Runnable is having run() method which developer need to override while using thread.

Multi Threading implement

There are 7 constructors in Thread Class
1. String- The name of the new thread is the parameter String. A thread can get its name by calling Thread.getName().
2. ThreadGroup- The new thread will belong to the group specified by the parameter ThreadGroup. A ThreadGroup can be used to organize a thread.
3. Runnable- The Runnable parameter is an object that has implemented the Runnable interface. The thread will start executing in the run() method of the Runnable parameter when Thread.start() has been called.

Following Demo Demonstrate an example using Thread Class

class SimpleThread extends Thread{
 String s=null;
 SimpleThread(String s1){
 public void run(){
public class UseThread{
 public static void main(String args[]){
  SimpleThread m1=new SimpleThread("Thread started....");

Following Demo Demonstrate an example using Runnable Interface

class SimpleThread2  implements Runnable{
    Thread t;
    String s=null;
    SimpleThread2(String s1)
        t=new Thread(this);
    public void run(){System.out.println(s);
public class UseRunableThread{
    public static void main(String args[]){
        SimpleThread2 m1=new SimpleThread2("Thread started....");
        Thread t=Thread.currentThread();