JAVA-递归文件复制,多线程

文件复制,递归复制

import java.io.*;

public class CopyCer {
    public static void main(String[] args) {
        File file = new File("D:/a");
        copys(file);
    }

    public static void copys(File file){
        if (file.isFile()){
            String absolutePath = file.getAbsolutePath();
            String newPath = "C"+absolutePath.substring(1);
            File newFile = new File(newPath);
            File newFilePath = newFile.getParentFile();


            if (!newFilePath.exists()) {
                newFilePath.mkdirs();
            }

            try(FileInputStream fileInputStream = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fileInputStream);

                FileOutputStream fos = new FileOutputStream(newFile);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
            ) {
                int read;
                byte[] bytes = new byte[102400];
                while((read = bis.read(bytes)) != -1){
                    bos.write(bytes, 0, read);
                }

            } catch (Exception e) {
                e.getMessage();
            }

        }else {
            File[] files = file.listFiles();
            for (File eve : files) {
                copys(eve);
            }
        }
    }
}

线程

创建线程

  • 继承Thread类
  • 实现Runnable接口

package com.Thread;

import java.util.function.DoubleToIntFunction;

public class Ticket {
    public static void main(String[] args) {

        Tic01 tic01 = new Tic01();
        Tic01 tic02 = new Tic01();
        Tic01 tic03 = new Tic01();
        tic01.setName("窗口一");
        tic02.setName("窗口二");
        tic03.setName("窗口三");
        tic01.start();
        tic02.start();
        tic03.start();

        //接口方式
       /* Tic02 tic02 = new Tic02();
        new Thread(tic02).start();
        new Thread(tic02).start();
        new Thread(tic02).start();*/
    }
}

class Tic01 extends Thread {
    //多个线程共享tic_num
    private static int tic_num = 100;

    @Override
    public void run() {
        while (true) {
            if (tic_num <= 1) {
//                    System.out.println("over");
                break;
            }
            //sleep 50ms
            synchronized (this) {
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("window " + Thread.currentThread().getName() + " sale one tic " + " 剩余 " + (tic_num--));
        }
    }
}

//使用接口方式解决
class Tic02 implements Runnable {
    private int tic_num = 100;

    @Override
    public void run() {
        while (true) {
            //sleep 50ms
            try {
                Thread.sleep(30);
                if (tic_num <= 0) {
                    System.out.println("over");
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("window " + Thread.currentThread().getName() + " sale one tic " + " and " + (--tic_num));
        }
    }
}

常用方法

  • getName
  • setName       设置名字
  • getPriority     
  • setPriority     设置优先级
  • sleep
  • interrupt
  • Thread.currentThread

优先级

1-10

声明周期

新建

就绪

运行

阻塞

死亡

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值