1.模拟QQ相册上传图片(上传指定文件夹下所有的图片) 多线程上传(每个图片开辟一个子线程)

public class zhuye01 {

    public static void main(String[] args) throws Exception {

       xianchneg xianchneg=new xianchneg();
       xianchneg.start();
    }
    public static void copy() throws Exception {
        File file1 = new File("C:\\Users\\孙申奥\\Desktop\\实训\\D0718\\图片1");
        File file2 = new File("C:\\Users\\孙申奥\\Desktop\\实训\\D0718\\图片2");
        File[] files = file1.listFiles();
        for (int i = 0; i < files.length; i++) {
            FileInputStream in = new FileInputStream(files[i]);
            File file3= new File(file2 ,files[i].getName());//拼接路径
           /* int a=files.length-1;
            int j=i*100/a;
            System.out.println(j+"%");*/
            FileOutputStream out=new FileOutputStream(file3);
            byte[] bytes=new byte[1024];
            int f=0;
            while ((f= in.read(bytes))!=-1){
                out.write(bytes, 0, f);
            }
            out.close();
            in.close();
        }
    }
}
class xianchneg extends Thread{

    @Override
    public void run() {
    zhuye01 zhuye01=new zhuye01();
        for (int i = 0; i <=10; i++) {//因为线程最大到10
            System.out.println(i*10+"%");
            if (i*10==100){
                System.out.println("传输完毕");
            }
            try {
                zhuye01.copy();
                xianchneg.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }



    }
}

在Python中,你可以使用`threading`模块创建线程,并通过`ThreadPoolExecutor`来管理线程池。假设你有两个独立的任务,分别是要显示图片和显示BMP图片,你可以这样做: ```python import os from PIL import Image import threading from concurrent.futures import ThreadPoolExecutor def display_image(path): try: img = Image.open(path) img.show() print(f"Image {path} displayed.") except Exception as e: print(f"Error displaying image: {e}") def display_bmp_image(bmp_path): try: img = Image.open(bmp_path.replace('.jpg', '.bmp')) # 注意这里假设.bmp文件在.jpg文件同目录下 img.show() print(f"BMP image {bmp_path} displayed.") except Exception as e: print(f"Error displaying BMP image: {e}") # 定义图片路径 image_path = 'your_image_path.jpg' bmp_path = 'your_bmp_path.bmp' # 创建线程池 with ThreadPoolExecutor(max_workers=2) as executor: # 创建并提交两个任务 executor.submit(display_image, image_path) executor.submit(display_bmp_image, bmp_path) # 线程池中的任务会被异步执行,主线程不会阻塞 ``` 这段代码首先导入所需的库,然后定义了两个函数`display_image`和`display_bmp_image`,分别用于显示图片和BMP图片。接着创建一个线程池,设置最大工作数为2。最后,提交这两个任务到线程池,当任务完成后,主线程将继续执行。 注意,这里的代码假设BMP图片位于对应的JPG图片的同一目录下,如果实际情况不同,需要相应调整`bmp_path`的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值