[Go to site: main page, start]

0% found this document useful (0 votes)
6 views7 pages

Java Notes Unit2

This document covers multithreaded programming and I/O streams in Java, detailing concepts such as multithreading, synchronization, and interthread communication. It provides examples of using the Thread class and Runnable interface, as well as methods for file handling and console input/output. Key topics include deadlock, byte and character streams, and the use of the BufferedReader for reading console input.

Uploaded by

Kani
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)
6 views7 pages

Java Notes Unit2

This document covers multithreaded programming and I/O streams in Java, detailing concepts such as multithreading, synchronization, and interthread communication. It provides examples of using the Thread class and Runnable interface, as well as methods for file handling and console input/output. Key topics include deadlock, byte and character streams, and the use of the BufferedReader for reading console input.

Uploaded by

Kani
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

UNIT III – MULTITHREADED PROGRAMMING & I/O STREAMS

(Based on Herbert Schildt – Java: The Complete Reference)

PART A: MULTITHREADED PROGRAMMING

1. Multithreading
Definition
Multithreading is a feature of Java that allows multiple parts of a program to execute
simultaneously.
Each part of such a program is called a thread, and each thread defines a separate path of
execution.
Advantages
 Better CPU utilization
 Faster program execution
 Efficient resource sharing

2. Thread Class
Definition
Java provides the Thread class ([Link]) to create and manage threads.
A thread is created by extending the Thread class and overriding the run() method.

Syntax
class ThreadName extends Thread {
public void run() {
// thread code
}
}

Program
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}

public static void main(String args[]) {


MyThread t = new MyThread();
[Link]();
}
}

Input
No input
Output
Thread is running

3. Runnable Interface
Definition
The Runnable interface is used to create a thread by implementing the run() method.
It is preferred when a class already extends another class.
Syntax
class ClassName implements Runnable {
public void run() {
// thread code
}
}

Program
class MyRunnable implements Runnable {
public void run() {
[Link]("Runnable thread executing");
}

public static void main(String args[]) {


MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
[Link]();
}
}

Input
No input
Output
Runnable thread executing

4. Synchronization
Definition
Synchronization is the process of controlling access to shared resources to avoid data
inconsistency.
Java uses the synchronized keyword for synchronization.

5. Synchronized Method
Definition
When a method is declared as synchronized, only one thread can access it at a time.

Syntax
synchronized returnType methodName() {
// critical section
}

Program
class Table {
synchronized void printTable(int n) {
for(int i=1;i<=5;i++) {
[Link](n*i);
}
}
}

class MyThread1 extends Thread {


Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
[Link](5);
}
}

class MyThread2 extends Thread {


Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
[Link](10);
}
}

class SyncDemo {
public static void main(String args[]) {
Table obj = new Table();
new MyThread1(obj).start();
new MyThread2(obj).start();
}
}

Input
No input
Output
5
10
15
20
25
50
100
150
200
250
(Output is synchronized, no mixing)

6. Synchronized Statement
Definition
A synchronized statement synchronizes only a block of code, not the entire method.

Syntax
synchronized(object) {
// critical section
}
Program
class Display {
void show(String msg) {
synchronized(this) {
[Link]("[ " + msg);
try {
[Link](1000);
} catch(Exception e) {}
[Link](" ]");
}
}
}

Output
[ Hello ]
[ World ]

7. Interthread Communication
Definition
Interthread communication allows threads to communicate with each other using:
 wait()
 notify()
 notifyAll()
These methods belong to the Object class.

Syntax
wait();
notify();
notifyAll();

Program (Simple Example)


class Demo {
synchronized void display() {
try {
wait();
} catch(Exception e) {}
[Link]("Thread resumed");
}

synchronized void resumeThread() {


notify();
}
}

Output
Thread resumed

8. Deadlock
Definition
Deadlock occurs when two or more threads wait indefinitely for each other to release
resources.

Program (Deadlock Example)


class DeadlockDemo {
public static void main(String args[]) {
final String r1 = "Resource1";
final String r2 = "Resource2";

Thread t1 = new Thread() {


public void run() {
synchronized(r1) {
synchronized(r2) {
[Link]("Thread 1");
}
}
}
};

Thread t2 = new Thread() {


public void run() {
synchronized(r2) {
synchronized(r1) {
[Link]("Thread 2");
}
}
}
};

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

Output
Program halts (deadlock occurs)

PART B: I/O STREAMS

9. Streams
Definition
A stream is a sequence of data used for input and output operations.
Types:
 Input Stream
 Output Stream

10. Byte Streams


Definition
Byte streams handle binary data such as images and audio files.
Classes
 FileInputStream
 FileOutputStream

Program
import [Link].*;

class ByteStreamDemo {
public static void main(String args[]) throws Exception {
FileOutputStream fout = new FileOutputStream("[Link]");
[Link](65);
[Link]();
}
}

Output
File created with character A

11. Character Streams


Definition
Character streams handle text data.

Classes
 FileReader
 FileWriter

Program
import [Link].*;

class CharStreamDemo {
public static void main(String args[]) throws Exception {
FileWriter fw = new FileWriter("[Link]");
[Link]("Java Programming");
[Link]();
}
}

Output
Text written into file

12. Reading Console Input


Definition
Console input is read using BufferedReader with InputStreamReader.

Program
import [Link].*;

class ConsoleRead {
public static void main(String args[]) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter your name:");


String name = [Link]();
[Link]("Hello " + name);
}
}

Input
Preetha
Output
Hello Preetha

13. Writing Console Output


Definition
Java uses [Link] to display output on console.

Methods
 print()
 println()
 printf()

14. File Handling


Definition
File handling allows Java programs to create, read, write, and delete files.

Program
import [Link].*;

class FileDemo {
public static void main(String args[]) throws Exception {
File f = new File("[Link]");
[Link]("File exists: " + [Link]());
}
}

Output
File exists: true

You might also like