标签: threadpool

我在哪里创建和使用ScheduledThreadPoolExecutor,TimerTask或Handler?

我需要让我的RSS Feed阅读器每隔10分钟检查一次新帖子,然后在有新帖子的情况下解析它们.我还需要每分钟都更新UI.

我从不同的来源阅读和听到了不同的东西.我目前的理解是我可以ScheduledThreadPoolExecutor用来制作两个预定的线程,其中一个需要Handler更新UI.我不确定这些类或者最有效的使用方法TimerTask.

我也很不确定在哪里制作这些的子类.一位朋友建议TimerTaskFeedParser课堂上扩展为内部课程以使其更简单.但是,要以这种方式实现它,我必须使用该run()方法TimerTask而不重写它,这意味着我不能简单地使用我需要的参数来运行需要的函数.

简而言之,为此安排任务的最佳方法是什么,我将在哪里实现这些?

android handler threadpool timertask scheduledexecutorservice

25
推荐指数
1
解决办法
2万
查看次数

C# - ThreadPool QueueUserWorkItem使用?

就在我现在使用以下代码添加排队的线程.我不喜欢它.而且我的同事也不会因为他们不太了解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<>.


编辑:得到(b)答案和评论,我已经更喜欢它了.

c# multithreading delegates threadpool

25
推荐指数
2
解决办法
6万
查看次数

Python 3:Pool是否保持传递给map的原始数据顺序?

我编写了一个小脚本来分配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)

python multithreading multiprocessing threadpool python-3.x

25
推荐指数
2
解决办法
9323
查看次数

TaskCreationOptions.LongRunning选项和ThreadPool

TPL使用任务计划程序来协调任务.根据官方文档,默认任务调度程序使用线程池,但如果显示TaskCreationOptions.LongRunning选项,则它将为该任务创建专用线程(A).

问题:截至目前,Visual Studio 2010的MSDN文档尚未就绪,当前的在线MSDN尚未最终确定; 有谁知道(A)是真还是假?

c# scheduled-tasks .net-4.0 threadpool task-parallel-library

24
推荐指数
2
解决办法
1万
查看次数

如何使用ThreadPoolExecutor和自定义任务实现PriorityBlockingQueue

我经常搜索,但找不到解决问题的方法.

我有自己的类,BaseTask使用a ThreadPoolExecutor来处理任务.
如果我不想要优先级(即使用a PriorityBlockingQueue),这可以正常工作,但当我尝试使用ClassCastException我得到的ThreadPoolExecutor因为FutureTask将我的任务包装到一个FutureTask对象中.
这显然是可以的,因为Comparable它没有实现newTaskFor(),但我将如何继续解决优先级问题?
我读过您可以覆盖ThreadPoolExecutorBaseTask,但我似乎无法在所有发现这个方法...?

我们欢迎所有的建议!

一些代码可以帮助:

在我的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)

java priority-queue executor futuretask threadpool

24
推荐指数
4
解决办法
2万
查看次数

24
推荐指数
2
解决办法
1万
查看次数

如何管理ruby线程以完成所有工作?

我有一个可以分成独立单元的计算,现在我正在处理的方法是创建一个固定数量的线程,然后在每个线程中分发要完成的工作块.所以在伪代码中这就是它的样子

# 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?在主线程中执行

ruby multithreading threadpool

24
推荐指数
2
解决办法
2万
查看次数

为什么ScheduledExecutorService在抛出异常后不再运行任务?

为了执行定期任务,我查看TimerScheduledThreadPoolExecutor(使用单个线程)并决定使用后者,因为在其中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)

java error-handling threadpool

24
推荐指数
2
解决办法
2万
查看次数

如何从循环外部杀死无限循环中的pthread?

我创建了一个线程,并将其置于无限循环中.使用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)但我仍然有泄漏.

c memory-leaks pthreads threadpool

24
推荐指数
2
解决办法
3万
查看次数

如何在IIS 7.0上增加线程池线程

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线程池限制达到了那个点,需要增加它.

谢谢

asp.net iis-7 threadpool

23
推荐指数
1
解决办法
4万
查看次数