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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:00:44+00:00 2026-05-25T03:00:44+00:00

This piece of code: synchronized (mList) { if (mList.size() != 0) { int s

  • 0

This piece of code:

synchronized (mList) {
    if (mList.size() != 0) {
        int s = mList.size() - 1;
        for (int i = s; i > 0; i -= OFFSET) {
            mList.get(i).doDraw(canv);
        }
        getHead().drawHead(canv);
    }
}

Randomly throws AIOOBEs. From what I’ve read, the synchronized should prevent that, so what am I doing wrong?

Edits:

AIOOBE = Array Index Out Of Bounds Exception
The code’s incomplete, cut down to what is needed. But to make you happy, OFFSET is 4, and just imagine that there is a for-loop adding a bit of data at the beginning. And a second thread reading and / or modifying the list.

Edit 2:

I’ve noticed it happens when the list is being drawn and the current game ends. The draw-thread hasn’t drawn all elements when the list is emptied. Is there a way of telling the game to wait with emtying the list untill it’s empty?

Edit 3:

I’ve just noticed that I’m not sure if this is a multi-threading problem. Seems I only have 2 threads, one for calculating and drawing and one for user input.. Gonna have to look into this a bit more than I thought.

  • 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-25T03:00:44+00:00Added an answer on May 25, 2026 at 3:00 am

    What you’re doing looks right… but that’s all:

    1. It doesn’t matter on what object you synchronize, it needn’t be the list itself.
    2. What does matter is if all threads always synchronize on the same object, when accessing a shared resource.
    3. Any access to SWING (or another graphic library) must happen in the AWT-Thread.

    To your edit:

    I’ve noticed it happens when the list is being drawn and the current game ends. The draw-thread hasn’t drawn all elements when the list is emptied. Is there a way of telling the game to wait with emtying the list untill it’s empty?

    I think you mean “…wait with emptying the list until the drawing has completed.” Just synchronize the code doing it on the same lock (i.e., the list itself in your case).

    Again: Any access to a shared resource must be protected somehow. It seems like you’re using synchronized just here and not where you’re emptying the list.

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

Sidebar

Related Questions

Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I have this piece of code i want to synchronized between the StartWatch() and
Consider this piece of code: Uint counter = 0; int* p1; int* p2; deque<int>
Here is my problem: This piece of code throws a java.util.ConcurrentModificationException , because the
This piece of code will not compile: synchronized( obj ) { Object a =
Take this piece of code: int a; int *pointer = &a; int **b =
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is from Adobe docs : package { import flash.display.Loader; import
This piece of code is supposed to go through a list and preform some

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.