Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:52:04+00:00 2026-06-09T10:52:04+00:00

Is the code below legit for synchronizing myIntArray? Will it prevent the three bottom

  • 0

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);
                           }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:52:05+00:00Added an answer on June 9, 2026 at 10:52 am

    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:

    public class HelpPlease {
        public int myIntArray[] = new int[100];
        Handler handler=new Handler();
    
        public void chooseSquare() {
            ...
            final Runnable r = new Runnable()
            {
                public void run() 
                {
                    delayedMethod1();
                    delayedMethod2();
                    method3();
                }
            };
            handler.postDelayed(r, 1000);
        }
    
        public void delayedMethod1() {
            ...
            change myIntArray;
            otherMethodsABC();
        }
    
        public void delayedMethod2() {
            ...
            change myIntArray;
            otherMethodsDEF();
        }
    
        public void method3() {
            ...
            change myIntArray;
            otherMethodsGHI();
        }
    }
    

    No need for synchronization at all. It’s important to use a single Handler to guarantee that multiple calls to chooseSquare() will result in serialized execution of the runnables.

    EDIT:

    Based on your latest comments, here’s how I’d proceed. First, have a single Handler object that is accessible by all your action scheduling code. To use your two examples, these could then be implemented as follows:

    public void chooseSquare() {
        . . .
        if (point_scored) {
            makeSquaresGlow(list_of_squares);
            playSound(sound_to_play);
            handler.postDelayed(new Runnable() {
                public void run() {
                    makeSquaresNotGlow(list_of_squares);
                }
            }, 1000L);
        }
        if (time_for_attack(purple)) {
            handler.postDelayed(new Runnable() {
                public void run() {
                    launchAttack(purple);
                }
            }, 1000L);
        }
    }
    

    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 (launchAttack and makeSquaresNotGlow will be scheduled at the same time), but handler will execute one at a time. If the sequencing of these delayed actions is vital, you can define a “meta” action Runnable that accepts a sequence of actions and executes them in a prescribed order at a future time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below will validate required fields within currently visible fieldsets, but only if
Code below: $_SESSION = array(); Will it clear all session data? If I wouldn't
(Code below has been updated and worked properly) There is dynamic OrderBy sample from
The code below works exactly as I want it to in my perl script.
Code below from other stackoverflow answer is used in jqGrid to implement checkbox using
The code below works. But if I comment out the line Dim objRequest As
Code below taken from here . * qsort example */ #include <stdio.h> #include <stdlib.h>
Code below is used to create post data from jqGrid colmondel and post it.
The code below shows my program that, when switches between methods home(), plus, minus(),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.