0% found this document useful (0 votes)
21 views6 pages

Java Io Concurrency Notes

Uploaded by

firekirtans
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)
21 views6 pages

Java Io Concurrency Notes

Uploaded by

firekirtans
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
You are on page 1/ 6

Java: File I/O (NIO.

2), Serialization, Concurrency, and Locks

File I/O (NIO.2)

Introduction

Key Concepts in NIO.2

Example: Creating a Path


import java.nio.file.*;

public class PathExample {


public static void main(String[] args) {
Path path = Paths.get("C:/example.txt");
System.out.println("Path: " + path);
}
}

File Operations using Files Class


import java.nio.file.*;
import java.io.IOException;

public class FileOperations {


public static void main(String[] args) {
Path path = Paths.get("C:/example.txt");
try {
if (!Files.exists(path)) {
Files.createFile(path);
System.out.println("File created.");
}
Files.writeString(path, "Hello, NIO.2!", StandardOpenOption.APPEND);
System.out.println("File content: " + Files.readString(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serialization and Deserialization

Introduction

Steps for Serialization

Serialization Example
import java.io.*;

class Student implements Serializable {


private static final long serialVersionUID = 1L;
int id;
String name;

public Student(int id, String name) {


this.id = id;
this.name = name;
}
}

public class SerializeExample {


public static void main(String[] args) {
try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("student.ser"))) {
Student student = new Student(1, "John");
oos.writeObject(student);
System.out.println("Object serialized successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

Deserialization Example
public class DeserializeExample {
public static void main(String[] args) {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("student.ser"))) {
Student student = (Student) ois.readObject();
System.out.println("ID: " + student.id + ", Name: " + student.name);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Concurrency and Multithreading

Introduction

Thread Creation Methods

Extending Thread Class


class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}

public class ThreadExample {


public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}

Implementing Runnable Interface


class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable thread running...");
}
}

public class RunnableExample {


public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
Synchronization and Locks

Introduction

Synchronized Block
class SharedResource {
public synchronized void display(String message) {
System.out.print("[" + message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}

class MyThread extends Thread {


SharedResource resource;
String message;

MyThread(SharedResource resource, String message) {


this.resource = resource;
this.message = message;
}

public void run() {


resource.display(message);
}
}

public class SyncExample {


public static void main(String[] args) {
SharedResource resource = new SharedResource();
MyThread t1 = new MyThread(resource, "Hello");
MyThread t2 = new MyThread(resource, "World");
t1.start();
t2.start();
}
}
Locks in Java

Reentrant Lock
import java.util.concurrent.locks.*;

class SharedCounter {
private int count = 0;
private final Lock lock = new ReentrantLock();

public void increment() {


lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}

public int getCount() {


return count;
}
}

public class LockExample {


public static void main(String[] args) {
SharedCounter counter = new SharedCounter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) counter.increment();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) counter.increment();
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Count: " + counter.getCount());
}
}
Conclusion

You might also like