Lab Assignment – 08
1. Write a program to create thread using Thread class and runnable interface.
i. class MyThread extends Thread {
public void run() {
[Link]("Thread using Thread class is running");
}
}
public class ThreadExample1 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link](); // starts thread
}
}
ii. class MyRunnable implements Runnable {
public void run() {
[Link]("Thread using Runnable interface is running");
}
}
public class ThreadExample2 {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
[Link]();
}
}
2. WAP to show the use od setname() set priority () and isAlive() and join().
class MyThread2 extends Thread {
public void run() {
for (int i = 1; i <= 3; i++) {
[Link]([Link]().getName() + " running");
}
}
}
public class ThreadMethodsDemo {
public static void main(String[] args) throws InterruptedException {
MyThread2 t1 = new MyThread2();
MyThread2 t2 = new MyThread2();
[Link]("First Thread");
[Link]("Second Thread");
[Link](Thread.MAX_PRIORITY);
[Link](Thread.MIN_PRIORITY);
[Link]();
[Link](); // wait for t1 to finish
[Link]();
[Link]("Is t1 alive? " + [Link]());
[Link]("Is t2 alive? " + [Link]());
}
}
3. WAP to show thread priority.
class PriorityThread extends Thread {
public void run() {
[Link]([Link]().getName() +
" Priority: " + [Link]().getPriority());
}
}
public class ThreadPriorityDemo {
public static void main(String[] args) {
PriorityThread t1 = new PriorityThread();
PriorityThread t2 = new PriorityThread();
PriorityThread t3 = new PriorityThread();
[Link]("Low Priority");
[Link]("Medium Priority");
[Link]("High Priority");
[Link](Thread.MIN_PRIORITY); // 1
[Link](Thread.NORM_PRIORITY); // 5
[Link](Thread.MAX_PRIORITY); // 10
[Link]();
[Link]();
[Link]();
}
}
4. WAP to show thread synchronization.
class Counter {
int count = 0;
synchronized void increment() {
count++;
}
}
public class SynchronizationDemo {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) [Link]();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) [Link]();
});
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Final Count: " + [Link]);
}
}
5. WAP to show inter thread communication.
class Shared {
int number;
boolean available = false;
synchronized void produce(int n) throws InterruptedException {
while (available)
wait();
number = n;
[Link]("Produced: " + n);
available = true;
notify();
}
synchronized void consume() throws InterruptedException {
while (!available)
wait();
[Link]("Consumed: " + number);
available = false;
notify();
}
}
public class InterThreadDemo {
public static void main(String[] args) {
Shared obj = new Shared();
Thread producer = new Thread(() -> {
try {
for (int i = 1; i <= 5; i++)
[Link](i);
} catch (Exception e) {}
});
Thread consumer = new Thread(() -> {
try {
for (int i = 1; i <= 5; i++)
[Link]();
} catch (Exception e) {}
});
[Link]();
[Link]();
}
}