Java Threads - Creating Threads and Multithreading in Java - by Swatee Chand - Edureka - Medium
Java Threads - Creating Threads and Multithreading in Java - by Swatee Chand - Edureka - Medium
Listen Share
Unlike many other computer languages, Java provides built-in support for multithreading.
Multithreading in Java contains two or more parts that can run concurrently. A Java thread
is actually a lightweight process.
This blog will introduce you to all the Java Thread concepts which many people find tricky
to use and understand. So let us get started then, shall we?
3. Multithreading in Java
Before we proceed with the first topic of this Java Thread blog, consider this example:-
Imagine a stockbroker application with lots of complex capabilities. These are a few of its
functions:
Now, if a historical analysis takes half an hour, and the user selects to perform a download
and check afterward, the warning may come too late to, buy or sell stock as a result. We just
imagined the sort of application that cries out for multithreading. Ideally, the download
should happen in the background (that is, in another thread). That way, other processes
could happen at the same time so that, for example, a warning could be communicated
instantly. All the while, the user is interacting with other parts of the application. The
analysis, too, could happen in a separate thread, so the user can work with the rest of the
application while the results are being calculated.
This is where Java thread helps. Let us understand Java Thread first:
Next concept in this blog is integral to the concept Threads and Multithreading.
So, this was all about the Java Thread states. Now, let us jump to the most important topic of
Java threads i.e. thread class and runnable interface. We will discuss these, one by one
below.
The Thread class defines several methods that help manage threads. The table below
displays the same:
Now let us see how to use a Thread which begins with the main java thread, that all Java
programs have.
So, this was the main thread. Let’s see how we can create a java thread?
By implementing the Runnable interface.
By extending the Thread
Let’s see how both the ways help in implementing Java thread.
Runnable Interface
Inside run( ), we will define the code that constitutes the new thread
Example:
To execute the run() method by a thread, pass an instance of MyClass to a Thread in its
constructor(A constructor in Java is a block of code similar to a method that’s called when an
instance of an object is created). Here is how that is done:
When the thread is started it will call the run() method of the MyClass instance instead of
executing its own run() method. The above example would print out the text “MyClass
running“.
Extending Java Thread
The second way to create a thread is to create a new class that extends Thread, then
override the run() method and then to create an instance of that class. The run() method is
what is executed by the thread after you call start(). Here is an example of creating a Java
Thread subclass:
public class MyClass extends Thread {
public void run(){
System.out.println("MyClass running");
}
}
To create and start the above thread you can do like this:
When the run() method executes it will print out the text “MyClass running“.
So far, we have been using only two threads: the main thread and one child thread. However,
our program can affect as many threads as it needs. Let’s see how we can create multiple
threads.
class MultiThread {
public static void main(String args[]) {
new MyThread("One");
new MyThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
This is how multi-threading in java works. This brings us to the end of the Java Thread
article. I hope this was informative and helpful to you.
If you wish to check out more articles on the market’s most trending technologies like
Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of
Java.