当前位置: 技术问答>java相关
大家帮忙看看,问题在哪里?!
来源: 互联网 发布时间:2017-03-11
本文导语: 一段播放midi的代码,在jbuilder7下执行之后,进程并没有象正常结束那样,显示“process finished”,run()函数结束之后,整个线程就应该结束了,为什么不能正常的退出呢?!(我不想在main()函数后面加System.exit(0)来退...
一段播放midi的代码,在jbuilder7下执行之后,进程并没有象正常结束那样,显示“process finished”,run()函数结束之后,整个线程就应该结束了,为什么不能正常的退出呢?!(我不想在main()函数后面加System.exit(0)来退出程序!!)
以下是源代码:
package soundalert;
import javax.sound.midi.*;
import java.io.*;
public class SoundAlert extends Thread {
private Sequence currentSound;
private Sequencer player;
private String songFile;
private float tempo = 1.0F;
private Thread runner;
public void setsongFile(String fileName)
{
songFile=fileName;
}
public String getsongFile()
{
return songFile;
}
public void settempo(float tempo)
{
this.tempo=tempo;
}
public float gettempo()
{
return tempo;
}
public SoundAlert()
{
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void run()
{
try {
File song = new File(songFile);
currentSound = MidiSystem.getSequence(song);
player = MidiSystem.getSequencer();
} catch (Exception ex) {
System.err.println(ex.toString());
}
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {
try {
player.open();
player.setSequence(currentSound);
player.setTempoFactor(tempo);
player.start();
while (player.isRunning()&& runner != null)
{
if(player.getMicrosecondPosition()==player.getMicrosecondLength())
player.stop();
try {
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println(e.toString());
}
}
player.stop();
player.close();
} catch (Exception ex) {
System.out.println(ex.toString());
break;
}
runner=null;
}
System.out.println("End");
}
public static void main(String[] args)
{
SoundAlert sa=new SoundAlert();
sa.setsongFile("e:/camptown.mid");
sa.start();
}
}
以下是源代码:
package soundalert;
import javax.sound.midi.*;
import java.io.*;
public class SoundAlert extends Thread {
private Sequence currentSound;
private Sequencer player;
private String songFile;
private float tempo = 1.0F;
private Thread runner;
public void setsongFile(String fileName)
{
songFile=fileName;
}
public String getsongFile()
{
return songFile;
}
public void settempo(float tempo)
{
this.tempo=tempo;
}
public float gettempo()
{
return tempo;
}
public SoundAlert()
{
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void run()
{
try {
File song = new File(songFile);
currentSound = MidiSystem.getSequence(song);
player = MidiSystem.getSequencer();
} catch (Exception ex) {
System.err.println(ex.toString());
}
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {
try {
player.open();
player.setSequence(currentSound);
player.setTempoFactor(tempo);
player.start();
while (player.isRunning()&& runner != null)
{
if(player.getMicrosecondPosition()==player.getMicrosecondLength())
player.stop();
try {
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println(e.toString());
}
}
player.stop();
player.close();
} catch (Exception ex) {
System.out.println(ex.toString());
break;
}
runner=null;
}
System.out.println("End");
}
public static void main(String[] args)
{
SoundAlert sa=new SoundAlert();
sa.setsongFile("e:/camptown.mid");
sa.start();
}
}
|
按理说,线程运行完成之后就会自已结束的,当player调用close()函数的时候,系统自动释放线程所占用的资源,线程应该在此结束了,至于主线程为什么没有结束,我也不是很清楚了,看一下thinking in java吧,那里讲了一些多线程的东西,也许对你有所帮助!
|
好像player在自己的线程里运行,那个线程并没有结束吧。具体的你查查手册吧,我忘了。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。