0

Synchronized method called from next batch even though previous batch is not completed its execution in synchronized method.

            public class FTPClientInvocation implements Job {
                public FTPClientInvocation() {
                }  
                // Quartz Scheduler will invoke this method
                public void execute(JobExecutionContext context)
                    throws JobExecutionException {       
                    FTPClient ftpClient = new FTPClient();
                    ftpClient.retriveFiles();
                }
            }

FTPClient.java class is

            public class FTPClient {

                public synchronized void retriveFiles() {
                 // from here multi threading  code is called for some file execution.
                }
            }

I am expecting that retriveFiles() method will be invoked only when current batch completes its execution but it is not happening here. Next batch starts executing retriveFiles() method. I have tried to add @DisallowConcurrentExecution annotation on top of FTPClientInvocation class but it also doesn't help.

            @DisallowConcurrentExecution
            public class FTPClientInvocation implements Job {
                public FTPClientInvocation() {
                }  
                // Quartz Scheduler will invoke this method
                public void execute(JobExecutionContext context)
                    throws JobExecutionException {       
                    FTPClient ftpClient = new FTPClient();
                    ftpClient.retriveFiles();
                }
            } 
Brijesh
  • 148
  • 2
  • 16
  • You're creating a new `FTPClient` every time. A `synchronized` instance method locks on `this` which is different for your every invocation. – Kayaman Sep 12 '17 at 14:59
  • The "duplicate" does not answer the question. The question asks how it is possible for a `foo.retrieveFiles()` call in one thread to overlap a `bar.retrieveFiles()` in a different thread? The answer is, `synchronized` does _not_ prevent multiple calls to the same method at the same time. It only prevents different threads from synchronizing on the _same object_ at the same time. If `foo` and `bar` refer to different objects, then the `synchronized` keyword will not block either of the two threads. – Solomon Slow Sep 12 '17 at 16:33
  • I have also tried to create FTPClient object outside the method. – Brijesh Sep 13 '17 at 10:09
  • it might be easier to see why "@DisallowConcurrentExecution" is not working for you. That is the right solve for your problem. – Srinivas Sep 13 '17 at 18:15

0 Answers0