Course Content
Advanced Java 2077
Advanced Java 2077
Threads and Runnable
Threads and Runnable are both used in concurrent programming in Java. Threads are lightweight processes that can run independently of each other within a single program. The Runnable
interface is used to define the code to be executed in a separate thread. In this chaper, we will discuss the use cases of threads and Runnable, including complete code examples.
Use Cases of Threads and Runnable
Multithreaded Servers
Multithreaded servers are servers that can handle multiple client connections simultaneously. To achieve this, each client connection is handled in a separate thread.
Here's an example.
Main
import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; class Server { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8080); while (true) { Socket client = server.accept(); new Thread(new ClientHandler(client)).start(); } } } class ClientHandler implements Runnable { private Socket client; public ClientHandler(Socket client) { this.client = client; } public void run() { // Code to handle client connection } }
Parallel Processing
Parallel processing is a technique used to split a large task into smaller parts that can be executed concurrently. This can significantly reduce the overall time required to complete the task. Here's an example of parallel processing using threads.
Main
import java.util.Random; class MyThread extends Thread { private int[] array; private int start; private int end; private int sum = 0; public MyThread(int[] array, int start, int end) { this.array = array; this.start = start; this.end = end; } public void run() { for (int i = start; i < end; i++) { sum += array[i]; } } public int getSum() { return sum; } } class ParallelSum { public static void main(String[] args) throws InterruptedException { int[] array = new int[1000000]; Random random = new Random(); for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(10); } MyThread[] threads = new MyThread[10]; for (int i = 0; i < threads.length; i++) { threads[i] = new MyThread(array, i * 100000, (i + 1) * 100000); threads[i].start(); } int sum = 0; for (int i = 0; i < threads.length; i++) { threads[i].join(); sum += threads[i].getSum(); } System.out.println("Sum: " + sum); }
In this example, we create a MyThread
class that sums a portion of an integer array. We create 10 instances of MyThread
, each summing a different portion of the array. We then start each thread and wait for them to complete using the join()
method. Finally, we sum the results of each thread to obtain the total sum.
Key Differences
The main difference between threads and the Runnable interface is that threads extend the Thread
class, while the Runnable
interface is implemented by classes that do not extend the Thread
class. This means that implementing the Runnable
interface allows for more flexible code, as a class can extend another class while still being able to use threads.
Another difference is that the run()
method in the Thread
class cannot be overridden, whereas the run()
method in the Runnable
interface must be implemented. This means that implementing the Runnable
interface allows for more control over the code to be executed in a separate thread.
Thanks for your feedback!