2

I have been messing around with synchronization in Java and it has yet to work for me.

I have two Runnable objects that are used to create separate threads, and each object has a handle to a shared ArrayList and a shared Object (for use in synchronized). The first Runnable is always reading tsome instance variable for all of the Objects in the array list, and the second Runnable continuously updates the instance variables for all of the Objects in the array list.

The way I have it setup now, both Runnable objects contain a pointer to an Object that I intended to function as a lock.

Runnable 1:

public void run() {
    if (syncLock == null)
        return;
    synchronized (syncLock) {
        try {
            syncLock.wait();
        } catch (InterruptedException e) {
        }

        for (int i = 0; i < list.size(); i++) {
            drawItem(list.get(i));
        }
        syncLock.notify();
    }
}    

Runnable 2:

public void run() {
    if (syncLock == null)
        return;
    synchronized (syncLock) {
        try {
            syncLock.wait();
        } catch (InterruptedException e) {
        }

        for (int i = 0; i < list.size(); i++) {
            updateItem(list.get(i));
        }
        syncLock.notify();
    }
}    

So technically the first Runnable is always drawing the objects on-screen and the second is calculating the items' new position based on change in time.

Anything I am missing?

4
  • 1
    "I have been messing around with synchronization in Java " - there's your problem! :) Commented Mar 7, 2011 at 0:13
  • What are you gaining over using a single thread to do one thing then the other? Commented Mar 7, 2011 at 0:14
  • It just seemed best to separate everything, would you recommend just pushing my updates into the end of my draw method? I just thought that would make things sluggish Commented Mar 7, 2011 at 0:23
  • At the moment these two threads force their work to be done one after the other, which is what would happen if they were just doing it in one thread. When one is running the other is always suspended (or about to be) and vice versa. They are never doing any of their work at the same time, so you aren't gaining anything having 2 threads (as things stand, at least). Commented Mar 7, 2011 at 11:24

1 Answer 1

5

It looks like both your threads are going to start and get stuck in wait(), unless you have some other object you aren't showing there to notify() one of them (just to start them off.) You'd have to be sure that both threads were waiting.

Alternatively you could change one of them to do their work first, then call notify(), then wait(). Again, you'd have to be sure the other thread was already waiting.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, well I didn't leave out anything, and that does seem to make sense. My only question is, would that cause a "java.lang.IllegalMonitorStateException: object not locked by thread before wait()"? Because that made it sound like I had forgot something else.
You should still keep the sections inside the synchronized block. That error happens when you call wait() when you don't have control of the lock (ie you are not inside a synchronized block for the lock).
Odd, the code above is inside a synchronized block is it not?
@Roflha Can you edit your question to include the full stack trace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.