0% found this document useful (0 votes)
40 views24 pages

JAVA Presentation

Uploaded by

fmfaustina
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)
40 views24 pages

JAVA Presentation

Uploaded by

fmfaustina
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/ 24

Slide 1:

Title: Practical Approach to Multi-Threading in


Java
- Introduction to Multi-Threading
- Benefits and Use Cases
- Challenges and Solutions
Slide 2:
Understanding Multi-Threading:
- Definition: Execution of multiple threads
concurrently within a single process.
- Key Concepts: Threads, Processes,
Synchronization, Deadlocks
Slide 3:
Benefits of Multi-Threading:
- Improved Performance: Utilizing CPU
resources efficiently
- Responsiveness: Responsive user interfaces
- Simplicity: Simplified code organization
Overall, multi-threading in Java allows for better performance and responsiveness in
applications by enabling concurrent execution of tasks. By understanding how to create and
manage threads in Java, you can take advantage of the benefits of multi-threading in your own
programs.
Slide 4:
Use Cases of Multi-Threading:
- Concurrent Programming: Web servers,
Database servers
- Parallel Processing: Image processing,
Scientific simulations
- Asynchronous Programming: File I/O,
Networking
Slide 5:
Challenges in Multi-Threading:
- Synchronization: Ensuring data consistency
- Deadlocks: Resource contention leading to
system halts
- Thread Safety: Avoiding race conditions
Slide 6:
Solutions to Multi-Threading Challenges:
- Synchronization Techniques: Locks,
Semaphores, Monitors
- Deadlock Prevention: Ordering resources,
Timeout mechanisms
- Thread Safety: Immutable objects, Atomic
operations
Slide 7:
Introduction to Java Application Programming
Interfaces (APIs):
- What are APIs?
- Importance of APIs in Java Development
Slide 8:
Understanding Java APIs:
- Definition: Set of classes, interfaces, methods
provided by Java for developers
- Examples: Java Collections Framework, JDBC,
JavaFX
Slide 9:
Benefits of Java APIs:
- Reusability: Pre-built functionality for
common tasks
- Interoperability: Compatibility across different
platforms and systems
- Productivity: Accelerated development with
ready-to-use components
Slide 10:
Commonly Used Java APIs:
- JDBC (Java Database Connectivity): Database
interaction
- Servlets and JSP (JavaServer Pages): Web
development
- Swing and JavaFX: GUI development
Slide 11:
Conclusion:
- Multi-Threading in Java enables efficient
utilization of system resources.
- Java APIs provide a rich set of functionalities,
accelerating development and enhancing
interoperability
EXAMPLES
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum
utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.

Threads can be created by using two mechanisms :

Extending the Thread class


Implementing the Runnable Interface
Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread
class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the
execution of a thread. Start() invokes the run() method on the Thread object.
// Java code for thread creation by extending
// the Thread class

class MultithreadingDemo extends Thread {

public void run()


{
try {
// Displaying the thread that is running
System.out.println(

"Thread " + Thread.currentThread().getId()

+ " is running")
}
catch (Exception e) {

// Throwing an exception

System.out.println("Exception is caught");

}
}

// Main Class

public class Multithread {

public static void main(String[] args)


{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {

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.

// Java code for thread creation by implementing


// the Runnable Interface

class MultithreadingDemo implements Runnable {

public void run()


{

try {

// Displaying the thread that is running

System.out.println(

"Thread " + Thread.currentThread().getId()

+ " is running");

catch (Exception e) {
// Throwing an exception

System.out.println("Exception is caught");

}
}

// Main Class

class Multithread {

public static void main(String[] args)

{
int n = 8; // Number of threads

for (int i = 0; i < n; i++) {

Thread object

= new Thread(new MultithreadingDemo());

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");
}
}

public class Main {


public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
```

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.

You might also like