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?