Crossfire JXClient, Trunk
SoundTaskExecutor.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.sound;
24 
25 import java.util.concurrent.BlockingQueue;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.concurrent.Semaphore;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class SoundTaskExecutor {
35 
39  @NotNull
40  private final BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<>();
41 
45  @NotNull
46  private final Thread thread = new Thread(this::executeTasks, "JXClient:SoundTaskExecutor");
47 
51  public SoundTaskExecutor() {
52  thread.setDaemon(true);
53  }
54 
58  public void start() {
59  thread.start();
60  }
61 
65  private void executeTasks() {
66  while (!Thread.currentThread().isInterrupted()) {
67  try {
68  tasks.take().run();
69  } catch (final InterruptedException ignored) {
70  thread.interrupt();
71  break;
72  }
73  }
74  }
75 
81  public void execute(@NotNull final Runnable task) {
82  if (Thread.currentThread() == thread) {
83  throw new IllegalStateException("must not be called from a sound task");
84  }
85  tasks.offer(task);
86  }
87 
95  public void executeAndWait(@NotNull final Runnable task) throws InterruptedException {
96  if (Thread.currentThread() == thread) {
97  throw new IllegalStateException("must not be called from a sound task");
98  }
99  final Semaphore sem = new Semaphore(0);
100  tasks.offer(() -> {
101  try {
102  task.run();
103  } finally {
104  sem.release();
105  }
106  });
107  sem.acquire();
108  }
109 
110 }
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.executeAndWait
void executeAndWait(@NotNull final Runnable task)
Schedules a task for execution.
Definition: SoundTaskExecutor.java:95
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.executeTasks
void executeTasks()
Executes the tasks from tasks.
Definition: SoundTaskExecutor.java:65
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.tasks
final BlockingQueue< Runnable > tasks
The pending tasks.
Definition: SoundTaskExecutor.java:40
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor
Executes non-blocking tasks for sound-related functions.
Definition: SoundTaskExecutor.java:34
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.thread
final Thread thread
The thread executing the tasks.
Definition: SoundTaskExecutor.java:46
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.execute
void execute(@NotNull final Runnable task)
Schedules a task for execution.
Definition: SoundTaskExecutor.java:81
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.start
void start()
Activates this instance.
Definition: SoundTaskExecutor.java:58
com.realtime.crossfire.jxclient.sound.SoundTaskExecutor.SoundTaskExecutor
SoundTaskExecutor()
Creates a new instance.
Definition: SoundTaskExecutor.java:51