Advertisement
Kostiggig

Untitled

May 4th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package multithreading.thread_pool;
  2.  
  3. public class Api {
  4.  
  5.     void makeRequest() throws InterruptedException {
  6.         Thread.sleep(3000); // simulate io blocking
  7.     }
  8. }
  9.  
  10. package multithreading.thread_pool;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.concurrent.ArrayBlockingQueue;
  15. import java.util.concurrent.BlockingQueue;
  16.  
  17. public class MyThreadPool {
  18.  
  19.     private final BlockingQueue<Runnable> tasks;
  20.     private volatile boolean isStopped = false;
  21.     private final List<Thread> threadsInPoll;
  22.  
  23.     public MyThreadPool(int countOfThreads, int countOfTasks) {
  24.         tasks = new ArrayBlockingQueue<>(countOfTasks);
  25.         threadsInPoll = new ArrayList<>(countOfThreads);
  26.         for (int i = 0; i < countOfThreads; i++) {
  27.             Thread thread = new Thread(() -> {
  28.                 while(true) {
  29.                     try {
  30.                         getTask().run();
  31.                     } catch (InterruptedException e) {
  32.                         System.out.println("Interruption... tasks left " + tasks.size());
  33.                         break;
  34.                     }
  35.                 }
  36.             }, "Thread " + i);
  37.             threadsInPoll.add(thread);
  38.         }
  39.  
  40.         threadsInPoll.forEach(Thread::start);
  41.     }
  42.  
  43.     public void execute(Runnable runnable) throws InterruptedException {
  44.         if (!isStopped) {
  45.             tasks.put(runnable);
  46.         }
  47.     }
  48.  
  49.     public void stop() {
  50.         synchronized (this) {
  51.             if (isStopped) return;
  52.             isStopped = true;
  53.             threadsInPoll.forEach(Thread::interrupt);
  54.         }
  55.     }
  56.  
  57.     private Runnable getTask() throws InterruptedException {
  58.         if (!isStopped) {
  59.             return tasks.take();
  60.         }
  61.         throw new InterruptedException();
  62.     }
  63. }
  64.  
  65. Client:
  66. package multithreading.thread_pool;
  67.  
  68. public class Client {
  69.  
  70.     private static Api api;
  71.  
  72.     public static void main(String[] args) throws InterruptedException {
  73.         int countOfThreads = 5;
  74.         int countOfTasks = countOfThreads * 3;
  75.  
  76.         MyThreadPool threadPool = new MyThreadPool(countOfThreads, countOfTasks);
  77.  
  78.         api = new Api();
  79.  
  80.         for (int i = 0; i < 15; i++) {
  81.             int finalI = i;
  82.             threadPool.execute(() -> {
  83.                 System.out.println("Thread " + Thread.currentThread().getName() + " makes network request number " + finalI);
  84.                 makeRequest();
  85.                 System.out.println("Thread " + Thread.currentThread().getName() + " fetched response for the network request number " + finalI);
  86.             });
  87.         }
  88.  
  89.         Thread.sleep(3500);
  90.         threadPool.stop();
  91.     }
  92.  
  93.     private static void makeRequest() {
  94.         try {
  95.             api.makeRequest();
  96.         } catch (InterruptedException e) {
  97.             System.out.println("Interruption while making a request has occurred");
  98.         }
  99.     }
  100. }
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement