我想调用一个方法并行.在这个方法中,我计算一些值(非常快)并将此计算的值写入数据库表.
因此我使用Threadpool
ThreadPool.QueueUserWorkItem((state) => {
method();
});
Run Code Online (Sandbox Code Playgroud)
经过一段时间的计算(不确定,发生这种情况的时间变化,有时几分钟后,有时几个小时后),Threadpool已满,但不是使用method()的线程,而是使用我的mysql类的线程电话MySqlCommand.ExecuteNonQuery.
有没有人知道如何避免这种情况?在mysql类的文档中,我发现你无法终止正在执行的查询但是如何避免这样的问题呢?
首先是一点背景.我在NetBeans中收到警告告诉我不要在构造函数中启动新线程.我已经读过它的原因是因为新线程可能会启动并尝试在构造函数实际完成对象之前引用启动线程的对象.
1.)为了实验而不是使用new Thread和thread.start()我试过ExecutorService,我没有得到任何警告. 这是否意味着如果我使用的话,可以在构造函数中创建和启动新线程ExecutorService?
2.)另外,如果我有ExecutorService一个缓存线程池的形式将通过标准方法创建一个新线程new Thread并thread.start()从缓存池中拉出一个线程(或者如果一个线程不可用则导致它创建一个)或者这些线程是否完全独立于缓存的线程池?
我有一个JSF 2.0页面,用户可以在其中登录,他可以选择注销(令人惊讶)。我的JBoss服务器配置最多允许7个线程(连接)。我与一位用户测试了几次页面登录,然后尝试进行7次尝试Transaction not active,这也许意味着注销后连接不会重新回到池中并保持打开状态。
问:注销和返回线程池中的线程的方法是什么?这个问题困扰了我很长时间。请帮忙。
这是我的JBoss中standalone.xml用于限制连接的数据源的配置:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/MyJndiDS" pool-name="MyPoolDS" enabled="true" jta="true" use-java-context="false" use-ccm="true">
<connection-url>
jdbc:postgresql://192.168.2.125:5432/t_report
</connection-url>
<driver>
org.postgresql
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
3
</min-pool-size>
<max-pool-size>
7
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
my_user
</user-name>
<password>
my_pass
</password>
</security>
<statement>
<prepared-statement-cache-size>
32
</prepared-statement-cache-size>
</statement>
</datasource>
...
...
</datasources>
</subsystem>
Run Code Online (Sandbox Code Playgroud)
和在注销方法@SessionScoped类
import javax.faces.context.ExternalContext;
...
...
@Inject ExternalContext ec;
public void validateUserLogOut() {
HttpServletRequest request …Run Code Online (Sandbox Code Playgroud) 我们知道有两种方法可以在Java中创建Thread.
在第二种方法中,我们必须提供run()方法的实现来执行我们的线程逻辑.如果是这样,那么为什么JDK开发人员没有在Thread类中将run()方法作为抽象?
我有兴趣了解其背后的原因,为Thread Class中的run()方法提供默认实现.
我有一些带有@Asynchronous方法的EJB 。
我试图弄清楚当池中配置的所有线程都在处理中并且又出现一个异步调用时会发生什么?
我在这篇文章中找到了一些答案,但它仅适用于 Websphere:
我想知道在 JBoss 上会发生什么,以及是否有一些选项可以像在 Websphere 上那样对线程进行排队。
我像这样配置线程池:
<subsystem xmlns="urn:jboss:domain:ejb3:1.2">
<async thread-pool-name="default"/>
<thread-pools>
<thread-pool name="default">
<max-threads count="10"/>
<keepalive-time time="100" unit="milliseconds"/>
</thread-pool>
</thread-pools>
...
</subsystem>
Run Code Online (Sandbox Code Playgroud)
我试图使用bounded-queue-thread-poolinsinde of <thread-pools>element,但它不起作用。
谢谢你的帮助
我有一个大的计算大致基于以下模式:
def f1(i:Int):Int = ???
def f2(i:Int):Int = ???
def processA(l: List[Int]) =
l.map(i => Future(f1(i)))
def processB(l: List[Int]) = {
val p = processA(l)
p.map(fut => fut.map(f2))
}
def main() = {
val items = List( /* 1k to 10k items here */ )
val results = processB(items)
results.map(_.onComplete ( ... ))
}
Run Code Online (Sandbox Code Playgroud)
如果我的理解是正确的,我遇到的问题是处理是广度优先的.ProcessA启动了数千个期货,然后processB将汇集数千个新的期货,这些期货将在processA完成后处理.onComplete回调将开始很晚才开始...
我想把这个深度优先:过程A的几个未来开始,然后,processB从那里继续而不是切换到队列中的其他东西.
可以在香草scala中完成吗?我应该转向一些替代Futures()和ThreadPools的lib吗?
编辑:更详细一点.f1 andThen f2正如答案中所建议的那样,重写目前是不切实际的.实际上,processA and B正在做一堆其他事情(包括副作用).而processB依赖的事实ProcessA是私人的.如果曝光,它会破坏SoC.
编辑2:我想我会放松一点"香草"约束.有人建议Akka流可以提供帮助.我目前正在看scalaz.Task:有意见吗?
我正在尝试使用线程池并使用选择来实现多线程服务器,因此我已fd_set set全局声明我传递给线程池运行的函数。我收到此错误
在函数 'threadF' 转换为非标量类型请求 fd_set set1=(fd_set) s;
代码是这样的
pool *createPool(int size){
/*...*/
if((err=pthread_create(&id,NULL,&threadF,(void *)&set))!=0){
fprintf(stderr,"thread\n");
exit(errno);
}
/*...*/
}
void *threadF(void* s){
fd_set set1=(fd_set) s;
/*...*/
}
Run Code Online (Sandbox Code Playgroud)
也许我忘记了什么?
我是使用 boost 和线程池的新手。我想将一个函数传递给一个将变量作为参数的线程池。在这个简单的例子中,我只是传入一个整数。我的理解是 post 将调用线程池中的可用线程来执行某些操作。如果我将 printProduct 设置为不接受任何变量,代码就会编译,但这不是我最终想要做的。
参考相关文档会很有帮助(我还没有成功找到很多关于 boost::asio::post 的讨论),以及如何解决问题的建议。谢谢!
#include <cstdlib>
#include <iostream>
#include <pthread.h>
#include <boost/version.hpp> // version is 1.72
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/thread_pool.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>
#include <boost/lockfree/queue.hpp>
using namespace std;
int printProduct(int endval){
// int endval = 1000000;
int prod = 1;
for (int i=0;i<endval;i++){
prod = prod * i;
}
return prod;
}
int main() {
boost::asio::thread_pool tp(8);
for (int i =0; i<200; i++){
// issue is how …Run Code Online (Sandbox Code Playgroud) 大约 50% 的时间,对我的线程池的测试不会引发任何异常并且似乎按预期工作。然而,另外 50% 的时间它会抛出一个std::bad_function_call或double free or corruption (!prev). 我究竟做错了什么?
#include <thread>
#include <iostream>
#include <atomic>
#include <any>
#include <stack>
#include <mutex>
#include <algorithm>
class reusable_thread {
std::thread thread;
std::atomic_bool kill = false;
std::stack<std::function<void(void)>> function_stack;
std::stack<std::function<void(void)>> pending_function_stack;
std::mutex stack_mutex;
std::atomic_size_t num_jobs = 0;
/** Seperate containers for functions and pending_function_stack, so that add_job does not have to be locking **/
inline void transfer_functions() {
std::lock_guard lock(stack_mutex);
while(pending_function_stack.size() != 0) {
function_stack.push(pending_function_stack.top());
pending_function_stack.pop();
}
}
public:
/** …Run Code Online (Sandbox Code Playgroud) 我最近被告知,在
async {
return! async { return "hi" } }
|> Async.RunSynchronously
|> printfn "%s"
Run Code Online (Sandbox Code Playgroud)
嵌套的Async<'T>( async { return 1 }) 不会被发送到线程池进行评估,而在
async {
use ms = new MemoryStream [| 0x68uy; 0x69uy |]
use sr = new StreamReader (ms)
return! sr.ReadToEndAsync () |> Async.AwaitTask }
|> Async.RunSynchronously
|> printfn "%s"
Run Code Online (Sandbox Code Playgroud)
嵌套Async<'T>( sr.ReadToEndAsync () |> Async.AwaitTask) 将是。是什么样的Async<'T>决定何时它像一个异步作业的执行是否在发送到线程池let!或return!?特别是,您将如何定义发送到线程池的一个?您必须在async块中或传入的 lambda 中包含哪些代码Async.FromContinuations?