Link to the DIT Home Page

Dublin Institute of Technology, School of Computing
Ciarán O'Leary

  Home -> Teaching -> DT249-4 Distributed Systems -> Notes -> Week 08
  DT249-4 Distributed Systems

Week 08


Learning Outcomes:

Upon completion of this class, you should be able to:

  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.

Tasks

1. Download this program which spawns threads of many different priorities. Observe how the high priority threads are given preference over the lower priority threads.

 

2. Download Main.java, Producer.java, Consumer.java and MyData.java. Compile all the code and run Main. Notice how the threads are not properly synchronised.

 

3. Download the new version of MyData.java which uses busy loops. Rewrite Main so that more than one consumer is running. Observe the  difficulties this presents.

 

4. Download the new version of MyData.java which uses Java object monitors and busy loops. Does this help address the problems observed in Task 3?

 

5. Download the new version of MyData.java which uses Java object monitors on code segments rather than whole methods. This will help avoid any deadlock situations.

 

6. Download the new version of MyData.java which uses Java object monitors with wait() and notify() methods to avoid the requirement for busy loops.

 

7. Try out the code from Task 7 with many producers and many consumers. Observe how it maintains its thread safety.

 

8. Implement a multithreaded program with the following characteristics:
  • Three threads start executing. Adds up all the numbers from 1 to some large number.
  • When all three threads finish, they each print out their totals.

See partial code here.


Further Reading

Java Tutorial section on

Hit Counter  [Home][School of Computing][Dublin Institute of Technology]