Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I took the Realtime and Concurrent programming course our lecturer told us that when writing concurrent programs in Java and using monitors, most of the logic should be in the monitor and as little as possible in the threads that access it. I never really understood why and I really would like to.

Let me clarify.

In this particular case we had several classes.

Lift extends Thread
Person extends Thread
Monitor, all methods synchronized.

This is nothing we came up with, our task was to implement a lift simulation with persons waiting on different floors, and theses were the class skeletons that were given.

Then our lecturer said to implement most of the logic in the monitor (he was talking about class Monitor as THE monitor) and as little as possible in the threads.

Why would he make a statement like that?

share|improve this question
This makes no sense at all. When you extend something, you create an "is a" relationship, as in dog extends animal. A Person is most decidedly not a Thread. Implementing most of the logic in the monitor makes no sense either. I don't think your lecturer knew what he was talking about. – Robert Harvey Nov 8 '12 at 22:33
cross-posted from SO:… – gnat Nov 9 '12 at 7:16
up vote 2 down vote accepted

He's getting you to do some really elementary multithreading. Or perhaps you could call it single-threading. If everything's in the Monitor class, and every method there is synchronized, nothing can get accessed by more than one thread at a time. This gives up most of the benefits of multithreaded code and is not apt to teach you much. On the other hand, the program is apt to work, thus building confidence in the students. It's no doubt a good starting point.

Hard core multithreading means lots of threads accessing the same fields at the same time. It keeps all the cores on your computer busy and so gets the work done faster. But it's insanely tricky, generating lots of unsolvable bugs. I think your lecturer is trying to ease you into it slowly. (And probably trying to save himself some work helping students whose programs have extremely subtle bugs. And also let the students finish all their assignments by the end of the semester.)

share|improve this answer
"If everything's in the Monitor class, and every method there is synchronized, nothing can get accessed by more than one thread at a time." This is what confuses me. As I see it as much as possible should be done in the threads and the monitors should only be used for data sharing using very short methods. If everything is executing in the monitor, why multi thread? Except for the cases where the treads are blocked by something or other. – evading Nov 8 '12 at 22:53
@refuser: You are quite right. I hope the idea here is to get you a single threaded program that works. From there, in incremental steps, you can work towards a seriously multithreaded program. That way you only have to figure out one or two impossible bugs at a time. The key point here is that when you run two or more threads at the same time, very strange things will start to happen, and I hope the lecturer's concern with this Monitor class is to limit the chaos--and not scare off the lecturees. – RalphChapin Nov 9 '12 at 14:15

Persons are not threads.

That's why I prefer:

Lift implements Runnable
Person implements Runnable

Then you can

Person p = new Person();
Thread t = new Thread(p)

That's exactly why Java designers creates both a Thread class and a Runnable interface, for is a and can do relationships respectively.

share|improve this answer
This part was already pretty clear to me. It was mostly the "put as much of the logic in the monitor as possible" part that confused me. I'm pretty sure that these skeletons are written by some older student so the is a , can do stuff is probably just an oversight. But thank you anyway for taking the time to answer. – evading Nov 9 '12 at 7:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.