1. Image Processing System

Industry Context

  • Used in photo editing applications
  • Parallel image transformation
  • Reduces processing time

Scenario Details

  • Batch image filtering
  • Concurrent image transformations
  • Efficient resource utilization

public class ImageProcessingSystem {
    public static void processImages(List images) {
        // Thread Inheritance Approach
        List processorThreads = new ArrayList<>();
        
        for (BufferedImage image : images) {
            ImageProcessorThread processorThread = 
                new ImageProcessorThread(image);
            processorThread.start();
            processorThreads.add(processorThread);
        }
        
        // Wait for all threads to complete
        for (ImageProcessorThread thread : processorThreads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
    
    // Custom Thread for Image Processing
    static class ImageProcessorThread extends Thread {
        private BufferedImage image;
        
        public ImageProcessorThread(BufferedImage image) {
            this.image = image;
        }
        
        @Override
        public void run() {
            applyFilter(image);
        }
        
        private void applyFilter(BufferedImage img) {
            // Simulate complex image processing
            for (int y = 0; y < img.getHeight(); y++) {
                for (int x = 0; x < img.getWidth(); x++) {
                    int rgb = img.getRGB(x, y);
                    int filteredRgb = processPixel(rgb);
                    img.setRGB(x, y, filteredRgb);
                }
            }
        }
        
        private int processPixel(int rgb) {
            // Example: Simple grayscale conversion
            int r = (rgb >> 16) & 0xFF;
            int g = (rgb >> 8) & 0xFF;
            int b = rgb & 0xFF;
            
            int gray = (r + g + b) / 3;
            return (gray << 16) | (gray << 8) | gray;
        }
    }
}
                                        

2. Stock Market Data Aggregator

Industry Context

  • Used in financial trading platforms
  • Concurrent data retrieval
  • Real-time market analysis

Scenario Details

  • Fetch stock data from multiple sources
  • Parallel data collection
  • Aggregate market information

public class StockMarketDataAggregator {
    public static void collectStockData(List stockSymbols) {
        // Runnable Interface Approach
        ExecutorService executor = Executors.newFixedThreadPool(5);
        
        for (String symbol : stockSymbols) {
            executor.submit(new StockDataFetcher(symbol));
        }
        
        // Shutdown executor after tasks complete
        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    
    // Runnable implementation for stock data fetching
    static class StockDataFetcher implements Runnable {
        private String stockSymbol;
        
        public StockDataFetcher(String stockSymbol) {
            this.stockSymbol = stockSymbol;
        }
        
        @Override
        public void run() {
            try {
                StockData data = fetchStockData(stockSymbol);
                processStockData(data);
            } catch (Exception e) {
                System.err.println("Error fetching stock data: " + stockSymbol);
            }
        }
        
        private StockData fetchStockData(String symbol) {
            // Simulate API call
            return new StockData(
                symbol, 
                Math.random() * 100, 
                System.currentTimeMillis()
            );
        }
        
        private void processStockData(StockData data) {
            // Store or analyze stock data
            System.out.println("Processed: " + data);
        }
    }
    
    // Simple Stock Data Model
    static class StockData {
        private String symbol;
        private double price;
        private long timestamp;
        
        public StockData(String symbol, double price, long timestamp) {
            this.symbol = symbol;
            this.price = price;
            this.timestamp = timestamp;
        }
        
        @Override
        public String toString() {
            return "StockData{" +
                   "symbol='" + symbol + '\'' +
                   ", price=" + price +
                   ", timestamp=" + timestamp +
                   '}';
        }
    }
}
                                        

3. Log Processing System

Industry Context

  • Used in system monitoring
  • Distributed log analysis
  • Performance tracking

Scenario Details

  • Process logs from multiple sources
  • Lambda-based thread creation
  • Concurrent log parsing

public class LogProcessingSystem {
    public static void processLogs(List logFiles) {
        // Lambda-based Thread Creation
        ExecutorService executor = Executors.newFixedThreadPool(4);
        
        logFiles.forEach(logFile -> 
            executor.submit(() -> {
                try {
                    processLogFile(logFile);
                } catch (IOException e) {
                    System.err.println("Error processing log: " + logFile);
                }
            })
        );
        
        // Shutdown executor
        executor.shutdown();
        try {
            executor.awaitTermination(5, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    
    private static void processLogFile(Path logFile) throws IOException {
        // Read and analyze log file
        List lines = Files.readAllLines(logFile);
        
        long errorCount = lines.stream()
            .filter(line -> line.contains("ERROR"))
            .count();
        
        System.out.println(
            "Log: " + logFile.getFileName() + 
            ", Error Count: " + errorCount
        );
    }
}