0% found this document useful (0 votes)
2 views2 pages

Java Thread Creation Methods Explained

Uploaded by

ou638009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Thread Creation Methods Explained

Uploaded by

ou638009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Creating single thread with example program:

Creating thread:
Java defines tow ways in which this can be accomplished:

1. You can implements the Runnable interface


2. You can extends the Thread class

Creating thread by implements Runnable:


 Declare the class as implementing the “runnable” interface.
 Implement the run()
 Create a thread by defining an object that is instanciated from this runnable class as the
target of the thread
 Call the thread’s start() to run the thread

Program:

// creating thread using runnable interface


class A implements Runnable
{
public void run()
{
[Link]("thread is created..");
}
public static void main(String arg[])
{
A obj =new A();
Thread t1 = new Thread(obj);
[Link]();
}
}

Creating thread by extends Thread:


The second way to create a thread is to create a new class that extends Thread, and then to create an
instance of the class.

The extending class must override the run() method, which is the entry point for the new thread. It

must also call start() to begin execution of the new thread.


Program:
// creating thread using Thread class
class A extends Thread
{
public void run()
{
[Link]("thread is created..");
}
public static void main(String arg[])
{
A obj =new A();

[Link]();
}
}

You might also like