不成功。具体是这样的,我先写了一个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);
}
}--------------------------------------------------------------------------------------------------------请问该怎么解决?
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);
}
}--------------------------------------------------------------------------------------------------------请问该怎么解决?
interrupt方法不会停止线程,唯一强制停止的方法就是调用stop方法(虽然改方法标记为deperated)
否则就只能自己控制线程的执行了.
小第不明白还请大虾说详细点~~~~~~~~~~~~~~~~~~~~