Java Thread Programs With Static Input & Sample
Output
1) Thread Using Thread Class (Print Message 5 Times)
Java Program:
import [Link];
public class HelloThread {
static class MyThread extends Thread {
private final int times;
MyThread(int times) {
[Link] = times;
}
@Override
public void run() {
for (int i = 1; i <= times; i++) {
[Link]("Hello from Thread! (" + i + ")");
try { [Link](300); } catch (InterruptedException e) {}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Press Enter to start the thread.");
[Link]();
MyThread t = new MyThread(5);
[Link]();
try { [Link](); } catch (InterruptedException e) {}
[Link]("Main finished.");
[Link]();
}
}
Sample Output:
Press Enter to start the thread.
Hello from Thread! (1)
Hello from Thread! (2)
Hello from Thread! (3)
Hello from Thread! (4)
Hello from Thread! (5)
Main finished.
2) Multiple Threads Displaying Which Thread Is Running
Java Program:
import [Link];
public class WhichThreadRuns {
static class Worker extends Thread {
private final int iterations;
Worker(String name, int iterations) {
super(name);
[Link] = iterations;
}
@Override
public void run() {
for (int i = 1; i <= iterations; i++) {
[Link]("Running: " + getName() + " (iteration " + i + ")");
try { [Link](200); } catch (InterruptedException e) {}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of threads: ");
int n = [Link]();
[Link]("Enter iterations per thread: ");
int iters = [Link]();
Thread[] threads = new Thread[n];
for (int i = 0; i < n; i++) {
threads[i] = new Worker("Worker-" + (i + 1), iters);
threads[i].start();
}
for (Thread t : threads) {
try { [Link](); } catch (InterruptedException e) {}
}
[Link]("All threads finished.");
[Link]();
}
}
Sample Output:
Enter number of threads: 3
Enter iterations per thread: 2
Running: Worker-1 (iteration 1)
Running: Worker-2 (iteration 1)
Running: Worker-3 (iteration 1)
Running: Worker-1 (iteration 2)
Running: Worker-2 (iteration 2)
Running: Worker-3 (iteration 2)
All threads finished.
3) Producer–Consumer Simulation Using Threads
Java Program:
import [Link];
import [Link];
import [Link];
public class ProducerConsumer {
private final Queue<Integer> buffer = new LinkedList<>();
private final int capacity;
private final int totalItems;
public ProducerConsumer(int capacity, int totalItems) {
[Link] = capacity;
[Link] = totalItems;
}
class Producer extends Thread {
@Override
public void run() {
for (int i = 1; i <= totalItems; i++) {
produce(i);
try { [Link](150); } catch (InterruptedException e) {}
}
}
private void produce(int item) {
synchronized (buffer) {
while ([Link]() == capacity) {
try { [Link](); } catch (InterruptedException e) {}
}
[Link](item);
[Link]("Produced: " + item + " | Buffer size = " + [Link]());
[Link]();
}
}
}
class Consumer extends Thread {
@Override
public void run() {
int consumedCount = 0;
while (consumedCount < totalItems) {
Integer item = consume();
if (item != null) {
consumedCount++;
[Link]("Consumed: " + item + " | Buffer size = " + [Link]());
}
try { [Link](200); } catch (InterruptedException e) {}
}
}
private Integer consume() {
synchronized (buffer) {
while ([Link]()) {
try { [Link](); } catch (InterruptedException e) {}
}
Integer item = [Link]();
[Link]();
return item;
}
}
}
public void startSimulation() throws InterruptedException {
Thread producer = new Producer();
Thread consumer = new Consumer();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Simulation finished.");
}
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner([Link]);
[Link]("Enter buffer capacity: ");
int cap = [Link]();
[Link]("Enter number of items: ");
int items = [Link]();
ProducerConsumer pc = new ProducerConsumer(cap, items);
[Link]();
[Link]();
}
}
Sample Output:
Enter buffer capacity: 3
Enter number of items: 7
Produced: 1 | Buffer size = 1
Consumed: 1 | Buffer size = 0
Produced: 2 | Buffer size = 1
Produced: 3 | Buffer size = 2
Consumed: 2 | Buffer size = 1
Produced: 4 | Buffer size = 2
Produced: 5 | Buffer size = 3
Consumed: 3 | Buffer size = 2
Produced: 6 | Buffer size = 3
Consumed: 4 | Buffer size = 2
Produced: 7 | Buffer size = 3
Consumed: 5 | Buffer size = 2
Consumed: 6 | Buffer size = 1
Consumed: 7 | Buffer size = 0
Simulation finished.