All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.maxifier.mxcache.scheduler.MxSchedulerImpl Maven / Gradle / Ivy

Go to download

Constains all classes necessary for launching a MxCache-instrumentated application

There is a newer version: 2.6.9
Show newest version
/*
 * Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
 */
package com.maxifier.mxcache.scheduler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author Alexander Kochurov ([email protected])
 */
public class MxSchedulerImpl implements MxScheduler {
    private static final Logger logger = LoggerFactory.getLogger(MxSchedulerImpl.class);

    private final BlockingQueue tasks = new LinkedBlockingDeque();
    private Thread schedulerThread;

    @Override
    public synchronized void schedule(Runnable runnable) {
        tasks.offer(runnable);
        if (schedulerThread == null || !schedulerThread.isAlive()) {
            startThread();
        }
    }

    private void startThread() {
        schedulerThread = new Thread(new SchedulerRunner(), "MxCache IO Scheduler");
        schedulerThread.setDaemon(true);
        schedulerThread.start();
    }

    private class SchedulerRunner implements Runnable {
        @SuppressWarnings({"InfiniteLoopStatement"})
        @Override
        public void run() {
            while (true) {
                try {
                    Runnable r = tasks.take();
                    try {
                        r.run();
                    } catch (Throwable e) {
                        logger.error("Cannot execute scheduled task " + r, e);
                    }
                } catch (InterruptedException e) {
                    // ignore
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy