Advertisement
Guest User

Untitled

a guest
May 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. public static void main(String[] args) throws Exception {
  2.  
  3.         List<String> urls = new ArrayList<>();
  4.         urls.add("1");
  5.         urls.add("2");
  6.         urls.add("3");
  7.         urls.add("4");
  8.         urls.add("5");
  9.         urls.add("6");
  10.         urls.add("7");
  11.         urls.add("8");
  12.         urls.add("9");
  13.         urls.add("10");
  14.  
  15.         urls.stream().map(url -> new Thread(() -> {
  16.             while (true) {
  17.                 if (isCanBuy()) {
  18.                     Steam.buy(url, 1, 1d);
  19.                     break;
  20.                 }
  21.             }
  22.         })).peek(Thread::start).collect(Collectors.toList());
  23.  
  24.     }
  25.  
  26.     public static boolean isCanBuy() {
  27.         boolean b = new Random().nextBoolean();
  28.         return b;
  29.     }
  30.  
  31.     static class Steam {
  32.  
  33.         public static synchronized void buy(String url, Integer count, Double price) {
  34.             System.err.println("Buy:" + url);
  35.             try {
  36.                 Thread.sleep(1_000L);
  37.             } catch (InterruptedException e) {
  38.                 e.printStackTrace();
  39.             }
  40.         }
  41.  
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement