Ass 9
Ass 9
ROLL NO.-2300290100212
SEC-C
QUES1.)class MyThread extends Thread {
try {
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
System.out.println(e);
}}}
t1.setName("Thread A");
t2.setName("Thread B");
t1.start();
t2.start();}}Output
QUES 2.)
this.text = text;
System.out.println(text);
try { Thread.sleep(300);
} catch (InterruptedException e) {
System.out.println(e); }
t1.start();
t2.start();}
output
Ques3.)
try {
Thread.sleep(1500); // TIMED_WAITING
} catch (InterruptedException e) {
System.out.println(e);
t.start();
try {
Thread.sleep(500);
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(e);}}}
Output
Ques4
import java.io.*;
} catch (IOException e) {
System.out.println(e);
t.start();
try {
t.join(); // Wait for thread to finish
String line;
System.out.println(line);
br.close();
System.out.println(e);
}
Ques 5
class SharedResource {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
SharedResource resource;
String name;
this.resource = res;
this.name = name;
resource.printCount(name);
}
public static void main(String[] args) {
t1.start();
t2.start();
}
Ques
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