当我们使用Java的Executor服务创建一个线程池并向该线程池提交线程时,这些线程的执行顺序是什么?
我想确保线程先提交,先执行。例如,在下面的代码中,我希望首先执行前 5 个线程,然后执行接下来的 5 个线程,依此类推...
// Create a thread pool of 5 threads.
ScheduledExecutorService exService = Executors.newScheduledThreadPool(5, new ModifiedThreadFactory("ReadThreadPool"));
// Create 100 threads.
MyThread[] threads = createMyThreads(100);
// Submit these 100 threads to thread pool for execution.
for(MyThread thread : threads) {
exService.submit(thread);
}
Run Code Online (Sandbox Code Playgroud)
Java的线程池是否提供了用于此目的的API,或者我们是否需要在我们的末端实现一个FIFO队列来实现此目的。如果 Java 的线程池不提供任何此类功能,我真的很想了解此功能不存在背后的原因,因为它对我来说似乎是一个非常常见的用例。这在技术上是不可能的(我认为这不太可能),还是只是一个失误?
我正在运行一个 ThreadPoolExecutor 程序,它创建 1000 个 url 并发送到执行程序服务。
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:673)
at java.util.concurrent.ThreadPoolExecutor.addThread(ThreadPoolExecutor.java:681)
at java.util.concurrent.ThreadPoolExecutor.addIfUnderCorePoolSize(ThreadPoolExecutor.java:706)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:650)
at MyProgramName.main(MyProgramName.java:175)
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURLURLURLURLRURL Connection reset
Aug 16, 2014 8:46:20 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to …Run Code Online (Sandbox Code Playgroud) ThreadPool.SetMaxThreads(int workerThreads, int completionPortThreads)
Run Code Online (Sandbox Code Playgroud)
SetMaxThreads方法需要设置工作线程和完成端口线程。有什么方法可以只设置最大工作线程并让线程池决定完成端口线程。
如果没有解决方案,当workerThreads计数为6时,端口线程是否有推荐值?
我使用此代码对 SSIS 包进行排队,该代码的执行时间大约为 10 到 20 秒。
系统信息: Intel Core i3-3220 处理器(双核,4 线程)
我正在尝试编写一个非常简单的线程池来了解它们在幕后是如何工作的。不过,我遇到了问题。当我使用condition_variable并调用notify_all()时,它只会唤醒池中的一个线程。
其他一切都很好。我已经排队了 900 个工作,每个工作都有不错的负载。唤醒的一个线程会消耗所有这些作业,然后返回睡眠状态。在下一个循环中,这一切都会再次发生。
问题是只有一个线程完成这项工作!我怎么搞砸了这个模板?
线程池.h:
#pragma once
#include <mutex>
#include <stack>
#include <atomic>
#include <thread>
#include <condition_variable>
class ThreadPool
{
friend void __stdcall ThreadFunc();
public:
static ThreadPool & GetInstance()
{
static ThreadPool sInstance;
return (sInstance);
}
public:
void AddJob(Job * job);
void DoAllJobs();
private:
Job * GetJob();
private:
const static uint32_t ThreadCount = 8;
std::mutex JobMutex;
std::stack<Job *> Jobs;
volatile std::atomic<int> JobWorkCounter;
std::mutex SharedLock;
std::thread Threads[ThreadCount];
std::condition_variable Signal;
private:
ThreadPool();
~ThreadPool();
public:
ThreadPool(ThreadPool const &) = delete;
void operator …Run Code Online (Sandbox Code Playgroud) 这是我的简单代码:
public class Main4 {
public static void main(String[] args) {
System.out.println("Hello from thread: "+Thread.currentThread().getName());
new Game().run();
System.out.println("I am dying ... ");
}
static class Game {
public void run() {
value();
}
private int value() {
int number = 0;
CompletionStage<Void> c = calculate().thenApply(i -> i + 3).thenAccept(i -> System.out.println("I am done, and my value is " + i));
return number;
}
private CompletionStage<Integer> calculate() {
CompletionStage<Integer> completionStage = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
System.out.println("I am in the thread: …Run Code Online (Sandbox Code Playgroud) java multithreading threadpool completable-future completion-stage
我一直在尝试在图像过滤中使用线程池,该方法从图像中取出红色并返回过滤后的图像。当 y 变量达到最大数量时出现错误。我一直在寻找答案,但找不到与此相关的任何内容。
public Color[,] Apply(Color[,] input)
{
int width = input.GetLength(0);
int height = input.GetLength(1);
Color[,] result = new Color[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
ThreadPool.QueueUserWorkItem(state => Work(x, y));
}
}
void Work(int x, int y)
{
var p = input[x, y];
result[x, y] = Color.FromArgb(p.A, 0, p.G, p.B);
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我相信,虽然不太可能,但排队使用的任务有可能Task.Run()最终在主线程上运行。
我担心(在我的 WPF 应用程序中),如果我App.Current.Dispatcher.Invoke()在此任务中使用或类似的,我会导致死锁。
这是一种有效的恐惧吗?如果是这样,有没有办法防止这种情况发生(不检查当前线程*不是*主线程)。
谢谢
谁能解释一下处理器线程、tomcat线程和Java线程之间的区别以及它们之间的关系?
据我所知: 处理器有一定数量的核心。如果我有一台双核机器,它将为我提供 4 个线程。Tomcat 也有许多线程(默认值为 200),Java 也可以有许多线程,每次需要时都可以创建线程,也可以从可以配置的线程池中获取线程池。
我对调用端点时发生的情况的理解是这样的(假设我有双核 -> 4 个线程):
当我开始同时考虑大量请求时,问题就出现了:
我在这里和其他页面上阅读了很多问题,但没有找到一些东西来展示请求的完整生命周期(或者至少使其更清晰)以及多个请求的一些示例(300 个请求示例)具有较少数量的处理器线程) (processorThread -> TomcatThread -> JavaThread -> TomcatThread -> handlerThread ) …
我有一个关于 .NET 线程池的非常具体的问题。
我想说我对线程池有相当的了解,但有一件事仍然让我困惑。
假设我运行一个 Web 应用程序来处理请求,但也通过渲染/编辑上传的媒体来执行大量 CPU 密集型工作。
在应用程序中分离 I/O 和 CPU 密集型任务时,常见的建议是将 CPU 密集型工作分派到 .Net ThreadPool。Task.Run(...)具体来说,这意味着用- 到目前为止一切顺利来调度呼叫。
但是,我确实想知道,如果针对大量请求执行此操作会发生什么。假设数百/数千个,足以给机器带来巨大的压力,甚至到线程池无法再处理的程度。当您的 CPU 无法处理更多线程时,添加更多线程显然只能到此为止。我想说,此时线程池的线程也受 CPU 本身和调度算法的支配。
这对 I/O 绑定异步操作有何影响?
这是否会导致 I/O 绑定的异步操作在执行其延续时遇到困难?假设我们处于一个在 Threadpool 上执行 async/await 延续并丢弃 SynchronizationContext 的运行时环境中,如何确保这些仍然可以正确执行?
线程池是否对哪个线程接收调度优先级做出任何复杂的假设,以确保吞吐量,即使它完全被工作污染?
了解 ASP.Net Core 如何处理这个问题会特别有趣,因为请求处理程序本身就是线程池线程。
例如:当我编写这段代码时,我收到了正确的结果。
boost::asio::thread_pool t(3);
std::vector<int> vec = {10,20,30};
boost::asio::post(t, [&]{ foo(vec[0]);});
boost::asio::post(t, [&]{ foo(vec[1]);});
boost::asio::post(t, [&]{ foo(vec[2]);});
t.join();
Run Code Online (Sandbox Code Playgroud)
但是当我想在 for-cicle 中使用 boost::asio::post 时,我收到错误 Microsoft Visual C++ Runtime Library:“表达式:向量下标超出范围”
boost::asio::thread_pool t(3);
std::vector<int> vec = {10,20,30};
for (int i = 0; i < 3; ++i) {
boost::asio::post(t, [&]{ foo(vec[i]);});
}
t.join();
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让我的最后一种方法返回正确的答案?
threadpool ×10
c# ×4
java ×4
c++ ×2
.net ×1
asp.net-core ×1
boost-asio ×1
memory ×1
processor ×1
std ×1
task ×1
tomcat ×1