APP Assignment
APP Assignment
Abhisesha Kumaara
REG NO - RA2211026010487
import java.util.Random;
between 0 and 99
System.out.println(Thread.currentThread().getName() + " generated: " +
randomNumber); if (randomNumber % 2 == 0) {
try {
e.printStackTrace();
@Override
}
class CubeCalculator implements Runnable
{ private int number;
this.number = number;
@Override
OUTPUT :
1. Write a java program for to solve producer consumer problem in
which a producer produce a value and consumer consume the value
before producer generate the next value.
import java.util.LinkedList;
producerThread.start();
consumerThread.start();
class Buffer {
this.capacity = capacity;
queue.add(item);
notify();
this.buffer = buffer;
}
@Override
try {
buffer.produce(i);
} catch (InterruptedException e) {
e.printStackTrace();
this.buffer = buffer;
@Override
try {
} catch (InterruptedException e) {
e.printStackTrace();
OUTPUT :
1. Write a java program in which threads sleep for 5 sec and change the
name of the thread.
thread.start();
@Override
{ try {
e.printStackTrace();
OUTPUT :
1. Write a java program in which threads sleep for 6 sec in the loop in
reverse order from 5 to 1 and change the name of the thread.
thread.start();
@Override
{ try {
currentThread.setName("Thread-" + i);
} catch (InterruptedException e) {
e.printStackTrace();
OUTPUT :
1. Write a java program for multi thread in which user thread and thread
started from the main method invoked at a time each thread sleeps for 1
sec.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
OUTPUT :
import java.util.Queue;
import java.util.LinkedList;
job1.start();
job2.start();
job3.start();
class PrinterQueue {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
currentJobNumber++;
notifyAll();
}
class PrintJob implements Runnable {
@Override
printerQueue.print(jobName);
OUTPUT :
this.input = input;
@Override
input.toCharArray()) { if
(Character.isDigit(ch)) {
digitsCount++;
System.out.println("ThreadA:" + digitsCount);
this.input = input;
@Override
ch : input.toCharArray()) { if
(Character.isLetter(ch)) {
alphabeticCount++;
System.out.println("ThreadB:" + alphabeticCount);
OUTPUT :
import java.util.Scanner;
public class UserThreadPriority extends Thread
{ private String k; private char c;
super(name);
@Override
threadobj2.k = userInputString;
threadobj2.c = userInputChar;
• Start the threads
threadobj1.start();
threadobj2.start();
OUTPUT :
1. Write java program using sleep() method of Thread class where the
new class thread created from the previous class is implemented by
using sleep method for 10,20,50,70,100 ns
threadobj1.start();
threadobj2.start();
threadobj3.start();
threadobj4.start();
threadobj5.start();
long sleepDuration;
@Override
try {
} catch (InterruptedException e)
{ e.printStackTrace();
OUTPUT :
1. Write a java Thread program using Thread Priority for 5 processes
and execute the priority order among the processes.
}
class CustomThread extends Thread {
@Override
OUTPUT :