Big Java Lab 23

Running Threads

1.

Write a program to simulate a simple traffic light at an intersection of two roads.

It will be easiest to view the example as four one-way roads, since cars can approach and pass through the intersection from four directions. Assign each road a number from 1 to 4.

Each time a car is at the intersection, request that the traffic light turn green. Implement a traffic light object with a turnGreen(int roadNumber) method. When a car is at the intersection it will call this method and pass its number. If road number 1 calls the turnGreen method, the method should print out:

Light turned green by road 1

Waiting for road 1 car to clear intersection

10 9 8 7 6 5 4 3 2 1 0

Use a loop to print a countdown of the waiting period from 10 to 0.

What is the code of your TrafficLight class.



2.

A road is a simple queue. The first car in the road will call turnGreen and be removed. After waiting a short amount of time (for example, 5 milliseconds), the next car in the road will call turnGreen, until the road is empty.

What is the code of your RoadRunnable class? Part of the class code has been provided for you:

public class RoadRunnable implements Runnable
{
    public RoadRunnable(int roadNumber, TrafficLight aLight) { . . . }
    public void add(String car) { . . . }
    public void run() { . . . }

    private LinkedList<String> queue;
    private int number;
    private TrafficLight light;
}


3.

Fill the roads with 100 cars each. Then, create a thread for each of the four roads and start the four threads. Run the simulation.

What is the code of your tester class?


4.

Describe the output. Do any of the cars collide (i.e., are they in the intersection at the same time)?

Synchronizing Object Access

5.

Modify your TrafficLight class. Synchronize the access to its turnGreen method by using a lock.

What is the code of your modified TrafficLight class?


6.

Describe the output. Do any of the cars collide (i.e., are they in the intersection at the same time)?

Conditions

7.

Create a program that simulates a store's inventory. A store carries varying quantities of five different items (your choice of items and amounts).

Five customers repeatedly generate random requests for multiple items and ask the store to fill the requests. If the store has enough items in inventory, it will fill the request, and subtract the quantities of the request from the inventory. If the store does not have enough items in inventory, the customer must wait for more inventory to arrive. A customer should sleep for 50 milliseconds before making a new request.

A store manager is in charge of ordering more items. The store manager will check to see if any of the items in inventory are depleted. If they are, he will order more items. However, the store manager has other things to do, so he doesn't constantly check the amounts in the inventory. He sleeps in between checks of the inventory.

Create a program to run this simulation. Make sure all appropriate methods are synchronized. Sample output from the program may look like:


. . .
Store inventory
---------------
Apples: 44, Oranges: 29, Pineapples: 9, Persimmons: 43, Kumquats: 27
---------------

Customer 4 requests, 1 Apple, 3 Oranges, 9 Pineapples, 0 Persimmons, 5 Kumquats
Request fulfilled

---------------
Store inventory
---------------
Apples: 43, Oranges: 26, Pineapples: 0, Persimmons: 43, Kumquats: 22
---------------

Customer 0 requests, 4 Apples, 0 Oranges, 2 Pineapples, 1 Persimmon, 3 Kumquats
Customer 0 waiting . . .

Customer 1 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats
Customer 1 waiting . . .

Customer 2 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats
Customer 2 waiting . . .

Customer 3 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats
Customer 3 waiting . . .

** RESTOCKING **

---------------
Store inventory
---------------
Apples: 50, Oranges: 50, Pineapples: 50, Persimmons: 50, Kumquats: 50
---------------
Customer 0 requests, 4 Apples, 0 Oranges, 2 Pineapples, 1 Persimmon, 3 Kumquats
Request fulfilled

. . .


What is the code of your customer class?


8.

What is the code of your Store class?


9.

What is the code of your manager class?


10.

What is the code of your tester class? Also include the code of your helper classes, if any.

Animation

11.

Create a program that uses threads to display bouncing balls in a panel.

Create a Ball class. A Ball is colored a random color. A Ball should be able to move relative to the panel it is in, and draw itself in the panel. To bounce a ball, choose the distance the ball will move in the x and y directions. These values can be represented as dx and dy. When a ball moves, dx and dy are added to the ball's x and y positions. For your program, dx and dy should both be greater than 0. You may choose the dx and dy to be equal to have the ball move at a constant slope of either plus or minus 1. When a ball reaches the edge of the panel it is in, you must reverse its direction by changing the sign of dx and/or dy.

The program should have an "Add Ball" button. When the "Add Ball" button is pressed a new thread is created that moves and displays a new ball. Use the sleep() method to control the speed of the ball.

What is the code of your Ball class?


12.

What is the code of your component class?


13.

What is the code of your panel and/or frame class(es)?


14.

What is the code of your viewer class?