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

Java (Micro) 5

The document explains the process of creating multiple threads in Java using the Runnable interface and discusses synchronization techniques to avoid race conditions and deadlocks. It differentiates between multitasking and multiprocessing, outlines various layout managers in Java, and compares the AWT and Swing frameworks. Additionally, it describes different types of I/O streams in Java and their use cases.

Uploaded by

zanwarritesh7299
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

Java (Micro) 5

The document explains the process of creating multiple threads in Java using the Runnable interface and discusses synchronization techniques to avoid race conditions and deadlocks. It differentiates between multitasking and multiprocessing, outlines various layout managers in Java, and compares the AWT and Swing frameworks. Additionally, it describes different types of I/O streams in Java and their use cases.

Uploaded by

zanwarritesh7299
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

Q1.

Explain the process of creating multiple threads using the  Avoiding Race Conditions: Prevents situations where the
Runnable interface in Java. outcome depends on the sequence or timing of
class MyRunnable implements Runnable { uncontrollable events.
@Override  Deadlock Prevention: Helps avoid scenarios where two or
public void run() { more threads wait indefinitely for each other to release
resources.
System.out.println(Thread.currentThread().getName() + "
Ways to Achieve Synchronization
is running.");
1. Locks (Mutual Exclusion)
}
} Description: Locks provide a way to ensure that only one
thread can access a resource at a time.
public class ThreadExample {
Types:
public static void main(String[] args) {
Reentrant Locks: Allow the same thread to acquire the lock
MyRunnable runnable1 = new MyRunnable(); multiple times without causing a deadlock.
MyRunnable runnable2 = new MyRunnable(); Read/Write Locks: Allow multiple readers or a single writer to
Thread thread1 = new Thread(runnable1); access the resource, optimizing performance when reads
Thread thread2 = new Thread(runnable2); are frequent.
thread1.start(); ReentrantLock lock = new ReentrantLock();
thread2.start(); lock.lock();
try { try {
thread1.join(); // Critical section
thread2.join(); } finally {
} catch (InterruptedException e) { lock.unlock();
e.printStackTrace(); }
} Q4. Compare different ways to implement threads in Java
System.out.println("All threads have finished execution."); using the Thread class and Runnable interface.
} class MyThread extends Thread {
} @Override
public void run() {
Q2. Differentiate Between Multitasking and Multi processing.
System.out.println(Thread.currentThread().getName() + "
Multitasking;- 1.The execution of more than one task
is running.");
simultaneously is known as multitasking.
}
2.The number of CPU is one.3.It takes moderate amount of
time.4. this, one by one job is being executed at a time. }
5.It is economical. 6.The number of users is more than one. public class ThreadExample {
7.Throughput is moderate. 8.Its efficiency is moderate. public static void main(String[] args) {
9. It is of two types: Single user multitasking and Multiple user MyThread thread1 = new MyThread();
multitasking. MyThread thread2 = new MyThread();
Multiprocessing;-1. The availability of more than one thread1.start();
processor per system, that can execute several set of thread2.start();
instructions in parallel is known as multiprocessing.2.The
}
number of CPUs is more than one.3.It takes less time for job
processing.4.In this, more than one process can be executed }
at a time.5.It is less economical. 6.The number of users is can EX.2. class MyRunnable implements Runnable {
be one or more than one. 7. Throughput is maximum. @Override
8.Its efficiency is maximum. 9. It is of two types: Symmetric public void run() {
Multiprocessing and Asymmetric Multiprocessing.
System.out.println(Thread.currentThread().getName() +
Q3. Discuss Synchronization with ways to achieve " is running.");
Synchronization. }
Process Synchronization is the coordination of execution of }
multiple processes in a multi-process system to ensure that
they access shared resources in a controlled and predictable public class RunnableExample {
manner. It aims to resolve the problem of race conditions and public static void main(String[] args) {
other synchronization issues in a concurrent system. MyRunnable runnable = new MyRunnable();
portance of Synchronization Thread thread1 = new Thread(runnable);
 Data Integrity: Ensures that multiple threads or processes Thread thread2 = new Thread(runnable);
do not interfere with each other while accessing shared thread1.start();
data.
thread2.start();
} }
Q5. Explain the following layout managers (a) Border layout. FileOutputStream: Writes bytes to a file.
(b) Grid layout. (c) Flow layout.
BufferedOutputStream: Buffers output for performance
1.BorderLayout;-
improvement.
Overview
Description: Divides the container into five regions: North, DataOutputStream: Allows writing Java primitive data types.
South, East, West, and Center.
ByteArrayOutputStream: Writes to a byte array in memory.
Usage: It is ideal for creating a layout where you want a
component to stretch and fill a specific area. Use Cases
Behavior
Reading and writing binary files (images, videos).
 The center area takes up all the remaining space after the
other regions are laid out. Network communication where data is sent and received a.
 Each region can hold one component, and components Handling data in formats not based on character encoding.
can resize when the container is resized.
2.GridLayout Q7. Compare the AWT and Swing frameworks, highlighting their
Overview use cases and difference.
Description: Arranges components in a rectangular grid of AWT (Abstract Window Toolkit)
cells. Each component takes up the same amount of space
in a grid defined by a specified number of rows and Overview
columns.
 Native Components: AWT uses native system
Usage: Best for layouts where you want to display components
uniformly. components, meaning it relies on the operating system
for its GUI elements.
Behavior
 All components are of equal size.  Lightweight vs. Heavyweight: AWT components are
 The number of rows and columns can be defined, and the heavyweight because they rely on the native GUI toolkit
layout will automatically adjust to fill the grid. of the platform, which can lead to inconsistencies in
appearance across different systems.
3.FlowLayout
Overview Key Features
Description: Arranges components in a left-to-right flow,
 Performance: Generally faster for simple applications
similar to words on a line. When the line is filled, it wraps
to the next line. due to direct use of native components.

Usage: Suitable for simple layouts where you want  Limited Flexibility: Limited customization options
components to be laid out in a sequence. because it depends on the native look and feel.
Behavior
 Event Handling: AWT uses a simpler event handling
Components maintain their preferred size.
model compared to Swing. USE CASES
You can align components to the left, right, center, or specify
that they should be filled.
 Suitable for simple GUI applications where native
Q6. Explain the different types of I/O streams in Java and appearance is desired.
their use cases.  Best for applications that need to leverage native
functionality or where performance is critical with
Byte Streams minimal UI complexity.
Byte streams handle raw binary data and are used for reading and
writing bytes. They are primarily used when dealing with binary Swing
files, such as images, audio files, or any non-text data. Overview
Key Classes
 Lightweight Components: Swing components are
InputStream: The abstract class that represents an input lightweight; they are written entirely in Java and do not
stream of bytes. rely on native GUI components.
FileInputStream: Reads bytes from a file.  Pluggable Look and Feel: Swing allows developers to
customize the appearance of the GUI, offering various
BufferedInputStream: Buffers input for performance
improvement. themes and styles.

DataInputStream: Allows reading Java primitive data types Use Cases


(int, float, etc.) in a machine-independent way.
 Ideal for complex applications requiring rich user
ByteArrayInputStream: Reads from a byte array in memory. interfaces (e.g., IDEs, data management tools).

OutputStream: The abstract class that represents an output  Suitable for cross-platform applications where a
stream of bytes. consistent look and feel is necessary.

You might also like