0% found this document useful (0 votes)
4 views8 pages

Ass 9

The document contains multiple Java code examples demonstrating thread creation, execution, and management. It includes examples of extending the Thread class, implementing the Runnable interface, managing thread states, using file I/O with threads, synchronizing access to shared resources, and setting thread priorities. Each example is accompanied by comments and expected outputs to illustrate the functionality of the code.

Uploaded by

street28gamer
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)
4 views8 pages

Ass 9

The document contains multiple Java code examples demonstrating thread creation, execution, and management. It includes examples of extending the Thread class, implementing the Runnable interface, managing thread states, using file I/O with threads, synchronizing access to shared resources, and setting thread priorities. Each example is accompanied by comments and expected outputs to illustrate the functionality of the code.

Uploaded by

street28gamer
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/ 8

NAME- RIYA DESHMANKAR

ROLL NO.-2300290100212
SEC-C
QUES1.)class MyThread extends Thread {

public void run() {

for (int i = 1; i <= 5; i++) {

try {

Thread.sleep(1000); // 1000 milliseconds = 1 second

System.out.println(Thread.currentThread().getName());

} catch (InterruptedException e) {

System.out.println(e);

}}}

public static void main(String[] args) {

MyThread t1 = new MyThread();

MyThread t2 = new MyThread();

t1.setName("Thread A");

t2.setName("Thread B");

t1.start();

t2.start();}}Output
QUES 2.)

class PrintTask implements Runnable {

private String text;

public PrintTask(String text) {

this.text = text;

public void run() {

for (int i = 0; i < 10; i++) {

System.out.println(text);

try { Thread.sleep(300);

} catch (InterruptedException e) {

System.out.println(e); }

public static void main(String[] args) {

Thread t1 = new Thread(new PrintTask("TOM"));

Thread t2 = new Thread(new PrintTask("JERRY"));

t1.start();

t2.start();}

output
Ques3.)

class ThreadStates implements Runnable {

public void run() {

try {

Thread.sleep(1500); // TIMED_WAITING

} catch (InterruptedException e) {

System.out.println(e);

System.out.println("Thread has completed execution.");

public static void main(String[] args) {

ThreadStates runnable = new ThreadStates();

Thread t = new Thread(runnable);

System.out.println("State before start: " + t.getState()); // NEW

t.start();

System.out.println("State after start: " + t.getState()); // RUNNABLE

try {

Thread.sleep(500);

System.out.println("State after 0.5 sec: " + t.getState()); // TIMED_WAITING

Thread.sleep(2000);

System.out.println("State after 2 sec: " + t.getState()); // TERMINATED

} catch (InterruptedException e) {

System.out.println(e);}}}
Output

Ques4

import java.io.*;

class JoinDemo extends Thread {

public void run() {

try (FileWriter fw = new FileWriter("output.txt");

BufferedWriter bw = new BufferedWriter(fw)) {

bw.write("Thread has written this to the file.");

} catch (IOException e) {

System.out.println(e);

public static void main(String[] args) {

JoinDemo t = new JoinDemo();

t.start();

try {
t.join(); // Wait for thread to finish

// Now read the file

BufferedReader br = new BufferedReader(new FileReader("output.txt"));

String line;

System.out.println("Reading from file:");

while ((line = br.readLine()) != null) {

System.out.println(line);

br.close();

} catch (InterruptedException | IOException e) {

System.out.println(e);

}
Ques 5

class SharedResource {

synchronized void printCount(String threadName) {

for (int i = 1; i <= 5; i++) {

System.out.println(threadName + ": " + i);

try {

Thread.sleep(500);

} catch (InterruptedException e) {

System.out.println(e);

class SyncThread extends Thread {

SharedResource resource;

String name;

SyncThread(SharedResource res, String name) {

this.resource = res;

this.name = name;

public void run() {

resource.printCount(name);

}
public static void main(String[] args) {

SharedResource res = new SharedResource();

SyncThread t1 = new SyncThread(res, "Thread 1");

SyncThread t2 = new SyncThread(res, "Thread 2");

t1.start();

t2.start();

}
Ques

class PriorityExample implements Runnable {

public void run() {

System.out.println("Thread: " + Thread.currentThread().getName() +

", Priority: " + Thread.currentThread().getPriority());

public static void main(String[] args) {

Thread t1 = new Thread(new PriorityExample(), "Low Priority Thread");

Thread t2 = new Thread(new PriorityExample(), "Normal Priority Thread");

Thread t3 = new Thread(new PriorityExample(), "High Priority Thread");

t1.setPriority(Thread.MIN_PRIORITY); // 1

t2.setPriority(Thread.NORM_PRIORITY); // 5

t3.setPriority(Thread.MAX_PRIORITY); // 10

t1.start();

t2.start();

t3.start();

}}

Output

You might also like