public class MyClassLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
final String srcPath = args[0];
String desDir = args[1];
String desFileName = srcPath.substring(srcPath.lastIndexOf('\\') + 1);
final String desPath = desDir + File.separator + desFileName;
new Thread(new Runnable() {
FileOutputStream fos = new FileOutputStream(desPath);
FileInputStream fis = new FileInputStream(srcPath);
@Override
public void run() {
// fos=new FileOutputStream(desPath);
for (int i = 0; true; i++) {
try {
cypher(fis, fos);
System.out.println("外部线程");
new Thread(new Runnable() {
@Override
public void run() {
try {
cypher(fis, fos);
System.out.println("内部线程");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
// fis.close();
// fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void cypher(InputStream inputStream, OutputStream outputStream)
throws Exception {
int len = -1;
byte[] bufr = new byte[1024];
while ((len = inputStream.read(bufr)) != -1) {
outputStream.write(bufr, 0, len);
}
}
}
如上代码,内部线程和外部线程都用到了FileOutputStream fos = new FileOutputStream(desPath);
FileInputStream fis = new FileInputStream(srcPath);两个资源,为什么没有导致死锁现象的发生呢?
|