1. Create a console stock exchange simulation using the Java programming language.
The application should have a Trader class with traders ranging from 2 to 20 and one StockExchange class. The individual traders and the exchange are both separate threads, but all of the threads work concurrently within the program. At the start of the StockExchange (the main thread), all of the Traders are created and, when the main thread finishes, the Trader threads are terminated safely.
The StockExchange manages a book (i.e., a thread-safe queue) that is shared between traders, where traders publish their offers. The book only records the integer amount of stock that a trader wishes to sell on the StockExchange. After each trade completed, each trader randomly sleeps from 100 to 200 ms, then awakens to generate a random value between 1 and 10,000 (the amount of stock they want to sell). This random value is then published on the Stock Exchange with this process continually repeating until the Stock Exchange closes. The Stock Exchange closes when the user presses any key on their keyboard.
After a key is pressed the StockExchange sends a terminating signal to each individual Trader thread and subsequently waits for all of the Trader threads to safely terminate prior to closing the program.
Hint: use ArrayBlockingQueue and ThreadLocalRandom libraries from the java.util.concurrent package to help with this problem.
2. Now create a Stock Exchange logging service for the Java application.
There should be new functionality added so that every transaction on the Stock Exchange is logged with a timestamp and the stock traded. So, every transaction should be recorded and written to a text file i.e., a transaction.txt file.
The output from the transaction.txt file should look like the following:
1817693103 – 12 stock
1817693346 – 59 stock
1817694512 – 11 stock
1817694517 – 85 stock
Hint: the functionality required for the logging service should be made by altering the Stock Exchange class and NOT the Trader class.
3. Finally create a Traders logging service for the Java application.
The program should now log every transaction that an individual trader has made on the StockExchange. All of the transactions should be logged on a separate line in one trades.txt file. All of the active traders in an execution of the StockExchange program should append all of their transactions to this text file. Similar to the transaction.txt, each transaction in the trades.txt file must have a timestamp, unique Trader ID and the stock value that the Trader class has randomly generated. An example of how the trades.txt file is as below:
1817693103 – Trader 2 – 12 stock
1817693346 – Trader 4 – 59 stock
1817694512 – Trader 7 – 11 stock
1817694517 – Trader 2 – 85 stock
Hint: the functionality should be achieved by modifying the Trader class and NOT the StockExchange class.