Trash expiration time of realization

Trash achieve expiration time

After a period of time to prevent in the recycle bin, recycle bin contents are automatically deleted.


Method:
1, when a file is deleted as the Recycle Bin, plus a table in the recycle bin deletion time, the best time to save calendar with the Calendar class (I was not allowed to use Date deposit times, probably because I use SimpleDateFormat class converted into a string and then stored reasons).
2, the core of the while loop, all the time after the judge finished file, enter the wait state, waiting for the end of time, all the trash query data again, once traversed, the data in the recycle bin is empty, terminate the while loop. (I would like to place this class delete files, as long as the delete operation, data is stored in the Recycle Bin back to run until the Recycle Bin is empty data must be thread is waiting or sleeping, or may be memory is full.)

public class FileDelete {
    public synchronized void timeDelete() throws ParseException, InterruptedException {
        IRecycleService recycleService = Factory.getRecycleServiceImpl();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /**
         * 使用while进行判定List不为空
         * 进去判断时间是否过期
         */
        while (true) {
            List<Recycle> recycles = recycleService.AllData();
            if (recycles.size() == 0) {
                break;
            }
            for (Recycle recycle : recycles) {
                long date = sim.parse(recycle.getDeleteData()).getTime();
                //测试:两个时间相减的值大于6分钟进行删除操作
                if (Math.abs(date - System.currentTimeMillis()) >= 1000 * 60) {
                    System.out.println("删除!!!");
                    System.out.println("date--<" + sim.format(sim.parse(recycle.getDeleteData())));
                    System.out.println("System.currentTimeMillis()--><" + sim.format(System.currentTimeMillis()));
                    recycleService.OneDelete(recycle);
                }
            }
            this.wait(1000 * 30);   //测试:阻塞
        }
    }
}
Published 61 original articles · won praise 0 · Views 2189

Guess you like

Origin blog.csdn.net/sabstarb/article/details/104453029