0% found this document useful (0 votes)
4 views1 page

Implementing Multithreading in Java

Threads are objects of type Thread that allow for multithreading in Java by extending the Thread class or implementing the Runnable interface. There are two main ways to create threads - by extending Thread where the run() method is overridden, or by implementing Runnable which only requires implementing the run() method and passing the Runnable object to a Thread. Implementing Runnable is generally preferred unless other Thread methods need to be extended as well.

Uploaded by

Ranjan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Implementing Multithreading in Java

Threads are objects of type Thread that allow for multithreading in Java by extending the Thread class or implementing the Runnable interface. There are two main ways to create threads - by extending Thread where the run() method is overridden, or by implementing Runnable which only requires implementing the run() method and passing the Runnable object to a Thread. Implementing Runnable is generally preferred unless other Thread methods need to be extended as well.

Uploaded by

Ranjan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Multi Threading Threads: A thread is an object of type Thread, defined in [Link] package.

Multithreading functionality in Java is supported in four places:


Thread class Runnable interface Object class Java virtual machine Extend the Thread class itself. Implement the Runnable interface.

There are two ways to create threads:


When we have to extend a thread? The Thread class implements Runnable interface, which defines run() method. To create a thread, Thread class can be extended and the run() method must be overridden. In this method, the code to be executed by the thread is defined. Thread class defines many other methods too, which may or may not be overridden. In general, Thread should be extended to create a new thread only if methods other than run() are to be extended. Otherwise, it is probably better to implement Runnable interface. When we have to implement an interface? It is the simplest way to create a new thread. This interface defines only run() method. To implement Runnable, a class need only implement run(), which is declared as public and void in Runnable. After the class that implements Runnable is created, it can be passed in the constructor of Thread to instantiate an object of Thread.

You might also like