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

com.jme3.system.AWTTaskExecutor Maven / Gradle / Ivy

There is a newer version: 3.7.0-stable
Show newest version
/*
 * Copyright (c) 2009-2019 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.jme3.system;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 

* This class is dedicated to the queuing of AWT related tasks and their execution. * It's able to store tasks that have to be executed within an AWT context and execute them at the specified time. *

*

* This class is an AWT implementation of the JavaFX original code provided by Alexander Brui (see JME3-FX) *

* @author Julien Seinturier - COMEX SA - http://www.seinturier.fr * @author Alexander Brui (JavaSaBr) */ public class AWTTaskExecutor { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private static final AWTTaskExecutor INSTANCE = new AWTTaskExecutor(); /** * Get the instance of the executor. * @return the instance of executor. */ public static AWTTaskExecutor getInstance() { return INSTANCE; } /** * The list of waiting tasks. */ private final List waitTasks; private AWTTaskExecutor() { waitTasks = new LinkedList(); } public List getWaitingTasks(){ return waitTasks; } /** * Add the given {@link Runnable runnable} to the list of planned executions. * @param task the task to add. * @see #execute() */ public void addToExecute(final Runnable task) { lock.writeLock().lock(); try { waitTasks.add(task); } catch (Exception e) { // This try catch block enable to free the lock in case of any unexpected error. } lock.writeLock().unlock(); } /** * Execute all the tasks that are waiting. * @see #addToExecute(Runnable) */ public void execute() { if (waitTasks.isEmpty()) return; lock.readLock().lock(); try { for(Runnable runnable : waitTasks) { runnable.run(); } } catch (Exception e) { // This try catch block enable to free the lock in case of any unexpected error. } waitTasks.clear(); lock.readLock().unlock(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy