0% found this document useful (0 votes)
15 views8 pages

Multithreading in Java

Multithreading in Java allows for the simultaneous execution of multiple threads, enhancing application performance and responsiveness. Java provides a Thread class and a Runnable interface for creating and managing threads, each with distinct advantages and methods. The document outlines thread states, methods, and examples of implementing multithreading in Java applications.

Uploaded by

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

Multithreading in Java

Multithreading in Java allows for the simultaneous execution of multiple threads, enhancing application performance and responsiveness. Java provides a Thread class and a Runnable interface for creating and managing threads, each with distinct advantages and methods. The document outlines thread states, methods, and examples of implementing multithreading in Java applications.

Uploaded by

gvnbca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Multithreading in Java

Multithreading in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and


multithreading, both are used to achieve multitasking.

Advantages of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform multiple
operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.

4) Multithreading can significantly enhance the performance of applications by making better use of
multiple CPU cores. Each thread can run on a separate core, allowing for parallel processing and
faster execution of tasks.

5) In applications with a graphical user interface (GUI), multithreading can help keep the interface
responsive. Long-running tasks can be handled in the background by separate threads, preventing
the main thread from freezing and improving user experience.

6) Threads within the same process share the same memory and resources, which makes it easier to
share data between them without the need for complex inter-process communication mechanisms.

7) Using multiple threads can simplify the design of complex applications, allowing for more
modular and manageable code. For example, separate threads can be dedicated to different tasks,
such as network communication, file I/O, and computation.

8) Multithreading allows an application to continue processing while waiting for I/O operations to
complete. This leads to better utilization of CPU resources as threads can perform useful work
during I/O waits.

What is Thread in Java?

A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of


execution.

Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It
uses a shared memory area.
As shown in the above figure, a thread is executed inside the process. There is context-switching
between the threads. There can be multiple processes inside the OS, and one process can have
multiple threads.

Note: At a time one thread is executed only.

Java Thread class


Java provides Thread class to achieve thread programming. Thread class provides constructors and
methods to create and perform operations on a thread. Thread class extends Object class and
implements Runnable interface.

Thread States:

o New: A thread that has been created but not yet started.

o Runnable: A thread that is ready to run and waiting for CPU time.

o Blocked: A thread that is blocked waiting for a monitor lock.

o Waiting: A thread that is waiting indefinitely for another thread to perform a particular
action.

o Timed Waiting: A thread that is waiting for another thread to perform an action for up to a
specified waiting time.

o Terminated: A thread that has completed its task.

Java Thread Class Methods

S.N. Modifier Method Description


and Type

1) void start() It is used to start the execution of the thread.

2) void run() It is used to do an action for a thread.

3) static void sleep() It sleeps a thread for the specified amount of


time.
4) static currentThread() It returns a reference to the currently executing
Thread thread object.

5) void join() It waits for a thread to die.

6) int getPriority() It returns the priority of the thread.

7) void setPriority() It changes the priority of the thread.

8) String getName() It returns the name of the thread.

9) void setName() It changes the name of the thread.

10) long getId() It returns the id of the thread.

11) boolean isAlive() It tests if the thread is alive.

12) static void yield() It causes the currently executing thread object to
pause and allow other threads to execute
temporarily.

13) void suspend() It is used to suspend the thread.

14) void resume() It is used to resume the suspended thread.

15) void stop() It is used to stop the thread.

16) void destroy() It is used to destroy the thread group and all of
its subgroups.

17) boolean isDaemon() It tests if the thread is a daemon thread.

18) void setDaemon() It marks the thread as daemon or user thread.

19) void interrupt() It interrupts the thread.

20) boolean isinterrupted() It tests whether the thread has been interrupted.

21) static interrupted() It tests whether the current thread has been
boolean interrupted.

22) static int activeCount() It returns the number of active threads in the
current thread's thread group.

23) void checkAccess() It determines if the currently running thread has


permission to modify the thread.

24) static holdLock() It returns true if and only if the current thread
boolean holds the monitor lock on the specified object.

25) static void dumpStack() It is used to print a stack trace of the current
thread to the standard error stream.

26) StackTrace getStackTrace() It returns an array of stack trace elements


Element[] representing the stack dump of the thread.

27) static int enumerate() It is used to copy every active thread's thread
group and its subgroup into the specified array.

28) Thread.Sta getState() It is used to return the state of the thread.


te

29) ThreadGro getThreadGroup() It is used to return the thread group to which this
up thread belongs

30) String toString() It is used to return a string representation of this


thread, including the thread's name, priority,
and thread group.

31) void notify() It is used to give the notification for only one
thread which is waiting for a particular object.

32) void notifyAll() It is used to give the notification to all waiting


threads of a particular object.

33) void setContextClassLoader() It sets the context ClassLoader for the Thread.

34) ClassLoade getContextClassLoader() It returns the context ClassLoader for the thread.
r

35) static getDefaultUncaughtExce It returns the default handler invoked when a


Thread.Un ptionHandler() thread abruptly terminates due to an uncaught
caughtExc exception.
eptionHan
dler

36) static void setDefaultUncaughtExce It sets the default handler invoked when a thread
ptionHandler() abruptly terminates due to an uncaught
exception.

