Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsLink TO UsSEO Tools


HOME

J2ee tutorials

Java introduction
Java basic
Java installation
Java Packages
Learn Applets
Java Threads
Java Gui component
pixels


Java Event Handaling
Java Streams and Files
pixels
Java Swings
Java JDBC
pixels
Java Network Programming
Java RMI
pixels
Java Servlets
Java javabeans
pixels
EJB
Struts
pixels


Java Thread



A thread is a single independent path of execution within a program. When a program runs, it starts executing the initial code and the subsequent methods and then continues the processing until the program is exited. Threads allow you do many things at a time .for example in a company all the tasks are completed at the same time by assigning them to different people. Each person does their job. In software usually have single processor which do the entire task. To do this work multitasking was developed. For example a person who is assigned some job as an administration person who does multiple things at a time. He is taking printout, reaping spreadsheet ext. in an operating system this is known as multithreading.



A program can have many threads. In multithread program each thread can work at different times asynchronously one after the other sequentially or at the same time (Synchronously). These thread operations are built into the java.lang thread class.

Creating threads in java



Java has both class and an interface that can be used to create threads applets or applications. java support for thread is found in the java.lang package in the thread class and one interface runnable.
There are two ways in which we can create thread in java

By implementing as runnable interface

By extending the thread class


Implementing runnable interface



Any class whose instances are intended to be executed by thread should implement the runnable interface. The class implementing the runnable interface must define a method of no arguments called run. The only method in runnable interface is public void run() which need to be overridden. All the task that thread is supposed to be executed is defined inside the run() method for example



Class threadname implements Runnable
{
int x;
String name;
Public threadname(String n)
{
name=n;
}
public void run()
{
for(x=0;x<10;x++)
{
System.out.println(name);
}
}
}
public class mainclass
{
public static void main (String args[])
{
threadname tc1=new threadname(“welcome”);
threadname tc=new threadname(“to freewebschools”);
Thread t1=new Thread(tc1);
Thread t2=new Thread(tc2);
t1.start();
t2.start();
}
}

threadname implements runnable interface and implements run() method inside the main class two instances of the threadname are created. The instances of the threadname need to call start() method. When first instances tc1 called start() method, new thread of control is created. This new thread calls the run() method which is defined in the threadname

the run() method is not sufficient for the thread to start running. To make a thread run you need to call a start() method present in the thread class. Runnable is implemented by the class thread on calling start() method the thread becomes eligible for scheduling, which means that jvm will start running this thread as soon as the cpu time is available, because more that one thread might be waiting for its chance to come. The scheduling of the thread depend on the priority of a thread which could range from 1 to 10.if two threads have same priority the execution of the thread depends upon platform.

The output of the above example shows “welcome” and “ to free webschools” in some arbitrary sequence.

Extending thread class



The second way of creating threads is by extending the thread class. The following extends the thread class to create thread.

Public class firstthread extends Thread
{
public void run()
{
System.out.println(“Threwad is running….”);
}
public void start()
{
run();
}
public static voiud main(String args[])
{
firstthread t=new firstthread();
t.start()
}
}

if you override start() method then you have to explicitly call run() method in case you do not override that start() method then super class start() method is called, which also explicitly call the run() method present in the super class.


Inform Friend About This Site

Your Name:

Friend's Name:

Friend's Email: