Is the code below legit for synchronizing myIntArray?
Will it prevent the three bottom methods from changing myIntArray out of order?
I Want things to happen in the order delayedMethod1, delayedMethod2, method3 and not have one of them screwed up by running before the previous one has finished its changes to myIntArray.
Do the bottom 3 method declarations need the synchronized keywords?
Do the bottom 3 methods need to contain synchronized(myIntArray) blocks?
Should my synchronized block be around the Runnable rather than inside it?
Do I neet notify, wait, or join commands?
public class HelpPlease {
public int myIntArray[] = new int[100];
public void chooseSquare() {
...
Handler handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
synchronized(myIntArray) {
delayedMethod1();
}
}
};
handler.postDelayed(r, 1000);
...
Handler handler2=new Handler();
final Runnable r2 = new Runnable()
{
public void run()
{
synchronized(myIntArray) {
delayedMethod2();
}
}
};
handler2.postDelayed(r2, 1000);
...
synchronized(myIntArray) {
method3();
}
}
public void delayedMethod1() {
...
change myIntArray;
otherMethodsABC();
{
public void delayedMethod2() {
...
change myIntArray;
otherMethodsDEF();
}
public void method3() {
...
change myIntArray;
otherMethodsGHI();
}
}
More details: Handler/Runnable delays producing events that are out of sync sometimes
EDIT:
Does this make sense? To run a thread a wait for it to finish? Not sure how to add the delay tho, and that was the whole point.
//Handler handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
delayedMethod();
}
};
//handler.postDelayed(r, COMPUTER_MOVE_DELAY);
ExecutorService es = Executors.newFixedThreadPool(1);
final Future f1 = es.submit(r);
try
{
f1.get();
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (ExecutionException e)
{
throw new RuntimeException(e);
}
Don’t try doing this with synchronized blocks. It will only guarantee that no two blocks will run simultaneously, not which order they run. You’d be better off writing a “parent” runnable that executes the delayed methods in the order you want. Alternatively, you can have the first method, when it is finished, post a runnable to run the second, etc., chaining them together.
To be more specific, if you want a delay and then for the three methods to run in sequence, I’d code it like this:
No need for synchronization at all. It’s important to use a single
Handlerto guarantee that multiple calls tochooseSquare()will result in serialized execution of the runnables.EDIT:
Based on your latest comments, here’s how I’d proceed. First, have a single
Handlerobject that is accessible by all your action scheduling code. To use your two examples, these could then be implemented as follows:Assuming that
chooseSquare()is called on the event thread, everything (including the delayed method calls) will also run on the same thread, so no synchronization is needed. There may be race conditions (launchAttackandmakeSquaresNotGlowwill be scheduled at the same time), buthandlerwill execute one at a time. If the sequencing of these delayed actions is vital, you can define a “meta” actionRunnablethat accepts a sequence of actions and executes them in a prescribed order at a future time.