package performance; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import collection.Stack; public class Task extends Thread { private final Stack stack; private final double percentage; private final CyclicBarrier barrier; private int total; private long operations; private long end; private int random; public Task(Stack stack, double percentage, CyclicBarrier barrier, int random) { super(); this.stack = stack; this.percentage = percentage; this.barrier = barrier; this.total = 0; this.operations = 0; this.random = random; } private void nextInt() { this.random ^= (this.random << 6); this.random ^= (this.random >>> 21); this.random ^= (this.random << 7); } public void run() { try { this.barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } final long start = System.nanoTime(); Main.start.compareAndSet(null, start); while (true) { this.nextInt(); double choice = (this.random % 100) / 100.0; if (choice < percentage) { try { this.total += this.stack.pop(); } catch (Exception e) { // do nothing } } else { this.nextInt(); int data = this.random; this.stack.push(data); } this.operations++; long time = System.nanoTime(); if (time - Main.start.get() > Main.NANO_PER_SECOND) { return; } else { this.end = time; } } } public int getTotal() { return this.total; } public long getOperations() { return this.operations; } public long getEnd() { return this.end; } }