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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:01:01+00:00 2026-06-09T05:01:01+00:00

I have some class. For example: public class Data { private String name; public

  • 0

I have some class. For example:

public class Data {
    private String name;

    public Data(String url) {
        // There is download something from the Internet and set field "name".
    }

    public String getName() {
        return name;
    }
}

In some method I need to initialize array of objects Data.

ArrayList<Data> list = new ArrayList<Data>;
for(int i=0; i<max; i++) {
    list.add(new Data("http://localhost/" + String.valueOf(i)));
}

But it is to long. I wanna do this:

final ArrayList<Data> list = new ArrayList<Data>;
for(int i=0; i<max; i++) {
    final int tmp = i;
    new Thread() {

        public void run() {
            list.add(new Data("http://localhost/" + String.valueOf(tmp)));
        }   

    }.start();

}

But the main thread ends sooner than the others and variable list is empty. What should I do? Help pls 🙂

UP. That is not too fast to download some data from the Internet that’s why I’ve created several threads.

  • 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-09T05:01:02+00:00Added an answer on June 9, 2026 at 5:01 am

    Instead of dealing with the low level details of the Thread API, you could use the java concurrent package, with an executor to handle the threads (I don’t know what ListArray is but if it is not thread safe you will have issues with the solution proposed some of the other answers: adding a join will not be sufficient).

    For example, a simplified example would be:

    final ExecutorService executor = Executors.newFixedThreadPool(10);
    final List<Future<Data>> list = new ArrayList<Future<Data>>(max);
    
    for (int i = 0; i < max; i++) {
        final int tmp = i;
        Callable<Data> c = new Callable<Data>() {
            @Override
            public Data call() {
                return new Data("http://localhost/" + tmp);
            }
        };
        list.add(executor.submit(c));
    }
    
    executor.shutdown();
    
    for (Future<Data> future : list) {
        Data data = future.get(); //will block until the page has been downloaded
        //use the data
    }
    

    Ideally, you whould add some error handling around future.get(), as it will throw an ExecutionException if your task throws an exception, which I suppose could happen if the page is not availble for example.

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

Sidebar

Related Questions

If say I have some generic class, for example: public class Attribute<T> { }
I have a class along the lines of: public class Observation { private String
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
So I have some class starting with #include <wchar.h> #include <stdlib.h> and there is
In Python, say I have some class, Circle, that inherits from Shape. Shape needs
I have an app in android in which I return some data from facebook
I have a map declared inside a class as follows: class Example { public:
In a public static class I have the following local method: private static int
I have some class that I'm passing as a result of a service method,
I have some class SimpleButton . It is a Child of Button class. Button

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.