Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAIN.JAVA
- package org.suai;
- import java.util.*;
- public class Main {
- public static void main(String args[]) throws Exception {
- Scanner in = new Scanner(System.in);
- System.out.print("Input number of computers: ");
- int com = in.nextInt();
- System.out.print("Input number of tourists: ");
- int tour = in.nextInt();
- Journal today = new Journal(com, tour);
- }
- }
- JOURNAL.JAVA
- package org.suai;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- import java.util.*;
- public class Journal {
- private int tourists;
- private int computers;
- class Tourist implements Runnable {
- private int man;
- public Tourist (int nam) {
- man = nam;
- new Thread();
- }
- @Override
- public void run() {
- try {
- System.out.println("Tourist " + man + " is online");
- Random random = new Random();
- int time = 900 + random.nextInt(6300);
- Thread.sleep(time/10);
- System.out.println("Tourist " + man + " is done, having spent " + time/60 + " minutes online");
- } catch (InterruptedException e) {}
- }
- }
- public Journal (int com, int tour) {
- tourists = tour;
- computers = com;
- ExecutorService service = Executors.newFixedThreadPool(computers);
- for (int i = 1; i <= tourists; i++) {
- service.execute(new Tourist(i));
- }
- service.shutdown();
- try {
- service.awaitTermination(10, TimeUnit.SECONDS);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("The place is empty, let's close up and go to the beach!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement