Simulator.java

43 lines | 1.186 kB Blame History Raw Download
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrgs.inf.prosoft.requestssimulator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author romulo
 */
public class Simulator {

    private static final Logger logger = Logger.getLogger(Simulator.class.getName());

    public static final void simulate(Profile profile, long time) {
        simulate(profile, time, 1);
    }

    public static final void simulate(Profile profile, long time, int users) {
        Collection<Thread> threads = new ArrayList<>();
        for (int i = 0; i < users; i++) {
            Thread thread = new Thread(() -> {
                profile.run(time);
            });
            threads.add(thread);
            thread.start();
        }
        threads.forEach((thread) -> {
            try {
                thread.join();
            } catch (InterruptedException ex) {
                Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }
}