0% found this document useful (0 votes)
13 views7 pages

Moht

4

Uploaded by

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

Moht

4

Uploaded by

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

1. Explain Java Multithreading ?

Java Multithreading: Java


Multithreading refers to the ability of the
Java platform to execute multiple
threads simultaneously, allowing
programs to perform multiple tasks
concurrently. It enables the utilization of
multiple processors efficiently and
enhances the performance of
applications.
●Multithreading in Java is an act
of executing a complex process
using virtual processing entities
independent of each other. These
entities are called threads. Threads in
Java are virtual and share the same
memory location of the process. As the
threads are virtual, they exhibit a safer
way of executing a process

2.How to create and use


threads?
There are two ways to create a thread:
First, you can create a thread using
the thread class (extend syntax). This
provides you with constructors and
methods for creating and operating on
threads. The thread class extends the
object class and implements a runnable
interface
Creating and Using Threads: In Java,
you can create a thread by extending
the `Thread` class or
implementing the `Runnable` interface.
Extending the `Thread` class involves
overriding the `run()` method to define
the task for the thread, while
implementing the `Runnable` interface
requires implementing the `run()`
method. After creating a thread object,
you can start it by calling the `start()`
method.

3. Thread Scheduling?
Thread scheduling in Java refers to the
process by which the Java Virtual
Machine (JVM) manages the execution
of multiple threads in a Java program.
The JVM uses various scheduling
algorithms to determine the order in
which threads get access to the CPU
for execution. The Thread class in Java
provides methods to set thread priority,
which can influence the scheduling
behavior, although the actual thread
scheduling is platform-dependent and
may vary across different operating
systems and JVM implementations.
A component of Java that decides
which thread to run or execute and
which thread to wait is called a thread
scheduler in Java. In Java, a thread is
only chosen by a thread scheduler if it
is in the runnable state.
4. Synchronization in Java?
Synchronization in Java is the
process that allows only one
thread at a particular time to
complete a given task entirely.
By default, the JVM gives control
to all the threads present in the
system to access the shared
resource, due to which the system
approaches race condition
Synchronization in Java is used to
control access to shared resources
among multiple threads. The
`synchronized` keyword helps in
creating mutually exclusive blocks or
methods, ensuring that only one thread
executes them at a time, preventing
data inconsistency and race conditions.

5. Thread Class and Runnable


Interface?
The Runnable interface describes a
class whose instances can be run as
a thread. The interface itself is very
simple, describing only one method
( run ) that is called automatically by
Java when the thread is started. The
Runnable interface is usually used in
conjunction with the Thread class.

Thread Class and Runnable Interface


The `Thread` class in Java represents a
thread of execution. To create a thread,
you can extend this class and
override the `run()` method. The
`Runnable` interface, on the other
hand, provides a way to create a thread
by implementing its `run()` method. It's
often preferred because it allows
multiple interfaces to be implemented,
promoting better code organization and
reusability.

You might also like