Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Java-wait-test

shchuko May 22nd, 2020 (edited) 1,389 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Queue;
  7.  
  8. public class Main {
  9.     private final List<Thread> threadList = new ArrayList<>();
  10.     private final Queue<String> queue = new LinkedList<>();
  11.  
  12.     Main() throws InterruptedException {
  13.         for (int i = 0; i < 3; ++i) {
  14.             threadList.add(new Thread(new MyRunnable(i)));
  15.         }
  16.  
  17.         System.out.println(System.currentTimeMillis() + " Threads created:");
  18.         threadList.forEach(th -> System.out.println(th.getState()));
  19.         System.out.println("This thread: " + Thread.currentThread().getState());
  20.         synchronized (queue) {
  21.             System.out.println(System.currentTimeMillis() + " queue accessible");
  22.         }
  23.  
  24.         Thread.sleep(1000);
  25.         threadList.forEach(Thread::start);
  26.  
  27.         Thread.sleep(1000);
  28.         System.out.println(System.currentTimeMillis() + " Threads started:");
  29.         threadList.forEach(th -> System.out.println(th.getState()));
  30.         System.out.println("This thread: " + Thread.currentThread().getState());
  31.         synchronized (queue) {
  32.             System.out.println(System.currentTimeMillis() + " queue accessible");
  33.         }
  34.  
  35.         Thread.sleep(1000);
  36.         System.out.println(System.currentTimeMillis() + " Interrupting threads:");
  37.         synchronized (queue) {
  38.             queue.clear();
  39.             threadList.forEach(Thread::interrupt);
  40.             System.out.println(System.currentTimeMillis() + " queue accessible");
  41.         }
  42.  
  43.         Thread.sleep(1000);
  44.         System.out.println(System.currentTimeMillis() + " Threads interrupted:");
  45.         threadList.forEach(th -> System.out.println(th.getState()));
  46.         System.out.println("This thread: " + Thread.currentThread().getState());
  47.         synchronized (queue) {
  48.             System.out.println(System.currentTimeMillis() + " queue accessible");
  49.         }
  50.  
  51.     }
  52.  
  53.     public static void main(String[] args) throws InterruptedException {
  54.         new Main();
  55.     }
  56.  
  57.     private class MyRunnable implements Runnable {
  58.         private final int runnableNo;
  59.  
  60.         MyRunnable(int runnableNo) {
  61.             this.runnableNo = runnableNo;
  62.         }
  63.  
  64.         @Override
  65.         public void run() {
  66.             while (!Thread.currentThread().isInterrupted()) {
  67.                 String task;
  68.                 synchronized (queue) {
  69.                     while (queue.isEmpty()) {
  70.                         try {
  71.                             System.err.println(System.currentTimeMillis() + " Before wait #" + runnableNo);
  72.                             queue.wait();
  73.                         } catch (InterruptedException e) {
  74.                             System.err.println( System.currentTimeMillis() + " Interrupted #" + runnableNo);
  75.                             return;
  76.                         }
  77.                     }
  78.  
  79.                     task = queue.poll();
  80.                     if (!queue.isEmpty()) {
  81.                         queue.notify();
  82.                     } else {
  83.                         queue.notifyAll();
  84.                     }
  85.                 }
  86.  
  87.                 runTask(task);
  88.             }
  89.         }
  90.  
  91.         private void runTask(String s) {
  92.             System.err.println(System.currentTimeMillis() + " Task " + s + " solved by #" + runnableNo);
  93.         }
  94.     }
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top