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

  • Home
  • SEARCH
  • 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 7709283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:49:22+00:00 2026-06-01T00:49:22+00:00

I read in a few posts that using JUnit to test concurrency is not

  • 0

I read in a few posts that using JUnit to test concurrency is not ideal but I have no choice for now. I have just encountered an exception that I can’t explain.

I run a test where, in summary:

  • I submit 1000 runnables to an executor
  • each runnable adds an element to a list
  • I wait for the executor termination
  • JUnit tells me the list only has 999 elements
  • no exception is printed in the runnable catch block

What could cause that behavior?

Note: I only get the exception from time to time. The code has some non related stuff but I left it there in case I missed something. XXXQuery is an enum.

public void testConcurrent() throws InterruptedException {
    final int N_THREADS = 1000;
    final XXXData xxxData = new AbstractXXXDataImpl();
    final List<QueryResult> results = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(N_THREADS);
    for (int i = 0; i < N_THREADS; i++) {
        final int j = i;
        executor.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    results.add(xxxData.get(XXXQuery.values()[j % XXXQuery.values().length]));
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertEquals(N_THREADS, results.size());
}
  • 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-01T00:49:23+00:00Added an answer on June 1, 2026 at 12:49 am

    You cannot add to the results ArrayList in your Runnable.run() method in multiple threads without synchronizing around it.

    The assertion failed message is showing that although N_THREADS calls to add() were made, the ArrayList got fewer entries because of concurrency race conditions.

    I would use a final array instead of a list. Something like:

    final QueryResult[] results = new QueryResult[N_THREADS];
    for (int i = 0; i < N_THREADS; i++) {
        ...
            public void run() {
                results[j] = data.get(Query.values()[j % Query.values().length]);
            }
    

    Also, I don’t quite get the XXXQuery.values() but I’d pull that into a variable above the loop unless it is changing.

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

Sidebar

Related Questions

I've read a few posts where people have stated (not suggested, not discussed, not
i've just read a few posts on hiding Silverlight code in some way. Main
I have read quite a few posts on SO and around the web of
I've read a few other questions that seem similar but I'm still very confused,
Hey there, I have read the few posts here on when/how to use the
I have read a few posts on fail parameters for a JQuery Ajax call,
I've read quite a few posts that are very similar to the question I'm
Based on a few posts I've read concerning version control, it seems people think
I've read a few instances in reading mathematics and computer science that use the
I've read a few answers on here that condemn the use of svn:externals. I

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.