I want to know how multiple threads can be created by "extending the Thread class". I know how it is done using "Runnable". Please tell me how it can be done by "extending Thread class".
Asked
Active
Viewed 1.7k times
-2
-
Why dont you show us what you have tried and then we can help you out. – David says Reinstate Monica Oct 02 '13 at 05:45
-
I'm a beginner to java. I don't know how its done. I'm having problems getting started – Aadithya Oct 02 '13 at 05:46
-
1Here you go : http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html – Juned Ahsan Oct 02 '13 at 05:49
-
example on stackoverflow: http://stackoverflow.com/a/2531984/1554314 – Taky Oct 02 '13 at 05:51
-
You don't want to do it by extending Thread. Runnable is better. http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread?rq=1 – Thilo Oct 02 '13 at 05:52
-
If you do some homework and then ask. It will be more useful for you and easier for people to explain. – Lokesh Oct 02 '13 at 05:53
-
@user2798783 posted the reference + code for initial help! – Digital Alchemist Oct 02 '13 at 06:01
1 Answers
8
Main Method
public class ThreadDemo
{
public static void main(String args[])
{
//Creating an object of the first thread
FirstThread firstThread = new FirstThread();
//Creating an object of the Second thread
SecondThread secondThread = new SecondThread();
//Starting the first thread
firstThread.start();
//Starting the second thread
secondThread.start();
}
}
This class is made as a thread by extending "Thread" class.
public class FirstThread extends Thread
{
//This method will be executed when this thread is executed
public void run()
{
//Looping from 1 to 10 to display numbers from 1 to 10
for (int i=1; i<=10; i++)
{
//Displaying the numbers from this thread
System.out.println( "Messag from First Thread : " +i);
/*taking a delay of one second before displaying next number
*
* "Thread.sleep(1000);" - when this statement is executed,
* this thread will sleep for 1000 milliseconds (1 second)
* before executing the next statement.
*
* Since we are making this thread to sleep for one second,
* we need to handle "InterruptedException". Our thread
* may throw this exception if it is interrupted while it
* is sleeping.
*
*/
try
{
Thread.sleep(1000);
}
catch (InterruptedException interruptedException)
{
/*Interrupted exception will be thrown when a sleeping or waiting
* thread is interrupted.
*/
System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);
}
}
}
This class is made as a thread by extending "Thread" class.
public class SecondThread extends Thread
{
//This method will be executed when this thread is executed
public void run()
{
//Looping from 1 to 10 to display numbers from 1 to 10
for (int i=1; i<=10; i++)
{
System.out.println( "Messag from Second Thread : " +i);
/*taking a delay of one second before displaying next number
* "Thread.sleep(1000);" - when this statement is executed,
* this thread will sleep for 1000 milliseconds (1 second)
* before executing the next statement.
*
* Since we are making this thread to sleep for one second,
* we need to handle "InterruptedException". Our thread
* may throw this exception if it is interrupted while it
* is sleeping.
*/
try
{
Thread.sleep (1000);
}
catch (InterruptedException interruptedException)
{
/*Interrupted exception will be thrown when a sleeping or waiting
*thread is interrupted.
*/
System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);
}
}
}
}
Please check Reference # Threading using Extend for complete detail, For quick reading i m posting code from above mentioned reference

Digital Alchemist
- 2,324
- 1
- 15
- 17