1。现在Thread的suspend()和resume()被反对使用了,我以前都一直用的这个,现在改到JDK1.5,不能用这两个函数了,请问,我应该怎样实现上述两个函数的功能?2。我用如下代码实现wav格式的播放
   
   
   //定义
   AudioStream music;   
   try{
       music = new AudioStream(new FileInputStream("1.wav"));
   }
   catch (Exception e){}
   //播放
   try{
      AudioPlayer.player.start(music);
   }
   catch(Exception e){}我把播放的代码放到一个按钮的事件相应里,问题来了,第一次按这个按钮的时候,可以发出声音,第二次按的时候就没有声音了,两次按之间我隔了很久,足够声音放完了,我应该怎么做?是不是应该有个文件流关闭之类的代码?或者哪位有更好的用wav的代码呢?我上面这个代码是从网上搜到的,还有上面的代码编译要
import sun.audio.AudioPlayer;
import java.io.FileInputStream;java比C++难多了!!!

解决方案 »

  1.   

    1. 这是JDK 5.0 documentation说的What should I use instead of Thread.suspend and Thread.resume?
    As with Thread.stop, the prudent approach is to have the "target thread" poll a variable indicating the desired state of the thread (active or suspended). When the desired state is suspended, the thread waits using Object.wait. When the thread is resumed, the target thread is notified using Object.notify. For example, suppose your applet contains the following mousePressed event handler, which toggles the state of a thread called blinker:     private boolean threadSuspended;    Public void mousePressed(MouseEvent e) {
            e.consume();        if (threadSuspended)
                blinker.resume();
            else
                blinker.suspend();  // DEADLOCK-PRONE!        threadSuspended = !threadSuspended;
        }You can avoid the use of Thread.suspend and Thread.resume by replacing the event handler above with: 
        public synchronized void mousePressed(MouseEvent e) {
            e.consume();        threadSuspended = !threadSuspended;        if (!threadSuspended)
                notify();
        }and adding the following code to the "run loop": 
                    synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }The wait method throws the InterruptedException, so it must be inside a try ... catch clause. It's fine to put it in the same clause as the sleep. The check should follow (rather than precede) the sleep so the window is immediately repainted when the the thread is "resumed." The resulting run method follows: 
        public void run() {
            while (true) {
                try {
                    Thread.currentThread().sleep(interval);                synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }
                } catch (InterruptedException e){
                }
                repaint();
            }
        }Note that the notify in the mousePressed method and the wait in the run method are inside synchronized blocks. This is required by the language, and ensures that wait and notify are properly serialized. In practical terms, this eliminates race conditions that could cause the "suspended" thread to miss a notify and remain suspended indefinitely. 
    While the cost of synchronization in Java is decreasing as the platform matures, it will never be free. A simple trick can be used to remove the synchronization that we've added to each iteration of the "run loop." The synchronized block that was added is replaced by a slightly more complex piece of code that enters a synchronized block only if the thread has actually been suspended:                 if (threadSuspended) {
                        synchronized(this) {
                            while (threadSuspended)
                                wait();
                        }
                    }In the absence of explicit synchronization, threadSuspended must be made volatile to ensure prompt communication of the suspend-request. The resulting run method is: 
        private boolean volatile threadSuspended;    public void run() {
            while (true) {
                try {
                    Thread.currentThread().sleep(interval);                if (threadSuspended) {
                        synchronized(this) {
                            while (threadSuspended)
                                wait();
                        }
                    }
                } catch (InterruptedException e){
                }
                repaint();
            }
        }
      

  2.   

    catch (Exception e){}
    捕捉异常后应该要打印一下。
    第二次没有声音应该有抛出异常的:)
      

  3.   

    to yonghar(ohno):没有异常出来 遗憾
    to 725137(2005年不会菜):由于不知道怎样得到music的播放时间长度,所以把close()加到播放的代码下面就完全没有声音出来了谁有好点的代码啊?我很需要,真烦