Guest User

Untitled

a guest
May 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package cp;
  2.  
  3. import static org.junit.Assert.assertTrue;
  4.  
  5. import java.util.Random;
  6. import java.util.concurrent.ConcurrentLinkedQueue;
  7.  
  8. import org.junit.After;
  9. import org.junit.Assert;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13.  
  14. import nl.utwente.pp.cp.junit.ConcurrentRunner;
  15. import nl.utwente.pp.cp.junit.ThreadNumber;
  16. import nl.utwente.pp.cp.junit.Threaded;
  17.  
  18. @RunWith(ConcurrentRunner.class)
  19. public class QueueTest {
  20.  
  21. /**
  22. * The amount of threads used in each test.
  23. */
  24. private static final int AMOUNT_OF_THREADS = 100;
  25.  
  26. /**
  27. * Simple random object to get random integers from.
  28. */
  29. private final Random random = new Random();
  30.  
  31. /**
  32. * Queue used for the producer consumer example (advancedMultiThreadedTest).
  33. */
  34. private Queue<Long> queue;
  35.  
  36. /**
  37. * If you need to setup some object before the multithreaded tests start, you can do it in a method annotated with
  38. * {@link Before}. This methods are always executed single threaded.
  39. */
  40. @Before
  41. public void before() {
  42. //Setup an empty queue.
  43. this.queue = new NameQueue<>();
  44. }
  45.  
  46. @Test
  47. @Threaded(count = AMOUNT_OF_THREADS)
  48. public void testThread() {
  49. long myId = Thread.currentThread().getId();
  50. producer(myId);
  51. consumer();
  52.  
  53.  
  54. }
  55.  
  56. public void producer(long myId) {
  57. queue.push(myId);
  58. }
  59.  
  60. public void consumer() {
  61. try {
  62. System.out.println(queue.pull());
  63. } catch (QueueEmptyException e) {
  64. assertTrue(false);
  65. }
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment