Threads program

There are three parts of the Producer / Consumer example that you will need to modify:

  • Producer - you will need a producer that will place items on the queue. Your main routine will start the Producer and the Producer will be responsible for reading commands from standard input. The commands will be:
    • read a list of commands from a file
    • exit (and wait for all running threads to complete)
    • kill all and exit
  • The Queue - you will need a queue that will be able to hold several commands at a time so that they can be taken off by the consumer thread one at a time. The Queue in the example in the book is only capable of holding one item so it won't do for this assignment. You should use a List as your queue.

See: http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html be sure to read the documentation completely. Your queue will also have to be synchronized. The documentation shows how to do this in this code example:

List list = Collections.synchronizedList(new LinkedList(...));
  • The final part is the Consumer. Your consumer will be responsible for taking items off the queue and then executing the command in the item (which can be a String if you wish) in a SEPARATE THREAD. There are several types of threads that can be created:
    • count up - give a command of the form "up max sleep time file" that will count from zero to max delaying sleep time milliseconds on every iteration. The output from this thread will be written to a file named file
    • count down - give a command of the form "down max sleep time file" that will count from max to zero delaying sleep time milliseconds on every iteration. The output from this thread will be written to a file named file
    • create a multiplication table - give a command of the form "table max sleep time file" that will create a multiplication table. The first row will have the numbers 1 - 12. The first column of each row will have the iteration count and the rest of the row will be the iteration count times the number in the first row. Each element in the table should be printed right justified in a field 10 characters wide (see the printf command) For example:
1 2 3 4 5 6 7 8 9 10 11 12
1 1 2 3 4 5 6 7 8 9 10 11 12
2 2 4 6 8 10 12 14 16 18 20 22 24
etc.

The output from all threads will be written to a file named . This will allow you to test that your synchronization is working correctly so that the output from independent threads does not get interspersed into the file.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.