标签: threadpool

使用GCD实现线程池

我有一个包含可以并行计算任务的大循环。为此,我决定使用 GCD 编写一个简单的并发线程池,因为我正在 iOS 上工作。

我的线程池看起来相当简单。我将仅附加.m文件,这足以理解我的想法:

#import "iOSThreadPool.h"

@interface iOSThreadPool()
{
    int                                     _timeout;
    int                                     _currentThreadId;
    NSMutableArray<dispatch_queue_t>        *_pool;
    NSMutableArray<dispatch_semaphore_t>    *_semaphores;
    dispatch_group_t                        _group;
}

@end

@implementation iOSThreadPool

- (instancetype)initWithSize:(int)threadsCount tasksCount:(int)tasksCount
{
    self = [super init];
    if (self) {
        _timeout = 2.0;
        _currentThreadId = 0;
        _pool = [NSMutableArray new];
        _semaphores = [NSMutableArray new];
        for (int i = 0; i < threadsCount; i++) {
            dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_BACKGROUND, 0);
            dispatch_queue_t queue = dispatch_queue_create([NSString stringWithFormat:@"com.workerQueue_%d", i].UTF8String, attr);
            [_pool addObject:queue];

            dispatch_semaphore_t sema = …
Run Code Online (Sandbox Code Playgroud)

concurrency multithreading grand-central-dispatch threadpool ios

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

1
推荐指数
1
解决办法
4093
查看次数

C#异步IO:有没有办法确保任务的排序?

我想使用异步io与分布式哈希服务器进行套接字通信.环境是C#3.5,但如果需要可以使用4.0.

假设我发出以下异步命令(伪代码):

socket.set_asynch("FOO","bar");
string rc = socket.get_asynch("FOO");
Run Code Online (Sandbox Code Playgroud)

由于异步io使用系统线程池,因此这两个命令可以在两个不同的线程上运行.我怎样才能确保rc等于"bar"?即在第二个命令发出之前发出第一个命令?

谢谢!

c# sockets asynchronous system threadpool

1
推荐指数
1
解决办法
534
查看次数

C#Threadpool创建UI元素

我正在通过数据绑定更新列表框,我试图插入的一个列是一个复选框.此更新由线程池处理,我能够正确插入数据,复选框除外.当我创建复选框时,它显示xaml而不是checkbox元素.即

System.Windows.Controls.Checkbox内容:IsChecked:False

的定义 NotesReminderViewDetails

private struct NotesRemindersViewDetails
{
    public string NoteReminderID { get; set; }
    public string NoteReminderEnterDate { get; set; }
    public string NoteReminderDueDate { get; set; }
    public string NoteReminderConents { get; set; }
    public CheckBox NoteReminderCompleted { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我用来更新列表视图的代码.NoteReminderType是一个包含所有注释/提醒信息的结构.

NoteReminderType noteType = noteReminder.NoteReminderDetails;

NotesRemindersViewDetails noteReminderDetails = new NotesRemindersViewDetails();
noteReminderDetails.NoteReminderID = noteType.UserFriendlyNoteReminderID.ToString();
noteReminderDetails.NoteReminderEnterDate = noteType.InsertionDate.ToShortDateString();
noteReminderDetails.NoteReminderDueDate = noteType.DueDate.ToShortDateString();
noteReminderDetails.NoteReminderConents = noteType.Description;

listViewNotesReminders.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
    noteReminderDetails.NoteReminderCompleted = new CheckBox();

    listViewNotesReminders.Items.Add(noteReminderDetails);
}));
Run Code Online (Sandbox Code Playgroud)

我需要更改什么才能显示复选框而不是xaml形成线程池线程?

编辑 …

c# wpf multithreading threadpool

1
推荐指数
1
解决办法
668
查看次数

在XP中加载的DLL中使用新的Vista线程池API(XP中未使用的线程池代码)

我们正在生产一个针对Windows 7和XP的DLL.我们希望我们的DLL在Windows 7系统上加载DLL时使用较新的Vista线程池API,而不是在XP系统上加载时.

现在,我们尝试使用操作系统的运行时检测来编译DLL,以确保从未在XP系统上使用Vista API,但由于缺少kernel32.dll中的依赖性,我们仍然无法在Windows XP系统上注册我们的DLL. ..

除了构建DLL的两个单独版本之外,还有其他方法吗?

提前致谢

c++ windows dll winapi threadpool

1
推荐指数
1
解决办法
444
查看次数

懒惰地在一个单独的线程中编译.NET正则表达式

我一直在使用C#正则表达式,它在Web应用程序中被大量用作自定义模板系统的一部分.表达式很复杂,我注意到使用Regex.Compiled选项可以获得真正的性能提升.然而,编译的初始成本在开发期间是恼人的,特别是在迭代单元测试期间(这里提到了这种一般的权衡).

我目前正在尝试的一个解决方案是懒惰的正则表达式编译.我的想法是,我可以通过在一个单独的线程中创建一个正则表达式的编译版本并在准备就绪时将其打包来充分利用这两个世界.

我的问题是:有什么理由说这可能是一个糟糕的想法表现或其他?我问,因为我不确定是否分配跨线程的jitting和程序集加载之类的成本确实有效(尽管它似乎来自我的基准测试).这是代码:

public class LazyCompiledRegex
{
    private volatile Regex _regex;

    public LazyCompiledRegex(string pattern, RegexOptions options)
    {
        if (options.HasFlag(RegexOptions.Compiled)) { throw new ArgumentException("Compiled should not be specified!"); }
        this._regex = new Regex(pattern, options);
        ThreadPool.QueueUserWorkItem(_ =>
        {
            var compiled = new Regex(pattern, options | RegexOptions.Compiled);
            // obviously, the count will never be null. However the point here is just to force an evaluation
            // of the compiled regex so that the cost of loading and jitting the …
Run Code Online (Sandbox Code Playgroud)

c# regex performance jit threadpool

1
推荐指数
1
解决办法
195
查看次数

启动Windows服务时,启动线程.我怎么能做到这一点?

我正在创建一个窗口服务,但是当它启动时,我希望它创建线程来保持ftp站点的池/监视器.我面临的问题是,当我尝试使用while(true){}启动服务时检查新文件然后它应该是ThreadPool.QueueUserWorkItem,该服务在启动时有超时问题.

c# multithreading windows-services threadpool

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

CDI是否重用RequestScoped的代理?

如果我创建一个带注释的bean,@RequestScoped我希望它会为每个新请求实例化一个新的代理实例.

另一方面,每个请求都与其自己的线程相关联.

我的问题是:如果新请求重用以前从池中创建的线程,CDI会重用以前创建的bean/service的代理对象吗?

java java-ee threadpool proxies cdi

1
推荐指数
1
解决办法
203
查看次数

不执行无界线程池执行器中的所有线程的原因是什么

我正在使用ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(20000);
我在ThreadSystem.java类中有两个静态成员:

public static Integer count = 0;
public static Integer rejectedCount = 0;
Run Code Online (Sandbox Code Playgroud)

然后我正在添加线程:

for (i = 0; i < 20001; i++) {
    Runnable worker = new MyThread();
    try {
        executor.execute(worker);
    } catch (RejectedExecutionException ex) {
        rejectedCount++;
    }
}
executor.shutdown();
while (!executor.isTerminated()) {
}
Run Code Online (Sandbox Code Playgroud)

在线程内:

@Override
public void run() {
    ThreadSystem.count++;
    try{   
        Thread.sleep(50);       
    }
    catch(InterruptedException ex){
        Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, ex.getMessage(),ex);
      }
}
Run Code Online (Sandbox Code Playgroud)

我得到的结果表明存在未执行的线程且count变量不等于创建的线程数,尽管rejectedCount引用被拒绝的线程为0:

数:19488
拒绝计数:0

那么还有什么可以向我保证所有线程都会运行,这种情况的原因是什么:count(可运行线程)不等于添加的线程?

java multithreading threadpool threadpoolexecutor java-threads

1
推荐指数
1
解决办法
47
查看次数

为什么线程2不可用?

在下面的小控制台应用程序中,我打印主线程Id和另外5个线程,它打印1,3,4,5,6,7,但不是2.线程2是否可用以及如何生成此数字?

static void Main(string[] args)
    {
        Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}");

        Enumerable.Range(0, 5).ToList().ForEach(f =>
        {
            new Thread(() =>
            {
                Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
            }).Start();

        });
        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

螺纹1螺纹3螺纹4螺纹5螺纹6螺纹7

c# threadpool

1
推荐指数
1
解决办法
54
查看次数