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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:44:54+00:00 2026-05-26T15:44:54+00:00

I am working on Java multithreading , where I am starting 4 threads after

  • 0

I am working on Java multithreading , where I am starting 4 threads after assigning 4 different files to them , to be uploaded to the server.

My objective is , when one thread completes file upload , I need to start another thread assigning a new file to it.

After each file upload , I receive a notification from the server.

// The code for adding the first set of files

 for (int count = 0; count < 4; count++) {
                if (it.hasNext()) {
                    File current = new File((String) it.next());

                    try {
                        Thread t = new Thread(this, current );
                        t.start();
                        t.sleep(100);
                    } catch (Exception e) {
                    }
                }
            }

Now , I am assigning another thread with a file & keeping the thread in a wait state .
When a previous thread notifies , the current thread should start upload.

if (tempThreadCounter == 4 ) {

                if (it.hasNext()) {
                    File current = new File((String) it.next());
                        try {
                        Thread t = new Thread(this, current);
                        t.start();

                        synchronized (this) {
                            t.wait();
                        }
                        tempThreadCounter++;
                    } catch (Exception e) {
                    }
                }
            }

On the final statement on the run method , I am adding the following statement.

public void run (){

// Performing different operations

//Final statement of the run method below
synchronized (this) {
   this.notifyAll();
}
}

Currently , all the 5 threads are starting uploading at the same time.
It should be that the first 4 threads should start uploading & the fifth thread should start only when it it notified by any thread that it had completed its operation.

Any suggestions on the incorrect Thread implementation.

  • 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-26T15:44:55+00:00Added an answer on May 26, 2026 at 3:44 pm

    You can use ExecutorService with newFixedThreadPool and specify a concurrency of 1. But really, then why do you need multiple threads? One thread doing all the uploads so the user interface remains responsive should be enough.

    ExecutorService exec = Executors.newFixedThreadPool(1); //1 thread at a time
    for (int count = 0; count < 4; count++) {
        if (it.hasNext()) {
            File current = new File((String) it.next());
            exec.execute(new Runnable() {
                 @Override
                 public void run() {
                     upload(current);
                 }
            });
         }
    }
    exec.shutdown();
    exec.awaitTermination(900, TimeUnit.SECONDS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a nasty one for me... I'm a PHP guy working in Java
i'm working with java.nio.channels.Selector and i'd like to create a separate thread for each
I am working with Java. I have created one text file. In that text
Im working in Java Swing using JWindows to display images... Ive made different JLabels
I'm working on a java game. in which after some regular millisecond i'm repainting
I am working on a multithreading app in Java 5 . When it executes
While working with Java, I find it hard to position my main window in
I'm working in Java with XML and I'm wondering; what's the difference between an
I am working in Java on a fairly large project. My question is about
I recently started working in Java and was introduced to the wild and crazy

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.