Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What are the ways to define classes that can be run as
threads?



What are the ways to define classes that can be run as threads?..

Answer / ranganathkini

1. Have the class extend the java.lang.Thread class and
override the run() method. Example:

public class MyThread extends Thread {
// override the run method
public void run() {
// .. the thread code
}

public static void main( String[] args ) {
MyThread mt = new MyThread();
mt.start();
}
}

2. Have the class implement the java.lang.Runnable interface
and implement the run() method. Then create a new instance
of java.lang.Thread and pass the class instance reference as
a parameter to the constructor of the Thread class. Example:

public class MyThread implements Runnable {
public void run() {
// .. thread code here
}

public static void main( String[] args ) {
Thread theThread = new Thread( new MyThread() );
theThread.start();
}
}

3. Create an inner class (static or non-static) using
eiether technique 1 or 2. Example:

public class MyTestProgram {
private class MyThread implements Runnable {
public void run() {
// .. the thread code
}
}

public static void main( String[] args ) {
Thread theThread = new Runnable( this.new MyThread() );
theThread.start();
}
}

4. Create an anonymouse class of eiether java.lang.Runnable
or java.lang.Thread, override the run() method. Example:

public class TestProgram {
public static void main( String[] args ) {
Thread theThread = new Thread( new Runnable() {
public void run() {
// .. thread code here
}
} );
theThread.start();
}
}

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More Advanced Java Interview Questions

Is a class a subclass of itself?

0 Answers  


how to get value in combo for updation?

1 Answers   CMC,


Is “abc” a primitive value?

0 Answers  


what is default length of textfield ?

1 Answers  


How to determine applet?s height and width?

1 Answers  


How are the elements of a cardlayout organized?

0 Answers  


whats is mean by jndi

4 Answers   SolutionNET,


can i call multipule form beans in Action class?

1 Answers   Wipro,


Why use a datasource when you can directly specify a connection details?

0 Answers  


What happens when we invoke a thread?s interrupt method while it is in sleeping or waiting condition?

1 Answers  


Why does most servlets extend HttpServlet?

4 Answers   Accenture, Wipro,


Why DOM Parser would take more Memory than SAX parser while they are parsing?

3 Answers   OnMobile,


Categories