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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:03:00+00:00 2026-05-21T18:03:00+00:00

I am writing a code for my homework, I am not so familiar with

  • 0

I am writing a code for my homework, I am not so familiar with writing multi-threaded applications. I learned how to open a thread and start it. I better show the code.

 for (int i = 0; i < a.length; i++) {
     download(host, port, a[i]);
     scan.next();                        
 }

My code above connects to a server opens a.length multiple parallel requests. In other words, download opens a[i] connections to get the same content on each iteration. However, I want my server to complete the download method when i = 0 and start the next iteration i = 1, when the the threads that download has opened completes. I did it with scan.next() to stop it by hand but obviously it is not a nice solution. How can I do that?

Edit:

public static long  download(String host, int port) {
    new java.io.File("Folder_" + N).mkdir();
    N--;
    int totalLength = length(host, port);
    long result = 0;
    ArrayList<HTTPThread> list = new ArrayList<HTTPThread>();
    for (int i = 0; i < totalLength; i = i + N + 1) {
        HTTPThread t;
        if (i + N > totalLength) {
            t = (new HTTPThread(host, port, i, totalLength - 1));
        } else {
            t = new HTTPThread(host, port, i, i + N);
        } 
        list.add(t);
    }

    for (HTTPThread t : list) {
        t.start();
    }
    return result;
}

And In my HTTPThread;

 public void run() {
     init(host, port);
     downloadData(low, high);
     close();
 }

Note: Our test web server is a modified web server, it gets Range: i-j and in the response, there is contents of the i-j files.

  • 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-21T18:03:01+00:00Added an answer on May 21, 2026 at 6:03 pm

    You will need to call the join() method of the thread that is doing the downloading. This will cause the current thread to wait until the download thread is finished. This is a good post on how to use join.

    If you’d like to post your download method you will probably get a more complete solution

    EDIT:

    Ok, so after you start your threads you will need to join them like so:

    for (HTTPThread t : list) {
        t.start();
    }
    for (HTTPThread t : list) {
        t.join();
    }
    

    This will stop the method returning until all HTTPThreads have completed

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

Sidebar

Related Questions

I sometimes find myself writing code like this: someFunc :: Foo -> Int someFunc
I'm writing some code for testing multithreaded programs (student homework--likely buggy), and want to
Writing code with implicit cast generates CS0266 . Dictionary<Int32, string[]> d1 = new Dictionary<int,
I frequently find myself writing code like this: List<int> list = new List<int> {
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing code, it is pretty common to request a page with an appended
When writing code in an Eclipse project, I'm usually quite messy and undisciplined in
I am currently writing code in C++ to find all possible permutations of 6
I'm writing code to use UIImagePickerController. Corey previously posted some nice sample code on
I am writing code in Java where I branch off based on whether a

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.