Example usage for java.util TimerTask TimerTask

List of usage examples for java.util TimerTask TimerTask

Introduction

In this page you can find the example usage for java.util TimerTask TimerTask.

Prototype

protected TimerTask() 

Source Link

Document

Creates a new timer task.

Usage

From source file:com.yobidrive.diskmap.DiskMapManager.java

public void startSyncTask(long period) {
    logger.info("Sync logs every " + period + " ms");
    lastBtmSync = new Date().getTime();
    if (syncThreadTask != null)
        syncThreadTask.cancel();/*from ww w .  j av a2 s  .co m*/
    syncThreadTask = new TimerTask() {
        @Override
        public void run() {
            try {
                sync();
            } catch (Exception ex) {
                //swallow exception
                logger.error(ex.getMessage(), ex);
            }
        }
    };
    new Timer().schedule(syncThreadTask, period, period);
}

From source file:MonitorSaurausRex.MainMenu.java

public boolean MonitorDirectory() {

    Path directory = Paths.get("C:/Users/" + user + "/Documents/");
    try {/*  w  ww . j av a 2s  .c om*/
        WatchService fileSystemWatchService = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = directory.register(fileSystemWatchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);

        testTimer = new TimerTask() {

            @Override
            public void run() {
                checkPause = true;
                test = changeCheck();

                if (test) {
                    testTimer.cancel();
                    System.out.println("Quaritnen sucsessfully activates");
                }
            }

        };

        Timer timer = new Timer();
        timer.schedule(testTimer, 0, 1000);
        while (true) {
            WatchKey watchKeyActual = fileSystemWatchService.take();
            for (WatchEvent<?> event : watchKeyActual.pollEvents()) {

                WatchEvent.Kind<?> eventKind = event.kind();

                if (eventKind == StandardWatchEventKinds.OVERFLOW) {
                    continue;
                }

                WatchEvent<Path> eventPath = (WatchEvent<Path>) event;
                Path fileName = eventPath.context();

                //timerCheck(); ????? 
                //http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java
                //  boolean test = false;
                if (checkPause == false) {
                    checkPause = true;
                } else {
                    ChangeCounter++;
                    System.out.println("Event " + eventKind + " occurred on " + fileName);
                }
                if (test)
                    break;
            }
            boolean isReset = watchKeyActual.reset();
            if (!isReset || test) {
                break;
            }
        }

    } catch (IOException | InterruptedException ioe) {
    }

    return true; /// EXIT METHOD
}

From source file:com.hpe.application.automation.tools.octane.executor.TestExecutionJobCreatorService.java

/**
 * Delay starting of polling by 5 minutes to allow original clone
 *
 * @param proj/*from   w  ww .j  a v  a 2s. c  o  m*/
 * @param scmTrigger
 */
private static void delayPollingStart(final FreeStyleProject proj, final SCMTrigger scmTrigger) {
    long delayStartPolling = 1000L * 60 * 5;//5 minute
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            scmTrigger.start(proj, false);
        }
    }, delayStartPolling);
}

From source file:com.yobidrive.diskmap.DiskMapManager.java

public void startCleanerTask() {
    lastBtmSync = new Date().getTime();
    if (cleanerTask != null)
        cleanerTask.cancel();//from ww w  .jav  a 2 s .  co m
    cleanerTask = new TimerTask() {
        @Override
        public void run() {
            try {
                startClean();
            } catch (Exception ex) {
                //swallow exception
                logger.error(ex.getMessage(), ex);
            }
        }
    };
    //FIXME: Put it again later
    //new Timer().schedule( cleanerTask, 5000 , 5000);
}

From source file:com.eutectoid.dosomething.picker.PlacePickerFragment.java

private Timer createSearchTextTimer() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override/*from   w  w w  .j a v a  2s .com*/
        public void run() {
            onSearchTextTimerTriggered();
        }
    }, 0, searchTextTimerDelayInMilliseconds);

    return timer;
}

From source file:com.irccloud.android.activity.ImageViewerActivity.java

private void hide_actionbar() {
    if (mHideTimer != null) {
        if (mHideTimerTask != null)
            mHideTimerTask.cancel();//from  ww  w . j a  v a 2 s  .  co m
        mHideTimerTask = new TimerTask() {
            @Override
            public void run() {
                ImageViewerActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (Build.VERSION.SDK_INT > 16) {
                            toolbar.animate().alpha(0).translationY(-toolbar.getHeight())
                                    .withEndAction(new Runnable() {
                                        @Override
                                        public void run() {
                                            toolbar.setVisibility(View.GONE);
                                        }
                                    });
                        } else {
                            toolbar.setVisibility(View.GONE);
                        }
                    }
                });
            }
        };
        mHideTimer.schedule(mHideTimerTask, 3000);
    }
}

