How to do it...

Follow these steps to implement the example:

  1. Create a class named ProducerConsumerTest that extends the MultithreadedTestCase class:
        public class ProducerConsumerTest extends MultithreadedTestCase {
  1. Declare a private LinkedTransferQueue attribute parameterized by the String class named queue:
        private LinkedTransferQueue<String> queue;
  1. Implement the initialize() method. This method won't receive any parameters and will return no value. It will call the initialize() method of its parent class and then initialize the queue attribute:
        @Override 
public void initialize() {
super.initialize();
queue=new LinkedTransferQueue<String>();
System.out.printf("Test: The test has been initialized ");
}
  1. Implement the thread1() method. It will implement the logic of the first consumer. Call the take() method of the queue and then write the returned value in the console:
        public void thread1() throws InterruptedException { 
String ret=queue.take();
System.out.printf("Thread 1: %s ",ret);
}
  1. Implement the thread2() method. It will implement the logic of the second consumer. First wait until the first thread has slept in the take() method. To put the thread to sleep, use the waitForTick() method. Then, call the take() method of the queue and write the returned value in the console:
        public void thread2() throws InterruptedException { 
waitForTick(1);
String ret=queue.take();
System.out.printf("Thread 2: %s ",ret);
}
  1. Implement the thread3() method. It will implement the logic of a producer.
    First wait until the two consumers are blocked in the take() method; block this method using the waitForTick() method twice. Then, call the put() method of the queue to insert two strings in the queue:
         public void thread3() { 
waitForTick(1);
waitForTick(2);
queue.put("Event 1");
queue.put("Event 2");
System.out.printf("Thread 3: Inserted two elements ");
}
  1. Finally, implement the finish() method. Write a message in the console to indicate that the test has finished its execution. Check that the two events have been consumed (so the size of the queue is 0) using the assertEquals() method:
        public void finish() { 
super.finish();
System.out.printf("Test: End ");
assertEquals(true, queue.size()==0);
System.out.printf("Test: Result: The queue is empty ");
}
  1. Next, implement the main class of the example by creating a class named Main with a main() method:
        public class Main { 
public static void main(String[] args) throws Throwable {
  1. Create a ProducerConsumerTest object named test:
        ProducerConsumerTest test=new ProducerConsumerTest();
  1. Execute the test using the runOnce() method of the TestFramework class:
        System.out.printf("Main: Starting the test
"); 
TestFramework.runOnce(test);
System.out.printf("Main: The test has finished ");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.52.7