Lecture 2: Concurrency

Concurrency: motivations for concurrent code; Amdahl’s law and the opportunity for language design. Challenges with concurrent code, including liveness, fairness, scalability. Threads and processes. Java Thread basics. The need for mutual exclusion. Monitors in Java: locks and synchronize, condition variables with wait() and notify().

Link: Slides

Homework

Try the exercise given at the end of the lecture slides. Sun’s tutorial on concurrency gives an introduction to the concurrency facilities provided in Java. Experimentation is essential: try the exercise and tutorial examples inside Netbeans or Eclipse and investigate the facilities for debugging concurrent code.

If you have any thoughts, problems or interesting pointers, please comment here!

11 Responses to Lecture 2: Concurrency

  1. s0803652 says:

    It seems that if we use multiple pigeonholes it becomes extremely easy to produce a deadlock. If e.g. we have two pigeon holes A and B, where A contains a pigeon, and has a pigeon ‘breeder’ attempting to insert a pigeon, and B has a ‘racer’ waiting to extract a pigeon. This can be avoided using the timeout functionality of the wait() method or presumably by checking to see if a pigeonhole contains/does not contain a pigeon before attempting to insert/remove one.

  2. Yes, this could be a bit tricky. Can you think about other ways to avoid the deadlock? And what happens if you generalise to having many breeders and racers?

  3. s0788616 says:

    I have a question about one of the terms used on the slides. I am unclear on what exactly “task” means, as used in the slides. Is it “contained” within a thread or the other way around?Thanks!

  4. I explained in the lecture that, for this course at least, task will be something contained in a thread. Examples in today’s lecture in half an hour!

  5. Laurentiu says:

    On the slide: a simple blocking method, we are supposed to wait until the pigeon hole is empty, but there is nothing emptying it on the slide. Should we assume there is another method emptying it while the put method is waiting or how should we interpret that piece of code?

  6. Yes, you can assume there is a corresponding method emptying it (and experiment with writing this for the homework).

  7. s1019422 says:

    hi, does this problem involve 1 pigeonhole and 1+ pigeons? i image the scenario is 1+ pigeons(threads) race to fly in to the exactly 1 pigeonhole. in the mean time, one thread keeps removing the pigeon(if there is) in the pigeonhole.
    if it is the story, then it seems that “while(contents != null)” is a problem. because with the same pigeonhole object the removing thread shouldn’t wait() but immediately sets contents = null.

  8. I’m not sure I understand the problem you see. The put(o) method shown is for inserting the pigeon into an empty hole, not removing it (the object parameter o should be non-null!). You need to write the get() method which removes the pigeon and sets contents=null. Does this help?

  9. s1019422 says:

    yes, i should’ve write a get() instead of using put(null).