Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.}
1.Java Concept Matching.
Match each of the following Thread concepts.
blocked
prevented from running when it is waiting for some kind of I/O operation to complete
ready
waiting for the CPU to become available.
priority scheduling
scheduling threads according to relative priority. Higher priority threads get the CPU first
round-robin scheduling
threads of the same priority take turns using the CPU with each thread getting a fixed amount (quantum) of time.
producer
a thread that produces a resource and acts like a source
consumer
a thread that uses a resource and acts like a sink
monitor
a mechanism that guarantees mutually exclusive access to a synchronized method among a group of cooperating threads.
lock
a mechanism to prevent other threads from using an objects synchronized methods
concurrent
having multiple threads sharing a CPU over a period of time
time slicing
a method of sharing the cpu between threads by giving each thread a small amount of the CPU’s time.
busy waiting
when the CPU is in a trivial loop for a fixed amount of time to delay a thread’s progress
nonbusy waiting
a thread gives up the CPU to another thread while waiting for an event or condition to occur.
mutual exclusion
among a group of threads, one and only one thread has access to a certain resource at a time.
critical section
a part of code that should be executed in a mutually exclusive manner by a group of threads.
Fill in the Blanks.
Fill in the blanks.
2..
happens when a CPU’s time is divided among several different threads.
3..
A method that should not be interrupted during its execution is known as a .
4..
The scheduling algorithm in which each thread gets an equal portion of the CPU’s time is known as .
5..
The scheduling algorithm in which some threads can preempt other threads is known as .
6..
A is a mechanism that enforces mutually exclusive access to a synchronized method.
7..
A thread that performs an I/O operation may be forced into the state until the operation is completed.
8.Describe Time Slicing.
Describe the concept of time slicing as it applies to CPU scheduling.
9.Single vs. Multiple Processors.
What’s the difference in the way concurrent threads would be implemented on a computer with several processors and on a computer with a single processor?
10.I/O Operation Blocking.
Why are threads put into the blocked state when they perform an I/O operation?
11.Sleep vs. Ready.
What’s the difference between a thread in the sleep state and a thread in the ready state?
12.Deadlock.
Deadlock is a situation that occurs when one thread is holding a resource that another thread is waiting for, while the other thread is holding a resource that the first thread is waiting for. Describe how deadlock can occur at a four-way intersection with cars entering from each branch. How can it be avoided?
13.Starvation.
Starvation can occur if one thread is repeatedly preempted by other threads. Describe how starvation can occur at a four-way intersection and how it can be avoided.
14.Implement Random Number Runnable.
Use the Runnable interface to define a thread that repeatedly generates random numbers in the interval 2 through 12.
15.Two Clerk Bakery.
Create a version of the Bakery program that uses two clerks to serve customers.
16.Priority Numbers.
Modify the Numbers program so that the user can interactively create NumberThread s and assign them a priority. Modify the NumberThread s so that they print their numbers indefinitely (rather than for a fixed number of iterations). Then experiment with the system by observing the effect of introducing threads with the same, lower, or higher priority. How do the threads behave when they all have the same priority? What happens when you introduce a higher-priority thread into the mix? What happens when you introduce a lower-priority thread into the mix?
17.Bouncing Ball Thread.
Create a bouncing ball simulation in which a single ball (thread) bounces up and down in a vertical line. The ball should bounce off the bottom and top of the enclosing frame.
18.Bouncing Balls Threads.
Modify the simulation in the previous exercise so that more than one ball can be introduced. Allow the user to introduce new balls into the simulation by pressing the space bar or clicking the mouse.
19.Wild Bouncing Balls.
Modify your solution to the previous problem by having the balls bounce off the wall at a random angle.
20.Counting with threads.
Challenge: One type of producer/consumer problem is the reader/writer problem. Create a subclass of JTextField that can be shared by threads, one of which writes a random number to the text field, and the other of which reads the value in the text field. Coordinate the two threads so that the overall effect of the program will be to print the values from 0 to 100 in the proper order. In other words, the reader thread shouldn’t read a value from the text field until there’s a value to be read. The writer thread shouldn’t write a value to the text field until the reader has read the previous value.
21.Sign Program.
Challenge: Create a streaming banner thread that moves a simple message across a panel. The message should repeatedly enter at the left edge of the panel and exit from the right edge. Design the banner as a subclass of JPanel and have it implement the Runnable interface. That way it can be added to any user interface. One of its constructors should take a String argument that lets the user set the banner’s message.
22.Slide Show Program.
Challenge: Create a slide show program, which repeatedly cycles through an array of images. The action of displaying the images should be a separate thread. The frame thread should handle the user interface. Give the user some controls that let it pause, stop, start, speed up, and slow down the images.
23.Horse Racing Simulator.
Challenge: Create a horse race simulation, using separate threads for each of the horses. The horses should race horizontally across the screen, with each horse having a different vertical coordinate. If you don’t have good horse images to use, just make each horse a colored polygon or some other shape. Have the horses implement the Drawable interface, which we introduced in Chapter {chapter-inheritance}.
24.Digital Clock Application.
Challenge: Create a multithreaded digital clock application. One thread should keep time in an endless while loop. The other thread should be responsible for updating the screen each second.