import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
class Producer implements Runnable {
private BlockingQueue<String> buffer;
public Producer(BlockingQueue<String> buffer) {
this.buffer = buffer;
}
public void run() {
try {
for (int i = 0; i < 10; i++) {
String data = "Data " + i;
buffer.put(data);
System.out.println("Produced: " + data);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer implements Runnable {
private BlockingQueue<String> buffer;
public Consumer(BlockingQueue<String> buffer) {
this.buffer = buffer;
}
public void run() {
try {
while (true) {
String data = buffer.take();
System.out.println("Consumed: " + data);
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
BlockingQueue<String> buffer = new ArrayBlockingQueue<>(5);
Thread producerThread = new Thread(new Producer(buffer));
Thread consumerThread = new Thread(new Consumer(buffer));
producerThread.start();
consumerThread.start();
}
}
import java.util.concurrent.*;
class ThreadPoolDecorator implements ExecutorService {
private ExecutorService executor;
public ThreadPoolDecorator(ExecutorService executor) {
this.executor = executor;
}
public void execute(Runnable command) {
// Add additional functionality here
executor.execute(command);
}
// Implement other methods of ExecutorService interface
// ...
public static ExecutorService newFixedThreadPool(int nThreads) {
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
return new ThreadPoolDecorator(executor);
}
}
public class Main {
public static void main(String[] args) {
ExecutorService executor = ThreadPoolDecorator.newFixedThreadPool(5);
executor.execute(new Runnable() {
public void run() {
System.out.println("Task executed!");
}
});
executor.shutdown();
}
}