Problema con la espera: notificar la implementación

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.

preguntado el 08 de noviembre de 11 a las 09:11

My objective is , when one thread completes file upload , I need to start another thread assigning a new file to it. -> if they are not executing concurrently, why have multiple threads? -

Am starting 4 threads first time . Whenever one thread completes execution , I need to assign a file to a new thread until all the files have been uploaded. -

3 Respuestas

Puede usar el ExecutorService con 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);

respondido 08 nov., 11:13

thanks for the reply . How to make the above code work if everytime 4 threads will be used to upload files till all the files are uploaded ? - chiranjib

exec.awaitTermination does that for you, it will wait until all are finished. I've set a timeout of 900 seconds, which is the maximum wait. If you want to increase the concurrency of the upload just change the number 1 in the constructor (be sure that also the server part supports concurrency). - stivlo

Throw it all away and use java.util.concurrent.Executor.

respondido 08 nov., 11:13

you can join on the thread instead of waiting on it

try {
    t.join();
}catch(InterruptedException e){
    throw new RuntimeException(e);
}
tempThreadCounter++;

respondido 08 nov., 11:13

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.