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
Is a copy constructor?
What is thread synchronization in java?
Can we use String with switch case?
What are the advantages of inner classes?
What is time complexity algorithm?
Can we sort hashset in java?
What is the difference between throw and throws keywords?
How is abstraction implemented in java ?
What is the equal sign?
When wait(), notify(), notifyall() methods are called does it releases the lock or holds the acquired lock?
Is int a class in java?
What does it mean to flush a file?
What do you understand by abstract classes?
What is lossy conversion in java?
What is a functional interface?