0% found this document useful (0 votes)
5 views4 pages

ch 4

The document explains key concepts of multithreading in Java, including the definition of a thread, thread synchronization, and the thread life cycle. It also discusses the MapReduce programming model used for processing large data sets in a distributed manner, detailing its main components: Map, Shuffle, Sort, and Reduce functions. Additionally, it highlights the benefits of MapReduce such as scalability, fault tolerance, and parallel processing.

Uploaded by

lelinjiten2468
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)
5 views4 pages

ch 4

The document explains key concepts of multithreading in Java, including the definition of a thread, thread synchronization, and the thread life cycle. It also discusses the MapReduce programming model used for processing large data sets in a distributed manner, detailing its main components: Map, Shuffle, Sort, and Reduce functions. Additionally, it highlights the benefits of MapReduce such as scalability, fault tolerance, and parallel processing.

Uploaded by

lelinjiten2468
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/ 4

Thread, Multithreading, Thread Synchronization, and Example: A web server handling multiple client requests

Thread Life Cycle: concurrently using threads.

1. What is a Thread in Java (5 Marks)

A thread in Java is the smallest unit of execution within a


program. Each thread runs independently, but shares the
3. What is Thread Synchronization (5 Marks)
same memory space and system resources with other
threads in the same process. Java provides built-in support When multiple threads access a shared resource (e.g., a
for multithreading through the Thread class and Runnable variable or file), race conditions or inconsistent data can
interface. occur. To prevent this, thread synchronization is used to
allow only one thread at a time to access the critical
 A thread can be created by:
section.
o Extending the Thread class
✅ Techniques in Java:
o Implementing the Runnable interface
 Using synchronized keyword on:
 Threads help divide large tasks into smaller,
parallel units.
o Methods (synchronized void
methodName())
 Useful in scenarios like performing background
o Code blocks (synchronized(obj) { ... })
tasks, animations, or responding to user input
without freezing the application.  Java also provides ReentrantLock for advanced
locking.
Example use: Downloading a file while updating a progress
bar at the same time. ✅ Purpose:

 Prevent data inconsistency


2. What is Multithreading (5 Marks)
 Maintain thread safety
Multithreading is the capability of a Java program to
execute multiple threads simultaneously. It allows a  Avoid race conditions
program to perform many tasks at the same time, improving Example: Two threads trying to withdraw money from the
performance and responsiveness. same bank account object.
✅ Key Features:

 Threads run independently but share the same 4. Thread Life Cycle in Java (5 Marks)
memory space.
A thread in Java goes through several stages from creation
 Reduces CPU idle time and increases resource to termination. This is called the thread life cycle.
utilization.
✅ Stages:
 Makes applications more responsive (especially
1. New – Thread is created using new Thread(), but
GUIs and servers).
not started.
✅ Advantages:
2. Runnable – After calling start(), the thread is
 Efficient use of CPU ready to run and waits for CPU time.

3. Running – Thread is selected by the scheduler and


 Simultaneous background processing
is currently executing.
 Faster execution for concurrent tasks
4. Blocked/Waiting – Thread is temporarily inactive, System.out.println("Running...");
waiting to acquire a lock or wait for another
}
thread.
}
5. Terminated (Dead) – Thread finishes execution or
is forcefully stopped.

The thread scheduler decides which thread to run and public void stopThread() {
when, based on priority and availability.
running = false;
🔁 Diagram (text):
}
New → Runnable → Running → (Blocked/Waiting) →
Terminated }

How to Create a Thread in Java: public class Main {

class MyThread extends Thread { public static void main(String[] args) {

public void run() { MyThread t = new MyThread();

System.out.println("Thread is running"); t.start();

} try { Thread.sleep(1000); } catch (InterruptedException


e) {}
}
t.stopThread(); // graceful stop

}
public class Main {
}
public static void main(String[] args) {

MyThread t1 = new MyThread();


Multithreading Example:
t1.start(); // starts the thread
class Task1 extends Thread {
}
public void run() {
}
for (int i = 0; i < 5; i++) {

System.out.println("Task 1");
Destroying a Thread:
}
Java doesn't provide a direct method to forcibly destroy a
thread (like destroy()) as it can lead to inconsistent state. }
Instead, use flags to stop a thread safely:
}
class MyThread extends Thread {

volatile boolean running = true;


class Task2 extends Thread {

public void run() {


public void run() {
for (int i = 0; i < 5; i++) {
while (running) {
System.out.println("Task 2");
} 🔹 2. Shuffling

}  Once map output is ready, the system groups all


identical keys together across mappers.
}
 This is done automatically.

public class Main {  This phase ensures same keys go to the same
reducer.
public static void main(String[] args) {
Example: All ("Hello", 1) from different mappers are
Task1 t1 = new Task1(); grouped together.

Task2 t2 = new Task2(); 🧠 Think of shuffling as gathering all similar items together.
t1.start();
🔹 3. Sorting
t2.start();
 Within each group of identical keys, values are
} sorted.

}  Sorting makes it easier for reducers to aggregate


the values.

 Usually happens along with shuffling.


MAPREDUCE:
Example: All values of “Hello” will be in a sorted list:
MapReduce is a programming model used for processing
large volumes of data in a distributed and parallel fashion  ("Hello", [1, 1, 1])
across a cluster of machines.
Think of sorting as organizing your collected items in order
It breaks a big task into smaller sub-tasks, processes them before processing.
independently, and finally combines the results.
🔹 4. Reduce Function
Used widely in Big Data frameworks like Hadoop.
 Reducer takes each key and a list of values, and
Main Components of MapReduce:
performs aggregation or summary.
🔹 1. Map Function
 Output is the final processed result.
 Input: Raw data split into chunks (input split). Example:
Input: ("Hello", [1, 1, 1])
 The Mapper function takes each chunk and
Output: ("Hello", 3)
converts it into key-value (KV) pairs.
🧠 Think of Reduce as combining everything related to a key
 Works independently on each piece of input.
into a final result.
📌 Example:
Full Example – Word Count
Input: "Hello world Hello"
Map Output: Let’s say we want to count how many times each word
appears in a file:
 ("Hello", 1)
Input file: "cat dog cat"
 ("world", 1)
Map Phase:
 ("Hello", 1)
 ("cat", 1)
Think of Map as labeling or tagging each data point.
 ("dog", 1)

 ("cat", 1)

Shuffling + Sorting:

 ("cat", [1, 1])

 ("dog", [1])

Reduce Phase:

 ("cat", 2)

 ("dog", 1)

✅ Final Output:

cat: 2

dog: 1

Benefits of MapReduce:

1. Scalability

o Can handle petabytes of data by


distributing across many machines.

2. Fault Tolerance

o Automatically retries failed tasks and


reruns them on other nodes.

3. Parallel Processing

o Performs computation simultaneously


on different chunks of data, improving
speed.

4. Simplicity

o Abstracts complex parallel computation


using just two functions: map and
reduce.

5. Data Locality Optimization

o Processes data where it is stored,


reducing network traffic and increasing
performance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy