关于线程池得问题!

zytkwlh 2008-12-31 12:07:34
加精
线程池是对线程进行自动管理
我有两个问题
问题1.线程等待时间过长会不会自动放弃?
问题2.某个线程出异常了,会不会堵塞其他线程池中的其他线程的正常操作,该如何处理?
我是刚刚接触这方面的知识,请各位高手给指点,不胜感激
...全文
943 43 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianjiao85 2009-01-09
  • 打赏
  • 举报
回复
windows下,我使用过线程池,总结如下,不一定正确,仅供参考:
1.加入线程池的任务执行CPU时间不能过长,否则跟单线程效果差不多,MSDN里面有几个调度的参数,
BOOL WINAPI QueueUserWorkItem(
__in LPTHREAD_START_ROUTINE Function,
__in_opt PVOID Context,
__in ULONG Flags
);


Parameters
Function [in]
A pointer to the application-defined callback function of type LPTHREAD_START_ROUTINE to be executed by the thread in the thread pool. This value represents the starting address of the thread. This callback function must not call the TerminateThread function.

For more information, see ThreadProc.

Context [in, optional]
A single parameter value to be passed to the thread function.

Flags [in]
The flags that control execution. This parameter can be one or more of the following values.

Value Meaning
WT_EXECUTEDEFAULT
0x00000000
By default, the callback function is queued to a non-I/O worker thread.

The callback function is queued to a thread that uses I/O completion ports, which means they cannot perform an alertable wait. Therefore, if I/O completes and generates an APC, the APC might wait indefinitely because there is no guarantee that the thread will enter an alertable wait state after the callback completes.

WT_EXECUTEINIOTHREAD
0x00000001
The callback function is queued to an I/O worker thread. This thread performs an alertable wait. This is less efficient, so this flag should be used only if the callback generates APCs to the current thread and the APC should be executed after the thread returns to the thread pool.

The callback function is queued as an APC. Be sure to address reentrancy issues if the function performs an alertable wait operation.

WT_EXECUTEINPERSISTENTTHREAD
0x00000080
The callback function is queued to a thread that never terminates. It does not guarantee that the same thread is used each time. This flag should be used only for short tasks or it could affect other timer operations.

Note that currently no worker thread is truly persistent, although worker threads do not terminate if there are any pending I/O requests.

WT_EXECUTELONGFUNCTION
0x00000010
The callback function can perform a long wait. This flag helps the system to decide if it should create a new thread.

WT_TRANSFER_IMPERSONATION
0x00000100
Callback functions will use the current access token, whether it is a process or impersonation token. If this flag is not specified, callback functions execute only with the process token.

Windows XP/2000: This flag is not supported until Windows XP SP2 and Windows Server 2003

我加入线程的任务执行CPU时间比较长,我分别使用了几个Flags 参数测试,发现效果和单线程一样,或许是机器或许是本人程序的问题,
但是线程任务执行CPU时间过长的话就不要使用线程池,线程池适合小CPU时间任务。
2.切忌引发死锁,并保持线程间同步。虽然线程池可以减轻这方面的烦恼,但是本质上他也是多线程。多多注意。
工程菜鸟 2009-01-09
  • 打赏
  • 举报
回复
看你等待用什么了,我记得在等待的时候,sleep不释放对象锁,wait会释放对象锁
工程菜鸟 2009-01-09
  • 打赏
  • 举报
回复
1,应该不会放弃,可能会死锁
2,一个线程出现异常不会影响其他线程,基本上线程都是独立的吧,如果你写了等待其他线程操作的话,可能会有影响,原来研究过的,现在记忆不清晰了,再听听其他高手的解答吧
xiaomaoa 2009-01-09
  • 打赏
  • 举报
回复
了解了
xiaohui5850 2009-01-09
  • 打赏
  • 举报
回复
学习了
xtyccnu 2009-01-08
  • 打赏
  • 举报
回复
学习学习
谢谢楼主啊,辛苦了
xtyccnu 2009-01-08
  • 打赏
  • 举报
回复
学习学习
谢谢楼主啊,辛苦了
aston110119 2009-01-08
  • 打赏
  • 举报
回复
学习学习
看看大家的看法
nifeng370401747 2009-01-08
  • 打赏
  • 举报
回复
第一个线程等待时间过长,不会放弃,可能会死锁,也就是永远不会轮到它执行。
第二个线程池中的某个出现异常,若是未捕获的异常会导致线程结束,应该不会影响到其它的线程吧。
autotyx 2009-01-08
  • 打赏
  • 举报
回复
完全不支持这种多核结构,计算机系统应该像个团队一样,CPU就是主管,其他各处理器就是员工,当然也可以做得像大公司一样,CPU自然是懂事长,单个处理器没有必要做得很复杂,现在社会讲究的是合作,CPU中央处理器,就好像一个国家的中央领导人它也是普通人,这也跟现在的面对对象编程有点像,
liwei222 2009-01-07
  • 打赏
  • 举报
回复
我也想知道
hanxingzidian 2009-01-07
  • 打赏
  • 举报
回复
对于第一个线程等待时间过长,应该不会放弃,但这样会死锁。
第二个线程池中的某个出现异常,线程池中由于是多线程机制,一个线程出现异常不会干涉其它线程的调用和执行,但线程之间相互有关系,就会影响其他线程的调用和执行。
我是这样认为的,我也是新手,不敢保证一定正确。如果有错误,还请给予指证,谢谢,都是学习啊!
sjlianan 2009-01-07
  • 打赏
  • 举报
回复
这个东西比较复杂哦
sjlianan 2009-01-07
  • 打赏
  • 举报
回复
这个东西比较复杂哦
lifj07 2009-01-06
  • 打赏
  • 举报
回复
ding~~~
HuanxueOrSeaty 2009-01-06
  • 打赏
  • 举报
回复
学习
yangguan0104 2009-01-06
  • 打赏
  • 举报
回复
学习中
Technology_advance 2009-01-06
  • 打赏
  • 举报
回复
记得我在操作数据库没用到事务处理的后果 就是在处理数据库并发操作时
就出现了线程池用完 或等待时间过长 因为没一次操作都New 一个SQLConnetion
new 多了就出现问题 非常影响到 性能 所以还是用事务处理
Winner_xu 2009-01-06
  • 打赏
  • 举报
回复
学习了
xflyrmiss 2009-01-06
  • 打赏
  • 举报
回复
mark 学习了
加载更多回复(23)

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