会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133831个问题
Python 全系列/第七阶段:网页编程基础/jquery 5701楼
Python 全系列/第一阶段:AI驱动的Python编程/控制语句 5702楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 5703楼

package cn.sxt.pool108;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Test02 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// 如何创建一个线程池
		// 1.创建一个线程池,线程池中只有一个线程对象
		 //ExecutorService pool = Executors.newSingleThreadExecutor();
		// 2.创建一个线程池,线程池中有线程的数量固定
		 ExecutorService pool = Executors.newFixedThreadPool(10);
		// 3.创建一个线程池,线程池中的线程的数量可以动态的改变
		//ExecutorService pool = Executors.newCachedThreadPool();
		
		//创建一个集合
		 List<Future> list = new ArrayList<Future>();
		 
		/**使用线程池执行大量的Callable任务*/
		for(int i=0;i<20;i++) {
			//使用匿名内部类
			//创建任务
			Callable<Integer> task = new Callable<Integer>() {

				@Override
				public Integer call() throws Exception {
					Thread.sleep(2000);
					return (int)(Math.random()*10)+1;
				}				
			};//任务结束
			//将任务交给线程池
			Future f = pool.submit(task);
			//每执行完一个任务就将它添加到集合当中去
			list.add(f);
			//System.out.println(f.get());
		}
		System.out.println("ok?");
		//任务全部执行完毕之后再去遍历集合
		for (Future future : list) {
			System.out.println(future.get());
		}
		System.out.println("OKOK!");
		//关闭线程池
		pool.shutdown();
	}
}

问题一:在以上代码中,用线程池中的10个线程去执行,执行System.out.println(f.get());时为什么会一个一个执行呢?

问题二:用线程池中的10个线程去执行,加入ArrayList时,ArrayList是有序的,那是不是实际上还是10个线程同时执行,线程每执行完一个任务就将它添加到集合中去了?还是一下子同时执行完10个任务再一个一个添加到集合中?

问题三:在上一个程序中加入final,是不是为了提高程序的安全性?还是有其他的作用?



JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 5704楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/虚拟环境 5705楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Lombok 5707楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/SQL 语言 5709楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 5711楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql介绍与环境安装 5714楼

Installing requirements for CodeFormer

Traceback (most recent call last):

  File "E:\stable-diffusion-webui\launch.py", line 48, in <module>

    main()

  File "E:\stable-diffusion-webui\launch.py", line 39, in main

    prepare_environment()

  File "E:\stable-diffusion-webui\modules\launch_utils.py", line 389, in prepare_environment

    run_pip(f"install -r \"{os.path.join(repo_dir('CodeFormer'), 'requirements.txt')}\"", "requirements for CodeFormer")

  File "E:\stable-diffusion-webui\modules\launch_utils.py", line 138, in run_pip

    return run(f'"{python}" -m pip {command} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}", live=live)

  File "E:\stable-diffusion-webui\modules\launch_utils.py", line 115, in run

    raise RuntimeError("\n".join(error_bits))

RuntimeError: Couldn't install requirements for CodeFormer.

Command: "E:\stable-diffusion-webui\venv\Scripts\python.exe" -m pip install -r "E:\stable-diffusion-webui\repositories\CodeFormer\requirements.txt" --prefer-binary

Error code: 1

stdout: Looking in indexes: http://mirrors.aliyun.com/pypi/simple/


stderr: WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.

ERROR: Could not find a version that satisfies the requirement addict (from versions: none)

ERROR: No matching distribution found for addict


[notice] A new release of pip available: 22.2.1 -> 23.3.1

[notice] To update, run: E:\stable-diffusion-webui\venv\Scripts\python.exe -m pip install --upgrade pip


请按任意键继续. . .


AIGC人工智能工具全能班 /第五阶段:Stable Diffusion(自选模型)AI绘图/Stable Diffusion本地安装 5715楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637