Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. MAIN.JAVA
  2.  
  3.  
  4. package org.suai;
  5.  
  6. import java.util.*;
  7. public class Main {
  8. public static void main(String args[]) throws Exception {
  9. Scanner in = new Scanner(System.in);
  10. System.out.print("Input number of computers: ");
  11. int com = in.nextInt();
  12. System.out.print("Input number of tourists: ");
  13. int tour = in.nextInt();
  14. Journal today = new Journal(com, tour);
  15. }
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. JOURNAL.JAVA
  29.  
  30. package org.suai;
  31.  
  32. import java.util.concurrent.ExecutorService;
  33. import java.util.concurrent.Executors;
  34. import java.util.concurrent.TimeUnit;
  35. import java.util.*;
  36. public class Journal {
  37. private int tourists;
  38. private int computers;
  39.  
  40. class Tourist implements Runnable {
  41.  
  42. private int man;
  43.  
  44. public Tourist (int nam) {
  45. man = nam;
  46. new Thread();
  47. }
  48.  
  49. @Override
  50. public void run() {
  51. try {
  52. System.out.println("Tourist " + man + " is online");
  53.  
  54. Random random = new Random();
  55. int time = 900 + random.nextInt(6300);
  56.  
  57. Thread.sleep(time/10);
  58. System.out.println("Tourist " + man + " is done, having spent " + time/60 + " minutes online");
  59. } catch (InterruptedException e) {}
  60. }
  61. }
  62.  
  63. public Journal (int com, int tour) {
  64. tourists = tour;
  65. computers = com;
  66. ExecutorService service = Executors.newFixedThreadPool(computers);
  67. for (int i = 1; i <= tourists; i++) {
  68. service.execute(new Tourist(i));
  69. }
  70. service.shutdown();
  71. try {
  72. service.awaitTermination(10, TimeUnit.SECONDS);
  73. }
  74. catch (InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. System.out.println("The place is empty, let's close up and go to the beach!");
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement