Java代写:CST8277 Multithreading

代写多线程程序,解决生产者/消费者问题。

Purpose

After completing this assignment, you will have achieved the following:

  1. You will have modified a program to implement multithreading.
  2. You will have experience in implementing a solution to the Producer/Consumer problem.
  3. You will have experience in how to test this type of program.

Grading

Part 1a Code

  • Code compiles, has good indenting, not overly complicated, efficient.
  • Code demonstrates the concepts needed for the assignment: multithreaded producer-consumer.
  • Comments: every class and class member has comments, as well as a multiline comment block at the top of each file.

These should be Javadoc comments in your source code, minus the file header comment, there is no need to run the Javadoc tool; Your name must appear at the top of each source code file as well as on every class member or you will lose marks up to a score of zero for the coding part of the assignment.

Part 1b Screen shots of program execution

  • Take screen shots of your running program and database results.
    • Each screen shot must display your name, the date and time the program was run.
    • Place the screen shots into an MS Word document with figure text for submission.
    • Omitting your name will result in a loss of marks.

Part 2 Test Plan

  • This is a ‘paper-test-plan’ not JUnit testing (use MS-Word)
  • See the Appendix at the end of this handout for an example of the expected template;
  • Not following the template will result in loss of marks up to a score of zero for the test plan.

Part 3 Short Answer Questions

  • Answer the questions in section Part 3 below.

Program Setup

  • The program code you have been given reads records from a comma-separated-value text file (*.csv) and inserts the data into a MySQL database (MySQL 5.7). It currently does not use multithreading, and the records are inserted in the same order that they were read. (The file was created with fictional data).
  • Create a user account in MySQL named assignment1, with password ‘password’ and use the script file provided (fishsticks_create_tables.sql) to create the database.
  • Add the Connector-J jar file to your Eclipse project as an external jar.
  • Normally the Jar is located at C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.37-bin.jar (Adjust to match your version number).
  • The DataLoader.java file from the professor’s solution has also been provided to help with your work on this assignment.

Part 1 a,b Synchronizing Threads, Producer/Consumer

The goal of this part is for you to practice using the wait and notifyAll methods to synchronize threads. You will use similar techniques to the “Producer/Consumer Relationship with Synchronization” example in the Multithreading chapter of the Deitel textbook.

  1. It is recommended that you read the “Producer/Consumer Relationship with Synchronization” example in the Deitel textbook, and refer to the lecture slides on the Producer/Consumer problem. While reading the textbook think about “How do I implement a circular buffer?” and “How do I use the ArrayBlockingQueue?”

  2. Modify the provided code to use two additional threads of execution to implement the producer / consumer relationship. You may base this on textbook examples with attribution to the textbook authors. Suggested File Names to use:

    • BlockingQueueBuffer.java - Uses class ArrayBlockingQueue internally, implements interface Buffer
    • Buffer.java (interface) - An interface
    • FishStick.java - A data transfer object
    • FishStickCleaner.java - Resets the database table by truncating it to help with testing
    • CircularBuffer.java - Uses an array of 100 references to FishStick objects as buffer, implements Buffer
    • Consumer.java - Inserts records into the database, consumes from buffer
    • DataLoader.java - Sets up the objects, threads, outputs a time stamp
    • Launcher.java - Starts up the program
    • Producer.java - Reads records from dataset file, generates FishStick objects, produces into buffer
    • The records need to be inserted into the database in the same order they are read, with no duplicate records.
    • When the producer finishes reading records, it will need to let the consumer know when to stop consuming records, otherwise the consumer will get stuck waiting forever and the program will not shut down. (Tip: think about the producer placing a marker FishStick into the buffer that tells the consumer that this is the last FishStick).
    • You will need to create a CircularBuffer class using an Array of references to FishStick objects, as well as an alternative implementation that uses an ArrayBlockingQueue internally.
  3. Take screen shots of running your program using the CirclularBuffer and then the ArrayBlockingQueue. Make sure that the screen shots show your name and the date and time the program was run. (See sample figures below).
  4. Also take a screen shot of your database using the mysql command line tool as shown in Figure 4.0 below

Part 2 Testing

The goal of a test plan is to protect the programmer from defects in the code, or being added to the code by any programmer during the lifecycle of the program. A test case is like a sensor with an alarm that alerts you to a problem. Imagine that one of your colleagues has worked on your program while you were on vacation, and they have decided to play a trick on you by making something wrong. When you come back from vacation and execute your test plan, one of your tests should fail and alert you to the problem. No matter what trick your colleague tries to play on you, your test plan should protect you. Also remember that a test plan is usually executed by a tester who probably doesn’t know much about the workings of the program. The tester follows the instructions and descriptions of the test plan, and should not need to know anything about the program – they get that information from the test plan!

  1. Create a test plan in the form of a table that includes the possible different situations that you need to check for correct behavior.
  2. To make running your test cases easier, you can adjust your program code to provide output messages on screen or better yet write to a log file.

Part 3 Short Answer Questions

  1. Which implementation of the Buffer was easier to code, the circular buffer using an array and keywords synchronized with methods wait() and notifyAll() or the buffer using the ArrayBlockingQueue?
  2. Ease of implementation aside, what are some advantages and disadvantages of each approach? (Discuss)

Submission Requirements

  • Place the following items into a single zip archive, including your name as part of the zip file name.
    • A folder containing your source code files, and the data set file
    • An MS Word document that contains your screen shots as figures, your test plan, as well as your answers to the Part 3 questions.
      • Make sure your name appears within the word document either on a cover page or on the first line of the first page.
  • Remember, if your name is omitted from screen shots, code comments, or your MS-Word document you can lose marks up to getting a zero for the Assignment.