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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:00:40+00:00 2026-06-07T20:00:40+00:00

Sorry for the confusing title, I’m not sure how to describe it in minimal

  • 0

Sorry for the confusing title, I’m not sure how to describe it in minimal words but basically what I’m trying to do is return a LinkedList from a method. But I wanna put the actual content inside the method inside a Thread, and then return the data when the thread is done. Here is the code I am using:

public static LinkedList<Result> get(final String prefix){
    final LinkedList<Result> results = new LinkedList<Result>();
    if(thread != null){
        thread.stop();
        thread = null;
    }
    thread = new Thread(
            new Runnable(){
                public void run(){
                    try{
                        URL url = new URL(String.format(URL_INFO, prefix));
                        URLConnection connection = url.openConnection();
                        connection.setReadTimeout(Timeout.TIMEOUT);
                        connection.setConnectTimeout(Timeout.TIMEOUT);
                        InputStream is = connection.getInputStream();
                        Scanner reader = new Scanner(is);
                        while(reader.hasNextLine()){
                            String line = reader.nextLine();
                            if(line != null){
                                if(line.contains(String.format(WORDS_INFO, prefix))){
                                    String[] s = line.split(String.format(PREFIX_INFO, prefix));
                                    String[] s2 = s[1].split("\">");
                                    if(s2.length > 0){
                                        for(int i = 1; i < s2.length; i++){
                                            String l = s2[i];
                                            String[] split = l.split(FINAL_SPLIT);
                                            results.add(new Result(prefix, split[0].trim()));
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        reader.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
    );
    thread.start();
    return results;
}

The thread is statically defined inside the variables section. And basically the problem here is that it returns an empty LinkedList because it’s not waiting for the Thread to be finished. How can I wait until the Thread is finished before returning the LinkedList? Any help would be greatly appreciated. Thanks.

NOTE: The issue is not anything to do with reading the URL, the problem is is that it’s returning an empty LinkedList because the Thread has not finished yet and I want to know how to wait until the Thread is done before it returns something.

  • 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-07T20:00:41+00:00Added an answer on June 7, 2026 at 8:00 pm

    You need to wait till the thread completes execution. Look up the documentation on Thread.join.

    This is just one way to do what you are trying. There are many others, such as ExecutorService

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

Sidebar

Related Questions

Sorry for the confusing title, but I'm not sure what to call those. I'm
sorry if the title is very confusing, but I'm not sure how to really
Sorry for the confusing title, but it basically says it all. Here's the structures
Sorry if the title sounds confusing - but this is what I am trying
Sorry for the confusing title :P I'm sure I should know this but I
Sorry for the somewhat confusing title. Not sure really how to title this. My
Sorry if the title is confusing but I'm trying to get all comments and
I'm sorry if the title is confusing but I'm trying to record audio for
Sorry if my topic is confusing, I am not sure which words should i
I am not sure if the title formulates it well so sorry. I basically

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.