Skip to main content

You are here: Teaching, Distributed Systems, Week 2

This week's topic is Concurrent Programming in Java.


Internal Page Navigation Links


Learning Outcomes

Upon completion of this week's class and tasks, you should be able to do the following:

  1. Illustrate with clear examples the key issues associated with concurrent and multi-threaded programs.
  2. Demonstrate a clear understanding of the various mechanisms through which many of the problems can be resolved without sophisticated mechanisms.
  3. Demonstrate how mutual exclusion locks and semaphores can control access to shared state.
  4. Develop multi-threaded applications in Java.
  5. Use Java's inbuilt synchronized keyword for performing mutual exclusion.
  6. Use Java's Object.wait and Object.notify methods to handle thread synchronisation.
  7. Describe clearly the various states that a Java thread can be in, and the ways that threads change states, both through the operating system's inbuilt scheduler, by explicitly calling methods and by passively exiting methods.

Return to the internal page navigation links


Notes and Handouts

Return to the internal page navigation links


Tasks

Task NumberDetails
1Download the code for this week's class.
2 Compile and run Loopy.java as follows:
javac Loopy.java
java -classpath . Loopy 5
3 Compile and run Joiner.java as follows:
javac Joiner.java
java -classpath . Joiner
4 Compile and run JoinerTimed.java as follows:
javac JoinerTimed.java
java -classpath . JoinerTimed
5 Compile and run NumberLetter.java as follows:
javac NumberLetter.java
java -classpath . NumberLetter
6 Compile and run NumberLetterPriority.java as follows:
javac NumberLetterPriority.java
java -classpath . NumberLetterPriority
7 Compile and run each of the Producer - Consumer examples by changing to the appropriate directory, and executing the following commands.
javac *.java
java -classpath . ProducerConsumerMain
Note that the source code for Producer.java, Consumer.java and ProducerConsumerMain.java is the same for each of the examples. It is only the code in Data.java that changes from one example to the next.
8Try out each of the examples with multiple producers and consumers.
9

Provide the code for the following Java program:

The program takes an unlimited number of integer values as command line arguments.

For each command line argument, it starts a thread, which calculates the factorial of the number (the product of the number and all numbers less than it and greater than 0).

The main thread waits until all worker threads have finished their calculation, and then prints out the result.

Note: The main thread must go into a waiting state.

Return to the internal page navigation links


Further Reading

Return to the internal page navigation links