篇一:java中多线程操作文件的复制及剪切
方法,重命名此抽象路径名表示的文件。相当于剪切并且重命名。但是此方法是与平台相关的,重命名操作无法将一个文件从一个文件系统移动到另外一个文件系统。这是导致文件操作失败的原因之一,还有的一个可能原因是:目标路径已经存在此文件。
如果
如果
oldFile和newFile存在于不同的文件系统,则flag会返回false newFIle已经存在,则flag会返回false
在需要跨平台且文件比较大的时候,采用多线程进行文件传输是不错的选择。以下为具体方法:
根据文件大小( x 字节),启动 n 个线程( n = x/ β , β为常量),每个线程 i 以共享方式打开文件,从第 i* β个字节处开始读取。
写入文件同理,先创建 x 大小的文件,然后每个线程往第 i* β个字节处写入。
n 的数量不能太多,常量β的值很重要,是可以配置的,一般设置为 20m。
具体操作代码如下:
篇二:java实验报告多线程异常处理
信 息 工 程 学 院
Java程序设计 实习报告
多线程异常处理
1.实验目的
(1) 掌握Java的异常处理机制及相关实现方法能够在程序设计中熟练运用异常及相关类及
对象;
(2) 掌握Java多线程的概念和实现方法;
(3) 掌握Java多线程的同步问题;
2.实验内容
实验题1 创建两个线程,一个线程打印“A”,另一个线程打印“B”,比如ABBABAABBA….
主要代码:
package package1;
public class TestThread {
}
package package1;
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Printout A_Print = new Printout('A'); Printout B_Print = new Printout('B'); A_Print.start(); B_Print.start(); }
实验报告的内容与格式按任课教师的要求书写。
public class Printout extends Thread {
public char out;
Printout(char temp){
}
} public void run(){ } for(int i=0;i<5;i++){ System.out.print(out); } out = temp;
运行结果:
实验题 2 假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒买一张票。打印出售票过程,注意使用synchronized确保同一张票只能卖出一次。输出格式如下:
第4售票点卖出第100张票
第2售票点卖出第101张票 ??
主要代码:
package package2;
public class SellTicket extends Thread{
int Sellnum; static int Ticketnum = 0; SellTicket(int index){
} Sellnum = index; public void run(){ while(true){ synchronized(this){ if(Ticketnum<1000){ Ticketnum++; System.out.println("第"+Sellnum+"售票点卖出第
"+Ticketnum+"张票");
} } }} } else{ } try{Thread.sleep(500); } catch(InterruptedException e){} break; //return 1;
运行结果:
实验题3 假设某家银行,它可接受顾客的汇款,每做一次汇款,便可计算出汇款
的总额。现有两个顾客,每人都分3次,每次100元将钱到入。试编写一个程序,模拟实际作业。
程序如下:
class CBank
{ private static int sum=0;
public static void add(int n){
int tmp=sum;
tmp=tmp+n;// 累加汇款总额
try{
Thread.sleep((int)(10000*Math.random())); // 小睡几秒钟 }
catch(InterruptedException e){}
sum=tmp;
System.out.println("sum= "+sum);
}
}
class CCustomer extends Thread // CCustomer类,继承自Thread类
{ public void run(){ // run() method
for(int i=1;i<=3;i++)
CBank.add(100); // 将100元分三次汇入
}
}
public class Ex6_1
{public static void main(String args[])
{ CCustomer c1=new CCustomer();
CCustomer c2=new CCustomer();
c1.start();c2.start();
}
}
[基本要求] 运行程序5次,观察每次运行结果是否相同。
运行结果:
结果1 结果2 结果
3
结果4结果5
[思考问题] 程序运行结果每次是否相同,运行时间是否相同,为什么? 答:在add()函数前加synchronized关键字。
② 程序中为什么要使用异常处理?
答:程序调用了sleep()函数,可能被中断,产生异常。
篇三:JAVA 多线程文件拷贝
import java.io.*;
importimportpublicclassFileOperationimplements Runnable{
publicvoid run(){
synchronized(""){
fileCopy("E:\\计算机应用", "C:/Users/Jiangjin/Desktop");}
}
booleanfileCopy(String oldpath,Stringnewpath){
File srcfile=new File(oldpath);//原文件绝对路径
File destfile=new File(newpath+"/"+srcfile.getName());//新文件的文件路劲
if(!srcfile.exists())
returnfalse;
elseif(srcfile.isDirectory()){//如果是文件夹,则进行递归调用 if(!destfile.exists())
destfile.mkdir();
File allfile[]=srcfile.listFiles();
for(File x: allfile){
fileCopy(x.getAbsolutePath(),destfile.getAbsolutePath()); }
}
else {//如果是文件,则复制
try{
if(!destfile.exists()){
destfile.createNewFile();
FileInputStream in=newFileInputStream(srcfile);byte by[]=newbyte[(int)srcfile.length()];in.read(by);
FileOutputStream
out=newFileOutputStream(destfile);
out.write(by);
in.close();
out.close();
}
}catch(Exception e){
e.getMessage();
}
}
System.out.println(System.currentTimeMillis()/1000);returntrue;
}
publicstaticvoid main(String args[]){
System.out.println(System.currentTimeMillis()/1000);FileOperation file=newFileOperation();
file.fileCopy("E:\\计算机应用",
"C:/Users/Jiangjin/Desktop");//将文件复制到桌面
Thread t=newThread(newFileOperation());
t.start();
Thread t1=newThread(newFileOperation());
t1.start();
Thread t2=newThread(newFileOperation());
t2.start();
Thread t3=newThread(newFileOperation());
t3.start();
Thread t4=newThread(newFileOperation());
t4.start();
Thread t5=newThread(newFileOperation());
t5.start();
Thread t6=newThread(newFileOperation());
t6.start();
Thread t7=newThread(newFileOperation());
t7.start();
//System.out.println(System.currentTimeMillis()/1000); }
}
篇四:java多线程中的异常处理
在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己把自己的checked exception处理掉。这一点是通过java.lang.Runnable.run()方法声明(因为此方法声明上没有throw exception部分)进行了约束。但是线程依然有可能抛出unchecked exception,当此类异常跑抛出时,线程就会终结,而对于主线程和其他线程完全不受影响,且完全感知不到某个线程抛出的异常(也是说完全无法catch到这个异常)。JVM的这种设计源自于这样一种理念:“线程是独立执行的代码片断,线程的问题应该由线程自己来解决,而不要委托到外部。”基于这样的设计理念,在Java中,线程方法的异常(无论是checked还是unchecked exception),都应该在线程代码边界之内(run方法内)进行try catch并处理掉.
但如果线程确实没有自己try catch某个unchecked exception,而我们又想在线程代码边界之外(run方法之外)来捕获和处理这个异常的话,java为我们提供了一种线程内发生异常时能够在线程代码边界之外处理异常的回调机制,即Thread对象提供的setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)方法。
通过该方法给某个thread设置一个UncaughtExceptionHandler,可以确保在该线程出现异常时能通过回调UncaughtExceptionHandler接口的public void uncaughtException(Thread t, Throwable e) 方法来处理异常,这样的好处或者说目的是可以在线程代码边界之外(Thread的run()方法之外),有一个地方能处理未捕获异常。但是要特别明确的是:虽然是在回调方法中处理异常,但这个回调方法在执行时依然还在抛出异常的这个线程中!
比之上述方法,还有一种编程上的处理方式可以借鉴,即,有时候主线程的调用方可能只是想知道子线程执行过程中发生过哪些异常,而不一定会处理或是立即处理,那么发起子线程的方法可以把子线程抛出的异常实例收集起来作为一个Exception的List返回给调用方,由调用方来根据异常情况决定如何应对。不过要特别注意的是,此时子线程早以终结。