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 7608303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:50+00:00 2026-05-31T00:52:50+00:00

I am making a game on android which will have three threads. These will

  • 0

I am making a game on android which will have three threads. These will be my GL rendering thread, my game update thread and the UI thread.

The game revolves around an ArrayList (and also a regular array) of ‘ball’ objects. The user creates a ball by tapping the screen, the game update thread moves the balls around the screen and the rendering thread draws all the balls.

The problem I’m having is dealing with the concurrency issue that arises from many threads accessing the same data, namely the array of balls. An issue in particular is receiving an out of bounds exception when the game thread removes a ball, and the rendering thread tries to access the removed ball.

If you consider the code samples I’ve posted. What is the best way to ensure that once either of these code blocks (on different threads) has started execution, that the other cannot start until it has finished?

rendering thread

int size = balls.size();
for(int i = 0; i < size; i++){
      Ball ball = balls.get(i);
      drawBall(ball.xCoord, ball.yCoord, ball.image);
}

Game thead

int size = balls.size();
for(int i = size -1; i >= 0; i--){
    Ball b = balls.get(i);
    b.updateBallPosition();
    b.updateBallVelocity();

    if(b.isOutOfBounds())
        balls.remove(i);
}
  • 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-05-31T00:52:51+00:00Added an answer on May 31, 2026 at 12:52 am

    Use a synchronized block to ensure mutual exclusion on a portion of code:

    final Object lock = new Object(); // globally visible lock object
    

    Rendering thread:

    synchronized(lock) {
       int size = balls.size();
       for(int i = 0; i < size; i++){
             Ball ball = balls.get(i);
             drawBall(ball.xCoord, ball.yCoord, ball.image);
       }
    }
    

    Game thread:

    synchronized(lock) {
       int size = balls.size();
       for(int i = size -1; i >= 0; i--){
           i.updateBallPosition();
           i.updateBallVelocity();
    
           if(i.isOutOfBounds())
               balls.remove(i);
       }
    }
    

    At this point, only one of the two threads can execute the code, while the other will block. Note that they must both be synchronized on the same lock object.

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

Sidebar

Related Questions

We are making a game which will add a level editor feature soon. We
I'm making a simple 2d game for the android platform, which works perfectly from
I am making a game for android which needs to play a small beep
I'm making my first Android game which is going to be a 3D arcade-ish
We are in the making of an Android app, a quiz game, which we
I'm making this game where I'm trying to pair people. So I have this
I am making a game for my CS class and the sprites I have
I am making a game that needs a crosshair. I have been playing with
For a 2D game I'm making (for Android) I'm using a component-based system where
I am making a 2D game in Android with Cocos2D, written in Java. Here

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.