e-Science e-Business e-Government and their Technologies
Homework 2: Concurrent Programming
-
Modify the implementation of the
SimpleQueue
class, given in the lectures. Replace the rem()
method with a get() method that blocks
(waits) if the queue is currently emptly. You should
use the standard Java wait() and
notify() methods for this.
-
This queue may be used as a channel for communication
between threads. Rename your queue class to
Channel; rename the the add()
method to send(); rename the
get() method to recv().
-
Write a Java program with two threads, and one channel.
A "producer" thread sends a series of messages
to a "consumer" thread through the channel. The consumer
thread should print the messages it receives.
-
Write another Java program with two threads, and two
channels:
a "client" thread sends a series of messages to a
"server" thread using the first channel. The client
expects to receive a reply after each send.
The server thread receives the client's
messages and sends some extended (or otherwise altered)
messages back
to the client, using the second channel. The client
should print the messages it sends, and the messages it
receives back.
-
All threads should run in the same virtual machine.
Do not use sockets, or any other utility classes
like pipes! "Messages" may be simple strings like
"message 1", "message 2", etc.
Send the source of your Java classes to the address
below, preferably
in a zip file. You should not need to write more than
about 200 lines of code.
- Mail your code to
Bryan Carpenter with
a due date of Sunday February 8. You can
contact me for clarifications, if necessary.