Answer Posted / shaik baji
We can the Thread in two ways as follows
1) By implements the "Runnable" Interface
For Example:
class ThreadDemo implements Runnable
{
public static void main(String[] args)
{
Thread t = new Thread(new ThreadDemo());
t.start();
System.out.println("End of main thread");
}
public void run()
{
for(int i=1; i<=10; i++)
System.out.println(i);
System.out.println("End of child thread");
}
}
2) By extents the "Thread" class
NOTE: Thread is not a abstract class.
For Example:
------------
class ThreadDemo extends Thread
{
public static void main(String[] args)
{
Thread t = new ThreadDemo();
t.start();
System.out.println("End of main thread");
}
public void run()
{
for(int i=1; i<=10; i++)
System.out.println(i);
System.out.println("End of child thread");
}
}
| Is This Answer Correct ? | 7 Yes | 2 No |
Post New Answer View All Answers
What is this () in java?
what is the difference between the methods sleep() and wait()? : Java thread
What class of exceptions are generated by the java run-time system?
What is a private class in java?
What is stringjoiner ?
What is the difference between an inner class and a sub-class?
List primitive java types?
What is package protected in java?
Write a program to print 15 random numbers using foreach of java 8?
What is the use of StringTokenizer class?
What is output buffer?
Difference between static binding and dynamic binding?
what r advatages of websphere? & how to deploy?
How to disable caching on back button of the browser?
What is polymorphism java example?