1

I have a requirement in which I have to check whether any file exists in the folder. If yes, then I need to process it one by one. With my basic knowledge I have arrived to a code structure which I have posted below.

I'm creating an infinite loop and checking whether the file exists in that folder. If yes, then I'm creating a thread and processing it, else it waits for a min and checks again.

class sample {    
    synchronized int getNoOfFiles() {
        // get number of files in the folder
    }    
    synchronized void openFile() {
        // open one file
    }
    synchronized void getFileContents() {
        // get the file content
    }
    synchronized void processFileContent() {
        //performing some operation on file contents
    }    
    synchronized void closeFile() {
        //closing the file
    }    
    synchronized void deleteFile() {
        //delete the file
    }

}    
class Test {    
    public static void main(String args[]) {
        int flag=0;
        Sample obj = new Sample();
        while(1) {
            flag = obj.getNoOfFiles();    
            if(flag) {
                for(i=0;i<flag;i++) {
                    MyThread1 t1 = new MyThread1() {
                        public void run() {
                            obj.openFile();
                            obj.getFileContents();
                            obj.processFileContent();
                            obj.closeFile();
                            obj.deleteFile();
                        }
                    };    
                    t1.start();
                }
            }
            else {
                try {
                    Thread.sleep(60000);
                }
            }    
        }
    }    
}
gnat
  • 6,213
  • 108
  • 53
  • 73
srinath
  • 2,748
  • 6
  • 35
  • 56
  • 1
    Threads do not get garbage-collected. So your code already creates a new thread in every loop. Even if 't1' were to reference a thread that is still processing a file, this thread would not stop running. A new one would be instantiated, referenced by 't1' and started though. – Tobias Oct 18 '12 at 19:55
  • oh yeah. so , i am not in need of this for loop (just now edited the code). How could i stall the thread creation until one thread finishes the entire process. can you help me for that – srinath Oct 18 '12 at 20:01
  • Why would you want to do this? If you create a thread and then just wait for the thread to finish, why use concurrency at all? – Tobias Oct 18 '12 at 20:05
  • can you show up a code structure for this if possible. that would help me a lot – srinath Oct 18 '12 at 20:08
  • As the answers suggest I would advise using a Timer or Executor. If you are interested in when exactly a thread has finished, use a callback. – Tobias Oct 18 '12 at 20:12

2 Answers2

3

Instead of doing this kind of thing yourself, I suggest you take a look at the Timer class, that can be used to do recurring tasks. Because messing around with threads manually can often result in weird bugs.

Even better would be ScheduledThreadPoolExecutor, but it might be a bit complicated if you've never used executors before. See Keppil's answer for how to do this.

The differences between the two are nicely summed up here: Java Timer vs ExecutorService?.

Community
  • 1
  • 1
rolve
  • 10,083
  • 4
  • 55
  • 75
2

I would suggest using a ScheduledExecutorService:

ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(new Runnable() {
        public void run()
        {
            obj.openFile();
            obj.getFileContents();
            obj.processFileContent();
            obj.closeFile();
            obj.deleteFile();
         }
    }, 0, 1, TimeUnit.MINUTES);
Keppil
  • 45,603
  • 8
  • 97
  • 119