更具体的情况是. run() 函数中,涉及到io操作,本地与远处某服务器进行字节流的传输中,我想停止他们的传输,怎么做?
我试过了stop,destroy等。。发现好像io操作还在继续。
我试过了stop,destroy等。。发现好像io操作还在继续。
解决方案 »
- 请问抽象类与接口的具体区别是什么呀?
- 在设计一个类的时候不会做了,请教下
- 高人们帮忙分析一下这段代码哪里出错了!!!
- 静态初始化方法的代码超出 65535 字节限制
- 为何几个正整数相乘得出结果会是负数?
- 怎样在JAVA中实现判断局域网中哪些机器在线,急
- BASE64转码后生成得字符串由那些字符组成?
- 在命令行下执行时,JDBC连ACCESS,显示没有合适的驱动!(初学JAVA请大哥们多帮忙)
- 比特币 java 报错java.net.SocketException: Unexpected end of file from server
- 有没有相关的代码生成器项目是关于解决代码重用的?
- 如何提高计算机的英文水平呢?
- 对基本语句for,if,break等的问题
比如
XxxThread xt = new XxxThread().join();
这样sleep之类的才会有效.
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);
}
}请问该怎么解决?
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);
}
}-----------------------------------------------------------------------------------请问该怎么解决?
sleep(0);
out.write(in.read());
}
writtingThread.interrupt();
BufferedInputStream 和 BufferedOutputStream 否则传输效率会比较慢
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操作!但为什么不成功?
}