AWTTaskExecutor.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2009-2019 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.system;
  33. import java.util.LinkedList;
  34. import java.util.List;
  35. import java.util.concurrent.locks.ReadWriteLock;
  36. import java.util.concurrent.locks.ReentrantReadWriteLock;
  37. /**
  38. * <p>
  39. * This class is dedicated to the queuing of AWT related tasks and their execution.
  40. * It's able to store tasks that have to be executed within an AWT context and execute them at the specified time.
  41. * </p>
  42. * <p>
  43. * This class is an AWT implementation of the <a href="http://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html">JavaFX</a> original code provided by Alexander Brui (see <a href="https://github.com/JavaSaBr/JME3-JFX">JME3-FX</a>)
  44. * </p>
  45. * @author Julien Seinturier - COMEX SA - <a href="http://www.seinturier.fr">http://www.seinturier.fr</a>
  46. * @author Alexander Brui (JavaSaBr)
  47. */
  48. public class AWTTaskExecutor {
  49. private final ReadWriteLock lock = new ReentrantReadWriteLock();
  50. private static final AWTTaskExecutor INSTANCE = new AWTTaskExecutor();
  51. /**
  52. * Get the instance of the executor.
  53. * @return the instance of executor.
  54. */
  55. public static AWTTaskExecutor getInstance() {
  56. return INSTANCE;
  57. }
  58. /**
  59. * The list of waiting tasks.
  60. */
  61. private final List<Runnable> waitTasks;
  62. private AWTTaskExecutor() {
  63. waitTasks = new LinkedList<Runnable>();
  64. }
  65. public List<Runnable> getWaitingTasks(){
  66. return waitTasks;
  67. }
  68. /**
  69. * Add the given {@link Runnable runnable} to the list of planned executions.
  70. * @param task the task to add.
  71. * @see #execute()
  72. */
  73. public void addToExecute(final Runnable task) {
  74. lock.writeLock().lock();
  75. try {
  76. waitTasks.add(task);
  77. } catch (Exception e) {
  78. // This try catch block enable to free the lock in case of any unexpected error.
  79. }
  80. lock.writeLock().unlock();
  81. }
  82. /**
  83. * Execute all the tasks that are waiting.
  84. * @see #addToExecute(Runnable)
  85. */
  86. public void execute() {
  87. if (waitTasks.isEmpty()) return;
  88. lock.readLock().lock();
  89. try {
  90. for(Runnable runnable : waitTasks) {
  91. runnable.run();
  92. }
  93. } catch (Exception e) {
  94. // This try catch block enable to free the lock in case of any unexpected error.
  95. }
  96. waitTasks.clear();
  97. lock.readLock().unlock();
  98. }
  99. }