Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cp;
- import static org.junit.Assert.assertTrue;
- import java.util.Random;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import org.junit.After;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import nl.utwente.pp.cp.junit.ConcurrentRunner;
- import nl.utwente.pp.cp.junit.ThreadNumber;
- import nl.utwente.pp.cp.junit.Threaded;
- @RunWith(ConcurrentRunner.class)
- public class QueueTest {
- /**
- * The amount of threads used in each test.
- */
- private static final int AMOUNT_OF_THREADS = 100;
- /**
- * Simple random object to get random integers from.
- */
- private final Random random = new Random();
- /**
- * Queue used for the producer consumer example (advancedMultiThreadedTest).
- */
- private Queue<Long> queue;
- /**
- * If you need to setup some object before the multithreaded tests start, you can do it in a method annotated with
- * {@link Before}. This methods are always executed single threaded.
- */
- @Before
- public void before() {
- //Setup an empty queue.
- this.queue = new NameQueue<>();
- }
- @Test
- @Threaded(count = AMOUNT_OF_THREADS)
- public void testThread() {
- long myId = Thread.currentThread().getId();
- producer(myId);
- consumer();
- }
- public void producer(long myId) {
- queue.push(myId);
- }
- public void consumer() {
- try {
- System.out.println(queue.pull());
- } catch (QueueEmptyException e) {
- assertTrue(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment