<commit_before>package org.mac.sim.thread;

import java.util.concurrent.BlockingQueue;

import org.mac.sim.clock.Clock;
import org.mac.sim.domain.WorkerTask;
import org.mac.sim.global.PeriodConversionConstants;

/**
 * This class encapsulates the task of adding tasks to the queue at a specified
 * rate. It runs in it's own separate thread and adds a specified number of
 * tasks every interval, for a given number of intervals.
 * 
 * @author Mac-LP3
 *
 */
public class QueueManager extends Thread {

	private BlockingQueue<WorkerTask> queue;
	private Clock clock;
	private int ratePerPeriod;
	// private int totalPeriods;
	private boolean stop = true;
	private long periodsSpentInLoop = 0;
	private int addActions = 0;

	/**
	 * 
	 * @param queue
	 *            The queue to add tasks to.
	 * @param ratePerPeriod
	 *            The amount of tasks to add each period.
	 * @param clock
	 *            The clock tracking the passage of periods.
	 */
	public QueueManager(final BlockingQueue<WorkerTask> queue, final int ratePerPeriod, final Clock clock) {
		this.queue = queue;
		this.ratePerPeriod = ratePerPeriod;
		this.clock = clock;
	}

	/**
	 * Will continually watch the system clock until the next "period" has been
	 * reached. Will then update the queue with the given number of new
	 * arrivals.
	 */
	public void run() {

		this.stop = false;
		this.periodsSpentInLoop = 0;

		int j = 0;
		int tempTotalPeriods = clock.getTotalPeriods();
		long nanoTimePeriodLenth = PeriodConversionConstants.PERIOD_LENGTH_NANOS;
		long nextPeriod = 1;
		long periodToCheck = 0;
		long startTime = System.nanoTime();

		while (this.periodsSpentInLoop < tempTotalPeriods) {

			/*
			 * A period is defined by 10000000 nanoseconds. This number may be
			 * subject to change based on your CPU and the number of tasks to
			 * add each period. If the tests fail, try altering this number to
			 * better suit your hardware/model requirements.
			 */
			periodToCheck = Math.floorDiv((System.nanoTime() - startTime), nanoTimePeriodLenth);

			if (periodToCheck >= nextPeriod) {

				// Next period reached. Add required tasks to the list.
				for (j = 0; j < ratePerPeriod; j++) {
					queue.add(new WorkerTask(0));
					addActions++;
				}

				this.periodsSpentInLoop++;
				nextPeriod = periodToCheck + 1;
			}
		}

	}

	public long getPeriodsSpentInLoop() {
		return periodsSpentInLoop;
	}

	public int getAddActions() {
		return addActions;
	}

	public synchronized void doStop() {
		stop = true;
		this.interrupt(); // break pool thread out of dequeue() call.
	}

	public synchronized boolean isStopped() {
		return stop;
	}

}
<commit_msg>updated queueManager to rely on clock manager for time keeping<commit_after>package org.mac.sim.thread;

import java.util.concurrent.BlockingQueue;

import org.mac.sim.clock.Clock;
import org.mac.sim.domain.WorkerTask;

/**
 * This class encapsulates the task of adding tasks to the queue at a specified
 * rate. It runs in it's own separate thread and adds a specified number of
 * tasks every interval, for a given number of intervals.
 * 
 * @author Mac-LP3
 *
 */
public class QueueManager extends Thread {

	private volatile BlockingQueue<WorkerTask> queue;
	private volatile Clock clock;

	private int ratePerPeriod;
	private boolean stop = true;
	private long periodsSpentInLoop = 0;
	private int addActions = 0;

	/**
	 * 
	 * @param queue
	 *            The queue to add tasks to.
	 * @param ratePerPeriod
	 *            The amount of tasks to add each period.
	 * @param clock
	 *            The clock tracking the passage of periods.
	 */
	public QueueManager(final BlockingQueue<WorkerTask> queue, final int ratePerPeriod, final Clock clock) {
		this.queue = queue;
		this.ratePerPeriod = ratePerPeriod;
		this.clock = clock;
	}

	/**
	 * Will continually watch the system clock until the next "period" has been
	 * reached. Will then update the queue with the given number of new
	 * arrivals.
	 */
	public void run() {

		this.stop = false;
		this.periodsSpentInLoop = 0;
		int j = 0;
		int tempTotalPeriods = clock.getTotalPeriods();

		int currentPeriod = clock.getCurrentPeriod();
		while (currentPeriod < tempTotalPeriods) {

			// watch clock for a period change
			if (currentPeriod < clock.getCurrentPeriod()) {

				// Add the specified number of jobs to the queue
				for (j = 0; j < ratePerPeriod; j++) {
					queue.add(new WorkerTask(0));
					addActions++;
				}

				this.periodsSpentInLoop++;
				currentPeriod = clock.getCurrentPeriod();

			}
		}

	}

	public long getPeriodsSpentInLoop() {
		return periodsSpentInLoop;
	}

	public int getAddActions() {
		return addActions;
	}

	public synchronized void doStop() {
		stop = true;
		this.interrupt(); // break pool thread out of dequeue() call.
	}

	public synchronized boolean isStopped() {
		return stop;
	}

}

{'fraction_non_alphanumeric': 0.08831437435367115, 'fraction_numerical': 0.005170630816959669, 'mean_word_length': 3.283436669619132, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556831', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1405, 'n_words': 505}