Write a program in Java. You should use, when necessary, the following methods to synchronize all threads:
Question:
Write a program in Java. You should use, when necessary, the following methods to synchronize all threads: run(), start(), currentThread(), getName(), join(), yield(), |
sleep(random time), isAlive(), getPriority(), setPriority(), interrupt(). The use of semaphores to synchronize threads is strictly DISALLOWED. Additionally, you are NOT PERMITTED to use any of the following: wait(), notify(), notifyAll(), the synchronized keyword (for methods or blocks), and any synchronized collections or synchronization tools that were not discussed in class or mentioned here. You CAN, however, use the modifier volatile and the AtomicInteger and AtomicBoolean classes if you choose to. Directions: Synchronize the assembly-line, manufacturer, and truck type of threads in the context of the problem described below. Thread types: assembly-line (1 thread) manufacturer (4 threads) truck (4 threads) Simulate a bunny assembly line using Java-threads. A bunny assembly line assembles bunniesâ?? parts that are supplied by part-manufacturers. The possible parts are legs, torsos, heads and tails. A bunny is completed when it has 4 legs, 1 torso, 1 head and 1tail. Each manufacturer produces only one type of parts. There are four manufacturers. Use sleep of a random interval of time to simulate production. During this time a random number of parts have been produced (for heads, tails, torsos consider a random number between 70 and 100, for legs consider a random number between 250 and 500). The manufacturer uses a truck to deliver the parts to the assembly line. Loading the truck will take a brief time (simulated by sleep of random time). The truck busy waits during the loading time. On its way to delivery, the truck speeds up. Use getPriority(), setPriority(), and sleep(random time); after the truck has increased its priority, it will sleep for a random time. As soon as it wakes up make sure you reset its priority back to its normal value). Once arrived at the assembly line the truck will wait (simulated by a sleep of long time) on line until the assembly line will be ready to process its delivery. It will interrupt the first truck in line (in the body of the catch have a message that will show that the specific truck has been interrupted). Once the delivery is accepted (use messages that will show how many parts and of what type have been delivered), the truck decides to take a break and get gas. (simulated by a yield() followed by a sleep of random time). Next it goes back to its manufacturer (simulated by sleep of random time) for a new load. Each truck will do num_delivery/day. After each truck does all its deliveries for the day, they will busy wait for a report from the assembly line. The assembly line will issue an invoice with the total of complete bunnies assembled in that day and also the surplus of parts. The truck who delivered the largest surplus will be allowed to leave first. The other trucks will terminate in sequence. The order of sequence is from the truck with the highest surplus to the one with the lowest surplus. (use join() and isAlive() to achieve a proper sequence). After all manufacturers are closed for the day, the assembly line closes (terminates) too. Add the following lines to all the threads you instantiate: public static long time = System.currentTimeMillis(); public void msg(String m) { System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m); } It is recommended that you initialize the time at the beginning of the main method, so that it is unique to all threads. There should be output messages that describe how the threads are executing. Whenever you want to print something from a thread use: msg("some message about what action is simulated"); // Default constructor setName("RandomThread-" + id); } Design an OOP program. All thread-related tasks must be specified in its respective classes, no class body should be empty.
|
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch