Submit Your Article
Home Articles News Tutorials Videos Add An Article
Topics: Design Photoshop Programming PHP CSS Java Database Web Development Javascript Ajax
– Close + Open

Find Out More About DevWebPro!

Sign up for the newsletter


» Terms & Conditions

Welcome to the New DevWebPro!

DevWebPro Includes:
  Hundreds Of Tutorials   Developer News
  Unique Gadget Videos   Tons of Topics to Discuss
  Expert Advice   We Will Publish Your Articles

Creating And Using Java Threads

By: Vaibhav Pandey
Monday, November 23rd, 2009
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook

Multi threaded applications are the base of all modern computing applications. Threads save lot of time and keeps utilizing computing resources to the optimum levels. It is important for a Java developer to understand how to create Threads.

There are two ways by which you can create Threads:-

1. By extending Thread class

2. By implementing Runnable Interface

Creating Threads by extending Thread class

The java. lang. Thread class is used to construct and access individual threads in a multi threaded application. Thread class supports many methods that obtain information about the activities of a Thread,set and check properties of a Thread and cause a Thread to wait,be interrupted or be destroyed. Extending the Thread class enables your class to run in a separate new call stack. Just like the main Thread.

Example:-

class MThread extends Thread{

// code goes here

}

Creating Threads by Implementing Runnable Interface

Extending Thread class for creating Threads is no doubt a bad OO Practice because since Java doesn’t support multiple Inheritance,if you extend one class and also need to incorporate features of Other class then what you are supposed to do?

Let us Consider a scenario where we want to create an Applet which has some Thread capabilities,now if you extend Thread then from where applet methods come from and if you extend Applet class then from where the Thread capabilities come from. This is the reason why the Java creators decided to give some extra flexibility by providing us with Runnable Interface which consists of only a single method called run(). All the Thread related code goes inside run() method and Thread run() method is invoked when we call start() method on Thread object. This run() method starts a new call stack in this case but it has to be invoked to get things working.

Example:-

class MThread implements Runnable{

public void run(){
//Thread stuff goes here
}

}

NOTE:-Consider the context of Applet and Thread scenario ,when you use Runnable interface,the Thread becomes a part of the Applet and grants full access to its data members and methods. The subclass of Thread is limited to access the public components of any class. Therefore ,when the Thread depends strongly on the components of Applet class use Runnable Interface. Use this concept in general terms not only for Applet context.

Comments

Topics: ,

About the Author:
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.
DevWebPro is an iEntry Network ® publication - © 1998-2010 All Rights Reserved