我需要让我的RSS Feed阅读器每隔10分钟检查一次新帖子,然后在有新帖子的情况下解析它们.我还需要每分钟都更新UI.
我从不同的来源阅读和听到了不同的东西.我目前的理解是我可以ScheduledThreadPoolExecutor用来制作两个预定的线程,其中一个需要Handler更新UI.我不确定这些类或者最有效的使用方法TimerTask.
我也很不确定在哪里制作这些的子类.一位朋友建议TimerTask在FeedParser课堂上扩展为内部课程以使其更简单.但是,要以这种方式实现它,我必须使用该run()方法TimerTask而不重写它,这意味着我不能简单地使用我需要的参数来运行需要的函数.
简而言之,为此安排任务的最佳方法是什么,我将在哪里实现这些?
android handler threadpool timertask scheduledexecutorservice
就在我现在使用以下代码添加排队的线程.我不喜欢它.而且我的同事也不会因为他们不太了解C#.我想要的只是将一个方法排队在一个新线程中执行.
private static void doStuff(string parameter)
{
// does stuff
}
// call (a)
ThreadPool.QueueUserWorkItem(a => doStuff("hello world"));
// call (b)
ThreadPool.QueueUserWorkItem(delegate { doStuff("hello world"); });
Run Code Online (Sandbox Code Playgroud)
那么还有其他用途ThreadPool.QueueUserWorkItem吗?
最好的是另一个1-Line-Call.如果可能,使用Func<>或Action<>.
我编写了一个小脚本来分配4个线程之间的工作负载,并测试结果是否保持有序(关于输入的顺序):
from multiprocessing import Pool
import numpy as np
import time
import random
rows = 16
columns = 1000000
vals = np.arange(rows * columns, dtype=np.int32).reshape(rows, columns)
def worker(arr):
time.sleep(random.random()) # let the process sleep a random
for idx in np.ndindex(arr.shape): # amount of time to ensure that
arr[idx] += 1 # the processes finish at different
# time steps
return arr
# create the threadpool
with Pool(4) as p:
# schedule one map/worker for each row in the original data …Run Code Online (Sandbox Code Playgroud) TPL使用任务计划程序来协调任务.根据官方文档,默认任务调度程序使用线程池,但如果显示TaskCreationOptions.LongRunning选项,则它将为该任务创建专用线程(A).
问题:截至目前,Visual Studio 2010的MSDN文档尚未就绪,当前的在线MSDN尚未最终确定; 有谁知道(A)是真还是假?
c# scheduled-tasks .net-4.0 threadpool task-parallel-library
我经常搜索,但找不到解决问题的方法.
我有自己的类,BaseTask使用a ThreadPoolExecutor来处理任务.
如果我不想要优先级(即使用a PriorityBlockingQueue),这可以正常工作,但当我尝试使用ClassCastException我得到的ThreadPoolExecutor因为FutureTask将我的任务包装到一个FutureTask对象中.
这显然是可以的,因为Comparable它没有实现newTaskFor(),但我将如何继续解决优先级问题?
我读过您可以覆盖ThreadPoolExecutor在BaseTask,但我似乎无法在所有发现这个方法...?
我们欢迎所有的建议!
一些代码可以帮助:
在我的BaseFutureTask班上,我有
private static final BlockingQueue<Runnable> sWorkQueue = new PriorityBlockingQueue<Runnable>();
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
}
};
private static final BaseThreadPoolExecutor sExecutor = new BaseThreadPoolExecutor(
1, Integer.MAX_VALUE, …Run Code Online (Sandbox Code Playgroud) Web和Stack Overflow中有很多地方不鼓励改变ThreadPool线程或TPL 任务的优先级.特别是:
"你无法控制线程池线程的状态和优先级."
"运行时管理线程池.您无法控制线程的调度,也无法更改线程的优先级."
"你不应该改变PoolThread的文化或优先级......或者就像你没有画出或重新装修租赁汽车一样."
"在某些情况下,创建和管理自己的线程而不是使用线程池线程是合适的:(例如...)您需要一个线程具有特定的优先级."
"ThreadPool中的每个线程都以默认优先级运行,而更改ThreadPriority的代码无效."
但是,这样做很简单,调试器显示更改似乎确实存在(只要值可以回读).
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
Run Code Online (Sandbox Code Playgroud)
所以问题是,这个禁忌的具体原因是什么?
我怀疑:这样做会扰乱游泳池的微妙负载平衡假设.但这并不能解释为什么有些消息来源说你无法改变它.
我有一个可以分成独立单元的计算,现在我正在处理的方法是创建一个固定数量的线程,然后在每个线程中分发要完成的工作块.所以在伪代码中这就是它的样子
# main thread
work_units.take(10).each {|work_unit| spawn_thread_for work_unit}
def spawn_thread_for(work)
Thread.new do
do_some work
more_work = work_units.pop
spawn_thread_for more_work unless more_work.nil?
end
end
Run Code Online (Sandbox Code Playgroud)
基本上,一旦创建了初始线程数,每个线程都会完成一些工作,然后继续从工作堆中完成工作,直到没有剩下任何东西.当我在irb中运行时,一切正常,但是当我使用解释器执行脚本时,事情并没有那么好用.我不确定如何使主线程等到所有工作完成.有没有一种很好的方法可以做到这一点,或者我坚持sleep 10 until work_units.empty?在主线程中执行
为了执行定期任务,我查看Timer并ScheduledThreadPoolExecutor(使用单个线程)并决定使用后者,因为在其中reference for Executors.newSingleThreadScheduledExecutor(),它说:
但请注意,如果此单个线程由于在关闭之前执行期间的故障而终止,则在需要执行后续任务时将使用新的线程.
我的计划是使用它作为一个保护措施,以防止我希望监视其他操作的监视程序代码中未被捕获的异常.我想确认并编写下面的测试,这很快就失败了.看来我做错了假设,或者我的测试有些不对劲?
这是代码:
@Test
public void testTimer() {
final AtomicInteger cTries = new AtomicInteger(0);
final AtomicInteger cSuccesses = new AtomicInteger(0);
TimerTask task = new TimerTask() {
@Override
public void run()
{
cTries.incrementAndGet();
if (true) {
throw new RuntimeException();
}
cSuccesses.incrementAndGet();
}
};
/*
Timer t = new Timer();
t.scheduleAtFixedRate(task, 0, 500);
*/
ScheduledExecutorService exe = Executors.newSingleThreadScheduledExecutor();
exe.scheduleAtFixedRate(task, 0, 500, TimeUnit.MILLISECONDS);
synchronized (this) {
try {
wait(3000);
} catch (InterruptedException e) { …Run Code Online (Sandbox Code Playgroud) 我创建了一个线程,并将其置于无限循环中.使用valgrind检查代码时出现内存泄漏.这是我的代码:
#include <pthread.h>
#include <time.h>
void thread_do(void){
while(1){}
}
int main(){
pthread_t th;
pthread_create(&th, NULL, (void *)thread_do, NULL);
sleep(2);
/* I want to kill thread here */
sleep(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以在main中创建一个线程,并且一直运行thread_do().有没有办法在2秒后从主内部杀死它?我试过了两次pthread_detach(th),pthread_cancel(th)但我仍然有泄漏.
Environment: Windows Server 2008 Enterprise, IIS 7.0, ASP.NET 2.0 (CLR), .NET 4.0
我有一个ASP.NET没有页面和没有session(HttpHandler)的应用程序.它是流媒体服务器.我使用两个线程来处理每个请求,因此如果有100个连接的客户端,则使用200个线程.这是一个专用服务器,服务器上没有更多应用程序.
问题是200个客户端连接后(在压力测试下)应用程序拒绝新客户端,但如果我增加application pool(创建一个Web园)的工作线程,那么每个w3wp进程可以有200个新的快乐客户端.
我觉得.NET线程池限制达到了那个点,需要增加它.
谢谢
threadpool ×10
c# ×2
java ×2
.net ×1
.net-4.0 ×1
android ×1
asp.net ×1
c ×1
delegates ×1
executor ×1
futuretask ×1
handler ×1
iis-7 ×1
memory-leaks ×1
pthreads ×1
python ×1
python-3.x ×1
ruby ×1
task ×1
timertask ×1