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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:20:02+00:00 2026-05-31T08:20:02+00:00

I am retrieving some values from DB in form of ArrayList of 240 size

  • 0

I am retrieving some values from DB in form of ArrayList of 240 size and creating a sub list from this list of 8 values each. I want to do some processing on this sub list which will be passed as the parameter to the thread.
I am creating the thread object in the for loop where this sub list is created and passed to the thread. But I am facing problems that I have to wait till one thread finishes its work because the parameter is global in the thread and gives concurrency error.
How can I not make it a global variable and do processing on my sub list without waiting for 1 thread to finish, I want to send parallel requests and process this sub list.
Please see some code I m writing:

Class a {

    for(int j=0; j<240; j++) {

    subOrders.add(orders.get(j));
    if(subOrders.size()==8)
    {           
         TestThread test = new TestThread(subOrders);   
         try {
            test.t.join();
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            subOrders.clear();
        }

    }
}

Thread Class :

public class TestThread implements Runnable {

    ArrayList<Integer> b ;
    public Thread t;
    public TestThread(ArrayList<Integer> a) {
        b=a;
        t= new Thread(this);
        t.start();  
    }


    @Override
    public void run() {
        ArrayList<Integer> c = b;

            System.out.println(c);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           




    }
  • 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-31T08:20:03+00:00Added an answer on May 31, 2026 at 8:20 am

    This is the normal way you would pass data to a thread. Note that this method requires that the variable be declared final. The first way of obtaining the sub list is fine if you do not need to alter the sublist. The second can be modified without having to worry about synchronisation issues (any modifications will not be reflected in the global orders list).

    for (int i = 0; i < 240; i+=8) {
        // this sub list is backed by the orders list, so you should be careful
        // if you need to alter the sub list.
        final List<Integer> subList = orders.subList(i, i+8); 
        final List<Integer> subListThatAllowsLocalModifications =
                new ArrayList<Integer>(subList);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(subList);
            }
        });
        t.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am creating a few divs from some JSONP data that i am retrieving
I am retrieving some data from a table in mysql. The values are stored
This is puzzling me. I'm using PetaPoco to retreive some values from a database,
I am retrieving some configuration from a service and passing this data to the
I have a pretty simple form with some fields from a doctrine model. $this->widgetSchema['fields']
I am retrieving some data from the server which looks something like this: 1:some
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj
Currently, I'm retrieving some data from a MySQL database table, but the problem is
Within CodeIgniter, I'm doing a select, retrieving some blogposts. But I also want the
For some reason I'm having a problem retrieving data from my database. It leaves

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.