import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class PrintJobManagementSystem {
// Thread-safe print job queue
private BlockingQueue printQueue;
public PrintJobManagementSystem(int queueCapacity) {
this.printQueue = new LinkedBlockingQueue<>(queueCapacity);
}
// Print job submission method
public void submitPrintJob(PrintJob job) throws InterruptedException {
// Blocks if queue is full
printQueue.put(job);
System.out.println("Job Submitted: " + job);
}
// Printer worker thread
public void startPrinterWorker() {
new Thread(() -> {
while (true) {
try {
// Blocks and waits if no jobs are available
PrintJob job = printQueue.take();
processPrintJob(job);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}).start();
}
private void processPrintJob(PrintJob job) {
// Simulate print processing
System.out.println("Processing: " + job);
try {
// Simulate printing time based on pages
Thread.sleep(job.getPages() * 100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Completed: " + job);
}
// Print Job Model
public static class PrintJob {
private String documentName;
private int pages;
public PrintJob(String documentName, int pages) {
this.documentName = documentName;
this.pages = pages;
}
public int getPages() { return pages; }
@Override
public String toString() {
return documentName + " (" + pages + " pages)";
}
}
// Demonstration method
public static void main(String[] args) throws InterruptedException {
PrintJobManagementSystem printerSystem =
new PrintJobManagementSystem(5);
// Start printer worker
printerSystem.startPrinterWorker();
// Simulate multiple users submitting print jobs
new Thread(() -> {
try {
printerSystem.submitPrintJob(
new PrintJob("Report.docx", 10)
);
printerSystem.submitPrintJob(
new PrintJob("Presentation.pptx", 20)
);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}).start();
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TradingPlatformOrderSystem {
private final Lock orderLock = new ReentrantLock();
private final Condition orderAvailable = orderLock.newCondition();
private volatile boolean isOrderProcessing = false;
public void submitOrder(TradeOrder order) throws InterruptedException {
orderLock.lock();
try {
// Wait if another order is being processed
while (isOrderProcessing) {
orderAvailable.await();
}
// Process the order
processOrder(order);
// Signal that order processing is complete
isOrderProcessing = false;
orderAvailable.signal();
} finally {
orderLock.unlock();
}
}
private void processOrder(TradeOrder order) {
System.out.println("Processing Order: " + order);
try {
// Simulate order processing time
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// Trade Order Model
public static class TradeOrder {
private String symbol;
private int quantity;
private double price;
public TradeOrder(String symbol, int quantity, double price) {
this.symbol = symbol;
this.quantity = quantity;
this.price = price;
}
@Override
public String toString() {
return "Order{" +
"symbol='" + symbol + '\'' +
", quantity=" + quantity +
", price=" + price +
'}';
}
}
// Demonstration method
public static void main(String[] args) {
TradingPlatformOrderSystem tradingSystem =
new TradingPlatformOrderSystem();
// Simulate multiple traders
Runnable traderTask = () -> {
try {
tradingSystem.submitOrder(
new TradeOrder("AAPL", 100, 150.50)
);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
// Create multiple trader threads
new Thread(traderTask, "Trader-1").start();
new Thread(traderTask, "Trader-2").start();
}
}
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class DistributedTaskScheduler {
private final BlockingQueue taskQueue;
private final int workerCount;
public DistributedTaskScheduler(int queueCapacity, int workerCount) {
this.taskQueue = new LinkedBlockingQueue<>(queueCapacity);
this.workerCount = workerCount;
}
// Submit task to the queue
public void submitTask(Task task) throws InterruptedException {
taskQueue.put(task);
}
// Start worker threads
public void startWorkers() {
for (int i = 0; i < workerCount; i++) {
new Thread(new Worker(), "Worker-" + (i + 1)).start();
}
}
// Worker thread implementation
private class Worker implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
// Take task from queue, wait if empty
Task task = taskQueue.take();
processTask(task);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
private void processTask(Task task) {
System.out.println(
Thread.currentThread().getName() +
" processing: " + task
);
try {
// Simulate task processing
Thread.sleep(task.getProcessingTime());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
// Task Model
public static class Task {
private String name;
private long processingTime;
public Task(String name, long processingTime) {
this.name = name;
this.processingTime = processingTime;
}
public long getProcessingTime() { return processingTime; }
@Override
public String toString() {
return "Task{name='" + name + "'}";
}
}
// Demonstration method
public static void main(String[] args) throws InterruptedException {
DistributedTaskScheduler scheduler =
new DistributedTaskScheduler(10, 3);
// Start worker threads
scheduler.startWorkers();
// Submit tasks
scheduler.submitTask(new Task("Task-1", 1000));
scheduler.submitTask(new Task("Task-2", 500));
scheduler.submitTask(new Task("Task-3", 750));
}
}