gpt4 book ai didi

java - 在 Java 中使用同步,如果乐器是同一子类的实例,则不允许它们一起演奏

转载 作者:行者123 更新时间:2023-11-30 06:18:30 25 4
gpt4 key购买 nike

我创建了三个抽象类来划分乐器:String、Wind、Arc(其中每一个都扩展 Thread 并指定 run() 方法。我的乐器是这三个的子类。没有一个子类重写 run( ) 方法)。我希望只有一种 super 乐器(弦、管、弧)同时演奏。不超过一个。我怎样才能做到这一点?

重新编辑 09/02/2018 08:46 AM
谢谢@miroh。但我仍然有问题。这里我贴出一个类以及我使用时的Main类。谁能告诉我如何解决?

Archi 类

package strumenti.archi;

import java.io.FileInputStream;
import java.io.InputStream;
import strumenti.Sound;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


public class Archi extends Thread{
String soundFileName;

public Archi(String soundFileName){
this.soundFileName = soundFileName;
}

private static boolean canPlay = true;
private static Object playLock = new Object();

@Override
public void run() {
checkPlayable();
try {
// your code
play();
}
finally { // If a exception happens(or not) during the execution of the code block above, lock must be released.
synchronized (playLock) {
canPlay = true; // enable playing for others
playLock.notifyAll(); // wake up others
}
}

}

/*
* This is used to get the lock for the first one to come. Makes other ones wait.
*/
private static void checkPlayable() {
synchronized (playLock) {
while(!canPlay) {
try {
playLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
canPlay = false;
}
}

public synchronized void play(){
try {
InputStream in = new FileInputStream(soundFileName);

// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);

// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
} catch (Exception ex) {
System.err.println("Cannot play sound.");
System.exit(0);
}
}


}

主类

package orchestra;

import strumenti.archi.*;

public class Orchestra {

public static synchronized void main(String[] args) {

Thread[] strumenti = new Thread[3];
strumenti[0] = new Archi("sample\\viola.wav");
strumenti[1] = new Archi("sample\\cello.wav");
strumenti[2] = new Archi("sample\\violino.wav");
for(Thread t:strumenti){
t.start();
}
//you should listen the three audio file one after the other
//but it doesn't work
}

}

问题已解决。线程必须等待音频剪辑完成。

最佳答案

我没有看到你的代码,但是,如果我理解正确的话,你可以在这种情况下使用静态锁。下面的代码适用于单个仪器父级。这段代码使得父级乐器同时只演奏一台乐器,一台演奏完毕后,等待的乐器将一一演奏。

private static boolean canPlay = true;
private static Object playLock = new Object();

@Override
public void run() {
checkPlayable();

try {
// your code
}
finally { // If a exception happens(or not) during the execution of the code block above, lock must be released.
synchronized (playLock) {
canPlay = true; // enable playing for others
playLock.notifyAll(); // wake up others
}
}

}

/*
* This is used to get the lock for the first one to come. Makes other ones wait.
*/
private static void checkPlayable() {
synchronized (playLock) {
while(!canPlay) {
try {
playLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
canPlay = false;
}

}

关于java - 在 Java 中使用同步,如果乐器是同一子类的实例,则不允许它们一起演奏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com