File Name: MultiThreadingExample.java

class MyThread extends Thread {


private String threadName;
MyThread(String name) {
threadName = name;
}
// Override the run method to define the task for the thread
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
// Sleep for a while to simulate some work
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " finished.");
}
}
public class MultiThreadingExample {
public static void main(String[] args) {
// Create instances of MyThread
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
MyThread thread3 = new MyThread("Thread 3");
// Start the threads
thread1.start();
thread2.start();
thread3.start();
// Wait for all threads to finish
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("All threads have finished.");
}
}
Output:

Thread 3 - Count: 1
Thread 1 - Count: 1
Thread 2 - Count: 1
Thread 3 - Count: 2
Thread 2 - Count: 2
Thread 1 - Count: 2
Thread 3 - Count: 3
Thread 2 - Count: 3
Thread 1 - Count: 3
Thread 3 - Count: 4
Thread 1 - Count: 4
Thread 2 - Count: 4
Thread 3 - Count: 5
Thread 2 - Count: 5
Thread 1 - Count: 5
Thread 1 finished.
Thread 3 finished.
Thread 2 finished.
All threads have finished.
Explanation

By extending the Thread class and overriding its run method to print a message five times with a one-
second delay in between, this Java programme illustrates multithreading.

Three instances of MyThread are generated and launched using the start function in the main class,
MultiThreadingExample, enabling them to operate concurrently. In order to guarantee that the main
thread waits for every thread to finish before issuing a final message stating that every thread has
finished, the join() method is employed.

Java Runnable Interface


Java runnable is an interface used to execute code on a concurrent thread. It is an interface which is
implemented by any class if we want that the instances of that class should be executed by a thread.

The runnable interface has an undefined method run() with void as return type, and it takes in no
arguments. The method summary of the run() method is given below-
Method Description

public void run() This method takes in no arguments. When the object of a
class implementing Runnable class is used to create a
thread, then the run method is invoked in the thread
which executes separately.

The runnable interface provides a standard set of rules for the instances of classes which wish to
execute code when they are active. The most common use case of the Runnable interface is when we
want only to override the run method. When a thread is started by the object of any class which is
implementing Runnable, then it invokes the run method in the separately executing thread.

A class that implements Runnable runs on a different thread without subclassing Thread as it
instantiates a Thread instance and passes itself in as the target. This becomes important as classes
should not be subclassed unless there is an intention of modifying or enhancing the fundamental
behavior of the class.

Runnable class is extensively used in network programming as each thread represents a separate
flow of control. Also in multi-threaded programming, Runnable class is used. This interface is present
in java.lang package.

Implementing Runnable

It is the easiest way to create a thread by implementing Runnable. One can create a thread on any
object by implementing Runnable. To implement a Runnable, one has only to implement the run
method.

public void run()

In this method, we have the code which we want to execute on a concurrent thread. In this method,
we can use variables, instantiate classes, and perform an action like the same way the main thread
does. The thread remains until the return of this method. The run method establishes an entry point
to a new thread.

How to create a thread using Runnable interface?

To create a thread using runnable, use the following code-

Runnable runnable = new MyRunnable();


Thread thread = new Thread(runnable);
thread.start();

The thread will execute the code which is mentioned in the run() method of the Runnable object
passed in its argument.

A simple thread example using runnable

public class ExampleClass implements Runnable {


@Override
public void run() {
System.out.println("Thread has ended");
}
public static void main(String[] args) {
ExampleClass ex = new ExampleClass();
Thread t1= new Thread(ex);
t1.start();
System.out.println("Hi");
}
}
Output:
Use of Runnable class in network programming

The runnable class is used to perform multi-thread programming, especially in server-side as a server
may be getting several requests from different clients. To tackle this in a fast and resource-efficient
way, we use multi-thread programming.

Example of a networking program using Runnable-

The following program shows a server program which creates a thread, then creates a socket and
waits for a client to connect to it and asks for an input string-

public class Example {


public static void main(String[] args) {
new Thread(new SimpleServer()).start();
}
static class SimpleServer implements Runnable {

@Override
public void run() {
ServerSocket serverSocket = null;
while (true) {
try {
serverSocket = new ServerSocket(3333);
Socket clientSocket = serverSocket.accept();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(clientSocket.
getInputStream()));
System.out.println("Client said :"+inputReader.readLine());
}
catch (IOException e) {
e.printStackTrace();
}finally{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
In this program, we are creating a socket for the client on a thread. Usually, different threads are
created in a server for different client requests. Though, it is not a good practice to make an
individual thread for each client if the compiler's ability to control per-thread memory is bad.

Thread vs. Runnable


There are several differences between Thread class and Runnable interface based on their
performance, memory usage, and composition.

o By extending thread, there is overhead of additional methods, i.e. they consume excess or
indirect memory, computation time, or other resources.

o Since in Java, we can only extend one class, and therefore if we extend Thread class, then we
will not be able to extend any other class. That is why we should implement Runnable
interface to create a thread.

o Runnable makes the code more flexible as, if we are extending a thread, then our code will
only be in a thread whereas, in case of runnable, one can pass it in various executor services,
or pass it to the single-threaded environment.

o Maintenance of the code is easy if we implement the Runnable interface.

You might also like