From source file:com.mediaworx.intellij.opencmsplugin.listeners.OpenCmsModuleFileChangeListener.java

/**
 * Handles all the lists of deleted, moved and renamed files collected in
 * {@link #handleFileDeleteEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent)},
 * {@link #handleFileMoveEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent)} and
 * {@link #handleFileRenameEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent)}
 * @return <code>true</code> if any action was executed
 * @throws CmsConnectionException/*ww  w . j  a  va2s .c o  m*/
 */
private boolean handleAffectedFiles() throws CmsConnectionException {

    boolean wasExecuted = false;

    // Delete files
    if (vfsFilesToBeDeleted.size() > 0) {
        wasExecuted = deleteFiles();
    }
    // Move files
    if (vfsFilesToBeMoved.size() > 0) {
        wasExecuted = moveFiles();
    }
    // Rename files
    if (vfsFilesToBeRenamed.size() > 0) {
        wasExecuted = renameFiles();
    }

    // Refresh the affected files in the IDEA VFS after a short delay (to avoid event collision)
    if (refreshFiles.size() > 0) {
        final List<File> filesToBeRefreshedLater = new ArrayList<File>(refreshFiles);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                LocalFileSystem.getInstance().refreshIoFiles(filesToBeRefreshedLater, true, false, null);
            }
        }, 2000);
    }

    return wasExecuted;
}

From source file:com.irccloud.android.fragment.UsersListFragment.java

@Override
public void setArguments(Bundle args) {
    cid = args.getInt("cid", 0);
    bid = args.getInt("bid", 0);
    channel = args.getString("name");
    if (tapTimer == null)
        tapTimer = new Timer("users-tap-timer");

    tapTimer.schedule(new TimerTask() {
        @Override//  www  . ja  v  a2 s  . c om
        public void run() {
            if (getActivity() != null)
                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        ArrayList<UsersDataSource.User> users = null;
                        if (ChannelsDataSource.getInstance().getChannelForBuffer(bid) != null)
                            users = UsersDataSource.getInstance().getUsersForBuffer(bid);
                        refresh(users);
                        try {
                            if (getListView() != null)
                                getListView().setSelection(0);
                        } catch (Exception e) { //Sometimes the list view isn't available yet
                        }
                    }

                });
        }
    }, 100);
}

From source file:io.gromit.geolite2.GeoLocation.java

/**
 * Read database./*www. j a v a 2  s.  c  o m*/
 *
 * @param databaseLocationUrl the database location url
 */
private void readDatabase(String databaseLocationUrl) {
    String onlineMD5Checksum = null;
    try {
        onlineMD5Checksum = IOUtils.toString(new URL(md5ChecksumUrl).openStream()).trim();
    } catch (Exception e) {
        logger.error("could not read MD5 online: {}", e.getMessage());
        return;
    }
    if (!onlineMD5Checksum.equals(localMD5Checksum)) {
        try {
            logger.info("UPDATING local database with online database");
            DatabaseReader newReader = new DatabaseReader.Builder(
                    new GZIPInputStream(new URL(databaseLocationUrl).openStream()))
                            .locales(Collections.singletonList("en")).fileMode(FileMode.MEMORY).withCache(cache)
                            .build();
            if (databaseReader != null) {
                final DatabaseReader readerToClose = databaseReader;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            readerToClose.close();
                        } catch (IOException e) {
                            logger.warn("error closing reader: {}", e.getMessage());
                        }
                    }
                }, 60 * 1000);
            }
            this.localMD5Checksum = onlineMD5Checksum;
            this.databaseReader = newReader;
            logger.info("UPDATED local database with online database");
        } catch (Exception e) {
            logger.error("could not read Database online: {}", e.getMessage());
        }
    } else {
        logger.info("local and online database are the same");
    }
    this.cityFinder.readCities();
    this.countryFinder.readCountries();
    this.subdivisionFinder.readLevelOne();
    this.subdivisionFinder.readLevelTwo();
    this.timeZoneFinder.readTimeZones();
}