更具体的情况是. run() 函数中,涉及到io操作,本地与远处某服务器进行字节流的传输中,我想停止他们的传输,怎么做?
我试过了stop,destroy等。。发现好像io操作还在继续。

解决方案 »

  1.   

    你在把进程实例化的时候要加上.join()
    比如
    XxxThread xt = new XxxThread().join();
    这样sleep之类的才会有效.
      

  2.   

    还是不成功。具体是这样的,我先写了一个IO相关的进程,如下:
    import java.io.*;
    public class WrittingThread extends Thread
    {
       public WrittingThread(){
    this.start();
    }
       public void run()//这个线程不断地进行IO操作!
       {
           try
           {
    FileInputStream in=new FileInputStream("C:\\Source.avi");
    FileOutputStream out=new FileOutputStream("G:\\Target.avi",true);
    while(in.available()>0)
    out.write(in.read());
           }
           catch (Exception e)
           {
              e.printStackTrace();
           }
        }
    }然后写了一个GUI界面(有一个按钮试图停止以上IO进程):
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class MyUI extends JFrame
    {
       private JButton stopButton = new JButton();
       WrittingThread writtingThread;
       public MyUI()
       {
         try{
    jbInit();
    writtingThread=new WrittingThread();//启动一个IO进程!
            }
         catch (Exception e){
    e.printStackTrace();
           }
       }   private void jbInit() throws Exception
       {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(400, 300));
    stopButton.setText("StopWritting");
    stopButton.setBounds(new Rectangle(115, 75, 175, 25));
    stopButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    stopButton_actionPerformed(e);
    }
    });
    this.getContentPane().add(stopButton, null);
        }
        private void stopButton_actionPerformed(ActionEvent e)//按钮按下,试图停止IO进程,但是实际上,还在继续IO操作!
        {
    System.out.println("Stop Button has been Pressed..");
    writtingThread.interrupt();//停止io操作!但为什么不成功?
        }   public static void main(String[] args)
       {
    MyUI ui=new MyUI();
    ui.setVisible(true);
       }
    }请问该怎么解决?
      

  3.   

    还是不成功。具体是这样的,我先写了一个IO相关的进程,如下:import java.io.*;
    public class WrittingThread extends Thread
    {
       public WrittingThread(){
    this.start();
    }
       public void run()//这个线程不断地进行IO操作!
       {
           try
           {
    FileInputStream in=new FileInputStream("C:\\Source.avi");
    FileOutputStream out=new FileOutputStream("G:\\Target.avi",true);
    while(in.available()>0)//不断IO
        out.write(in.read());
           }
           catch (Exception e)
           {
              e.printStackTrace();
           }
        }
    }----------------------------------------------------------------------------
    然后写了一个GUI界面(有一个按钮试图停止以上IO进程):import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class MyUI extends JFrame
    {
       private JButton stopButton = new JButton();
       WrittingThread writtingThread;
       public MyUI()
       {
         try{
    jbInit();
    writtingThread=new WrittingThread();//启动一个IO进程!
            }
         catch (Exception e){
    e.printStackTrace();
           }
       }   private void jbInit() throws Exception
       {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(400, 300));
    stopButton.setText("StopWritting");
    stopButton.setBounds(new Rectangle(115, 75, 175, 25));
    stopButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    stopButton_actionPerformed(e);
    }
    });
    this.getContentPane().add(stopButton, null);
        }    //按钮按下,试图停止IO进程,但是实际上,还在继续IO操作!
        private void stopButton_actionPerformed(ActionEvent e)
        {
    System.out.println("Stop Button has been Pressed..");
    writtingThread.interrupt();//停止io操作!但为什么不成功?
        }   public static void main(String[] args)
       {
    MyUI ui=new MyUI();
    ui.setVisible(true);
       }
    }-----------------------------------------------------------------------------------请问该怎么解决?
      

  4.   

    while(in.available()>0) {
        sleep(0);
        out.write(in.read());
    }
      

  5.   

    writtingThread.suspend();
    writtingThread.interrupt();
      

  6.   

    还有 最好用下
    BufferedInputStream 和 BufferedOutputStream 否则传输效率会比较慢
      

  7.   

    在WrittingThread 中加个标志 stopflag 初始化 false
    public void run()//这个线程不断地进行IO操作!
    {
           try
           {
    FileInputStream in=new FileInputStream("C:\\Source.avi");
    FileOutputStream out=new FileOutputStream("G:\\Target.avi",true);
    while(in.available()>0)//不断IO
             if(!stopflag)//这里加一个标志----------------------------------
        out.write(in.read());
             else break;
           }
           catch (Exception e)
           {
              e.printStackTrace();
           }
        }
    然后再停止代码中给标志赋值
    private void stopButton_actionPerformed(ActionEvent e)
    {
    System.out.println("Stop Button has been Pressed..");
    writtingThread.setStopflag(true);
             //writtingThread.interrupt();//停止io操作!但为什么不成功?
    }
      

  8.   

    http://xhl.cqwu.net/article.asp?id=123