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 are the high-level thread states in java programming?
What is difference between static class and singleton pattern?
What do you mean by checked exceptions?
What do you understand by casting in java language?
Tell me the latest versions in java related areas?
What is re-factoring in software?
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
What do you mean by default constructor in java?
Explain the importance of finally block in java?
What is the use of optional ?
what is singleton class in java?
Mention the default values of all the elements of an array defined as an instance variable.
In the below example, what will be the output?
Why a dead thread occurs?
What is the purpose of stub and skeleton?