Multithreaded Programming in Java.docx Chatgpt
Multithreaded Programming in Java.docx Chatgpt
1. Thread Class:
The Thread class in Java is part of the java.lang package. It represents a single thread
of execution.
Threads can be created by either:
1. Extending the Thread class: Create a subclass of Thread and override its run()
method.
2. Implementing the Runnable interface: This is more flexible as it allows a class
to extend another class while still providing thread functionality.
java
CopyEdit
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
2. Runnable Interface:
The Runnable interface represents a task that can be executed by a thread. It has only one
method: run().
A class can implement the Runnable interface and pass it to a Thread object.
java
CopyEdit
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable is running");
}
}
3. Synchronization:
java
CopyEdit
class Counter {
private int count = 0;
java
CopyEdit
class Counter {
private int count = 0;
4. Interthread Communication:
Interthread communication allows threads to communicate with each other using methods
like wait(), notify(), and notifyAll() (these are methods in the Object class).
These methods allow threads to communicate and coordinate with each other (e.g., one
thread can wait until another thread signals it to proceed).
java
CopyEdit
class SharedResource {
synchronized void printMessages() throws InterruptedException {
System.out.println("Message from thread: " +
Thread.currentThread().getName());
Thread.sleep(1000); // Simulating some work
}
}
5. Deadlock:
Deadlock occurs when two or more threads are blocked forever because they are waiting
for each other to release resources.
A deadlock situation typically involves four conditions: mutual exclusion, hold and wait,
no preemption, and circular wait.
Example of deadlock:
java
CopyEdit
class A {
synchronized void methodA(B b) {
System.out.println("Thread 1: Holding lock A...");
try { Thread.sleep(1000); } catch (Exception e) {}
System.out.println("Thread 1: Waiting for lock B...");
b.last();
}
class B {
synchronized void methodB(A a) {
System.out.println("Thread 2: Holding lock B...");
try { Thread.sleep(1000); } catch (Exception e) {}
System.out.println("Thread 2: Waiting for lock A...");
a.last();
}
t1.start();
t2.start();
}
}
1. Concepts of Streams:
A stream is a sequence of data elements that are read from or written to an I/O device.
Java supports two types of streams: byte streams and character streams.
2. Stream Classes:
Byte Streams: These handle raw binary data (e.g., image files, audio files).
o Classes: InputStream, OutputStream, FileInputStream, FileOutputStream.
Character Streams: These handle character data and are designed for reading and
writing text.
o Classes: Reader, Writer, FileReader, FileWriter.
3. Byte Stream:
java
CopyEdit
import java.io.*;
4. Character Stream:
java
CopyEdit
import java.io.*;
You can use System.in for reading input from the console and System.out for printing
output to the console.
java
CopyEdit
import java.util.Scanner;
6. File Handling:
File handling in Java is done through classes in the java.io package, such as File,
FileReader, FileWriter, BufferedReader, and BufferedWriter.
java
CopyEdit
import java.io.*;
// Writing to file
FileWriter writer = new FileWriter(file);
writer.write("Hello, this is a file!");
writer.close();