JAVA Presentation
JAVA Presentation
+ " is running")
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
// Main Class
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
Output
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
Thread creation by implementing the Runnable Interface
We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a
Thread object and call start() method on this object.
try {
System.out.println(
+ " is running");
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
// Main Class
class Multithread {
{
int n = 8; // Number of threads
Thread object
object.start();
}
}
Output
Thread 13 is running
Thread 11 is running
Thread 12 is running
Thread 15 is running
Thread 14 is running
Thread 18 is running
Thread 17 is running
Thread 16 is running
Thread Class vs Runnable Interface
If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance.
But, if we implement the Runnable interface, our class can still extend other base classes.
We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like
yield(), interrupt() etc. that are not available in Runnable interface.
Using runnable will give you an object that can be shared amongst multiple
Multi-threading in Java allows multiple threads to run concurrently within a single process. This allows for better
utilization of resources and can improve the performance of an application. In this practical approach to multi-threading in
Java, we will explore how to create and manage threads in a Java program.
To create a thread in Java, you can either extend the Thread class or implement the Runnable interface. Here is an
example of how to create a thread by extending the Thread class:
```java
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
```
In the example above, we define a class `MyThread` that extends the `Thread` class and overrides the `run` method. Inside
the `run` method, we print out a message indicating that the thread is running. In the `main` method, we create an
instance of `MyThread` and start it using the `start` method.
Alternatively, you can create a thread by implementing the `Runnable` interface. Here is an example of how to create a
thread using the `Runnable` interface:
```java
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running");
}
}
In this example, we define a class `MyRunnable` that implements the `Runnable` interface and overrides the `run` method.
We then create an instance of `MyRunnable` and pass it to the `Thread` constructor. Finally, we start the thread using the
`start` method.
In addition to creating and managing threads, Java also provides a number of APIs for working with threads, such as the
`Thread` class, the `Executor` framework, and the `java.util.concurrent` package. These APIs offer more advanced features
for handling threads, such as thread pooling, synchronization, and coordination between threads.