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:
- Illustrate with clear examples the key issues associated with concurrent and multi-threaded programs.
- Demonstrate a clear understanding of the various mechanisms through which many of the problems can be resolved without sophisticated mechanisms.
- Demonstrate how mutual exclusion locks and semaphores can control access to shared state.
- Develop multi-threaded applications in Java.
- Use Java's inbuilt synchronized keyword for performing mutual exclusion.
- Use Java's Object.wait and Object.notify methods to handle thread synchronisation.
- 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
Go to the start of the main content
Go to the site navigation links
Notes and Handouts
- Slides: This is a PDF document with this week's notes, with one slide per page. Use this for viewing on your computer.
- Handouts: This is a PDF document with this week's notes, with six slides per page. Use this for printing.
Return to the internal page navigation links
Go to the start of the main content
Go to the site navigation links
Tasks
| Task Number | Details |
|---|---|
| 1 | Download 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 . ProducerConsumerMainNote 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. |
| 8 | Try 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
Go to the start of the main content
Go to the site navigation links
Further Reading
Return to the internal page navigation links
Go to the start of the main content
Go to the site navigation links
This is the end of the content of this page. Everything that follows is a link to another page.
Site Navigation Links
Teaching
- Distributed Systems
- Advanced Internet Development
- Object Oriented Programming
- Projects
- Examinations
- Universal Design