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

Multithreading in Java

The document explains wrapper classes in Java, which convert primitive data types into objects, facilitating object-oriented features and utility methods. It also covers multithreading, detailing the creation of threads, their lifecycle, synchronization, and the differences between multithreading and multiprocessing. Key concepts such as autoboxing, unboxing, and important thread methods are highlighted.
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 views5 pages

Multithreading in Java

The document explains wrapper classes in Java, which convert primitive data types into objects, facilitating object-oriented features and utility methods. It also covers multithreading, detailing the creation of threads, their lifecycle, synchronization, and the differences between multithreading and multiprocessing. Key concepts such as autoboxing, unboxing, and important thread methods are highlighted.
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

SHRI SHANKARACHARYA TECHNICAL CAMPUS

Wrapper Classes in Java


1. What is a Wrapper Class?
A wrapper class in Java is used to convert primitive data types into objects. Java
provides a wrapper class for each primitive type.
2. Why Use Wrapper Classes?
• Object-Oriented Features: Collections (like ArrayList) store only objects,
not primitives.
• Utility Methods: Wrapper classes provide useful methods.
• Autoboxing and Unboxing: Java automatically converts primitives to
objects and vice versa.
3. Wrapper Classes for Each Primitive Type

Primitive
Wrapper Class
Type

byte Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean

4. Example of Wrapper Class Usage


public class WrapperExample {
public static void main(String[] args) {
int num = 10;
Integer obj = [Link](num); // Manual Boxing

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 1


SHRI SHANKARACHARYA TECHNICAL CAMPUS

int newNum = [Link](); // Manual Unboxing


// Autoboxing (Automatic conversion)
Integer autoBoxed = num;
// Unboxing (Automatic conversion back to primitive)
int autoUnboxed = autoBoxed;
[Link]("Integer Object: " + obj);
[Link]("Primitive int: " + newNum);
}
}
5. Key Concepts
• Autoboxing: Java automatically converts a primitive into a wrapper object.
Integer obj = 5; // int to Integer (autoboxing)
• Unboxing: Java automatically converts a wrapper object back to a primitive.
int num = obj; // Integer to int (unboxing)

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 2


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Multithreading in Java

Multithreading is a fundamental concept in Java that allows a program to run multiple tasks
(threads) concurrently. This improves performance, especially on multi-core processors.

1. What is a Thread?

A thread is the smallest unit of execution in a program. Java supports multithreading, which
means multiple threads can run in parallel.

2. Why Use Multithreading?

• Better CPU Utilization – Multiple threads can use CPU efficiently.


• Faster Execution – Tasks can execute simultaneously.
• Improved Responsiveness – Useful in applications like GUI, where UI should
remain responsive.
• Efficient Resource Sharing – Threads of the same process share memory and
resources.

3. How to Create a Thread in Java?

Java provides two main ways to create a thread:

Method 1: Extending the Thread Class

class MyThread extends Thread {


public void run() {
[Link]("Thread is running...");
}

public static void main(String[] args) {


MyThread t1 = new MyThread();
[Link](); // Starts the thread
}
}

• The run() method contains the code that will execute in a separate thread.
• The start() method is used to begin execution.

Method 2: Implementing the Runnable Interface

class MyRunnable implements Runnable {


public void run() {
[Link]("Thread is running...");
}

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 3


SHRI SHANKARACHARYA TECHNICAL CAMPUS

public static void main(String[] args) {


Thread t1 = new Thread(new MyRunnable());
[Link]();
}
}

• The Runnable interface is a better approach because Java allows extending only one
class, but multiple interfaces can be implemented.

4. Thread Lifecycle

A thread in Java goes through several states:

1. New – Created using new Thread(), but not started.


2. Runnable – Started with start(), waiting for CPU time.
3. Running – Actively executing.
4. Blocked/Waiting – Waiting for resources or another thread.
5. Terminated – Execution completed.

5. Important Thread Methods

• start() – Starts the thread.


• run() – Defines the code executed by the thread.
• sleep(ms) – Pauses the thread for a specified time.
• join() – Waits for a thread to finish.
• isAlive() – Checks if a thread is running.

6. Synchronization in Multithreading

When multiple threads share resources, there is a risk of inconsistency. Synchronization


prevents multiple threads from accessing a shared resource simultaneously.

class Counter {
private int count = 0;

public synchronized void increment() {


count++;
}

public int getCount() {


return count;
}
}

class MyThread extends Thread {

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 4


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Counter c;

MyThread(Counter c) {
this.c = c;
}

public void run() {


for (int i = 0; i < 1000; i++) {
[Link]();
}
}
}

public class Test {


public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
MyThread t1 = new MyThread(counter);
MyThread t2 = new MyThread(counter);

[Link]();
[Link]();

[Link]();
[Link]();

[Link]("Final Count: " + [Link]());


}
}

• synchronized ensures only one thread accesses increment() at a time.

7. Multithreading vs. Multiprocessing

Feature Multithreading Multiprocessing


Number of Uses multiple threads on a single Uses multiple processes across multiple
Cores core cores
Memory Usage Low (shares memory) High (each process has its own memory)
Harder (inter-process communication
Communication Easier (shared memory)
required)

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 5

You might also like