Assignment 7
Managing Concurrency

Assignment

Due at the beginning of class on Wednesday, October 29, and Friday, October 31.

Preparation

Understand threads and concurrency.

To Do

You are to design and write a program to solve this very simplified stock market problem.  A stock market firm is allowing brokers to buy and sell stocks in one company. Stock trading begins at a certain time of the day and ends an another time (not important to the problem).  Here are the issues:

  1. The program has a graphical user interface that is used by a single representative of the stock market term.  This interface has four input possibilities:
    1. a field for entering the number of stocks on the market for the day.
    2. a field for inputting the number of brokers registered with the firm (this could change daily as new brokers are registered with the stock market firm and others back out).
    3. a filed for setting the stock price; this becomes the buying price. The selling price is always set at 1.1 times the buying price.
    4. a button to start trading
    5. a button to change the buying price.
    6. a button to stop trading
  2. Once all broker threads are started, they work this way:
    1. each runs in a loop that only exits when a global variable "endOfTrading" is true
    2. inside the loop, the thread waits on a randomly generated number that simulates waiting for a customer to place a buy or sell order for stock.
    3. once a customer places an order, another random number, either 0 or 1, is generated, with 0 representing "buy" and  1 representing "sell."  Finally, a third random number is generated between 1 and 100 that indicates how many stocks are to be bought or sold for this transaction.
    4. the thread then executes code that changes the number of stocks by the correct amount (either down or up, for buy or sell, respectively) and updates the total dollar value of the stocks bought and the stocks sold to date.
  3. When the representative clicks  the "change buying price" button, a single thread belonging to the representative must change the selling price of the stock (and hence the buying price at 1.1 times that selling price).
  4. When the representative clicks the stop button and all threads have stopped, the code is to generate the number of stocks the company now has, the dollar value of the stocks bought, the dollar value of the stocks sold, and the day's revenues as the difference between these two values.

You are to solve the concurrency issues using only semaphores.

To Turn In on Wednesday, October 29

Turn in this information:

  1. A clear description of the synchronization issues that need to be solved.
  2. A discussion of what kind of concurrency paradigm this represents, if any, from the list in the book
  3. A possible approach for solving the problem

To Turn In on Friday, November 1

Your Java program in a form ready to demonstrate.

Special Note

You may work in teams of two on this program.