Question
What are the two ways of creating thread

Answers

There are two ways to create a new thread. Extend the Thread class and override the run() method in your class. Create an instance of the subclass and invoke the start() method on it, which will create a new thread of execution.

public class NewThread extends Thread
{
public void run()
{
// the code that has to be executed in a separate new thread goes here
}
public static void main(String [] args)
{
NewThread c = new NewThread();
c.start();
}
}



Implements the Runnable interface. The class will have to implement the run() method in the Runnable interface. Create an instance of this class. Pass the reference of this instance to the Thread constructor a new thread of execution will be created.

public class NewThread implements Runnable
{
public void run()
{
// the code that has to be executed in a separate new thread goes here
}
public static void main(String [] args)
{
NewThread c = new NewThread();
Thread t = new Thread(c);
t.start();
}
}   Your Comment




More Software,Java Questions..
What is the inputsplit in map reduce software?

Can variables be used in Java without initialization?

What is the difference between java bean and enterprise java bean?

What is software configuration management?

How does the java database connectivity (jdbc) work?

What are the advantages of threads in java?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

What is object cloning in Java?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?



Search
Can you Answer!!
  • Q Odapter Development Environments
  • Q What are the different fields in a Central contact
  • Q Which command is used to show the version?
  • Q How can i bring my copy of the jmri code up to date?
  • Q Pellagra & Scurvy diseases caused due to deficiency of
  • Q What do you like the most about the inside sales job?
  • Q What is a class? What is an ID?
  • Q Differentiate between Dice and Slice.
  • Q Give an account of the morphology of Brachiopoda.
  • Q what is the Primary Action of Adductor Brevis
  • Q Explain in detail about the following Soxhlet apparatus