• 简单得线程池


    //
    // ThreadPool.h
    /**
    * 继承此类,重写execute方法
    */
    @interface WorkItem : NSObject
    -(void)execute;
    @end

    @class ThreadPoolInner;
    @interface ThreadPool : NSObject {
    ThreadPoolInner
    * _inner;
    }

    -(id)initWithThreadCount:(NSInteger)threadCount;
    -(void)addWorkItem:(WorkItem*)workItem;
    -(void)cancelAllWorkItems;
    -(void)dealloc;

    @end
    //
    // ThreadPool.m
    #import"ThreadPool.h"
    #include
    <pthread.h>

    @interface ThreadPoolInner : NSObject{
    NSMutableArray
    * _workThreads;
    NSInteger _workThreadCount;
    NSCondition
    * _condition;
    NSMutableArray
    * _workItems;
    }
    -(id)initWithThreadCount:(NSInteger)threadCount;
    -(void)pushWorkItem:(WorkItem*)workItem;
    -(WorkItem*)popWorkItem;
    -(void)removeAllWorkItems;
    -(void)dealloc;
    @end

    @interface WorkThread : NSObject {
    ThreadPoolInner
    * _inner;
    NSThread
    * _thread;
    pthread_t _nativeThread;
    volatile BOOL _isRun;
    }
    -(id)initWithThreadPoolInner:(ThreadPoolInner*)inner;
    -(void)run;
    -(void)dispose;
    -(void)dealloc;
    @end

    @implementation WorkItem
    -(void)execute{

    }
    @end

    @implementation ThreadPool

    -(id)initWithThreadCount:(NSInteger)threadCount{
    self
    = [super init];
    if (self) {
    self
    ->_inner = [[ThreadPoolInner alloc] initWithThreadCount:threadCount];
    }
    return self;
    }

    -(void)addWorkItem:(WorkItem*)workItem{
    [_inner pushWorkItem:workItem];
    }

    -(void)cancelAllWorkItems{
    [_inner removeAllWorkItems];
    }

    -(void)dealloc{
    [_inner release];
    [super dealloc];
    }

    @end

    @implementation ThreadPoolInner
    -(id)initWithThreadCount:(NSInteger)threadCount{
    self
    = [super init];
    if (self) {
    self
    ->_workThreads = [[NSMutableArray alloc] initWithCapacity:threadCount];
    self
    ->_workThreadCount = threadCount;
    self
    ->_condition = [[NSCondition alloc] init];
    self
    ->_workItems = [[NSMutableArray alloc] init];
    }
    return self;
    }

    -(void)startWorkThreads{
    if ([_workThreads count] ==0&& _workThreadCount >0) {
    for (NSInteger index =0; index < _workThreadCount; index++) {
    WorkThread
    * workThread = [[WorkThread alloc] initWithThreadPoolInner:self];
    [_workThreads addObject:workThread];
    [workThread release];
    }
    }
    }

    -(void)pushWorkItem:(WorkItem*)workItem{
    [self startWorkThreads];

    [_condition
    lock];
    [_workItems addObject:workItem];
    [_condition broadcast];
    [_condition unlock];
    }

    -(WorkItem*)popWorkItem{
    WorkItem
    * workItem = nil;

    [_condition
    lock];
    if ([_workItems count] >0) {
    workItem
    = [[_workItems objectAtIndex:0] retain];
    [_workItems removeObjectAtIndex:
    0];
    }
    else{
    [_condition wait];
    }
    [_condition unlock];

    return workItem;
    }

    -(void)removeAllWorkItems{
    [_condition
    lock];
    [_workItems removeAllObjects];
    [_condition unlock];
    }

    -(void)disposeWorkThreads{
    for(WorkThread* workThread in _workThreads){
    [workThread dispose];
    }
    }

    -(void)dealloc{
    [self disposeWorkThreads];
    [_workThreads release];
    [_workItems removeAllObjects];
    [_condition release];
    [super dealloc];
    }

    @end

    @implementation WorkThread

    -(id)initWithThreadPoolInner:(ThreadPoolInner*)inner{
    self
    = [super init];
    if (self) {
    self
    ->_inner = inner;
    self
    ->_nativeThread = NULL;

    _thread
    = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    _isRun
    = YES;
    [_thread start];
    }

    return self;
    }

    -(void)run{
    NSAutoreleasePool
    * pool = [[NSAutoreleasePool alloc] init];

    if (!_nativeThread){
    _nativeThread
    = pthread_self();
    }

    while(_isRun){

    WorkItem
    * workItem = [_inner popWorkItem];
    if (workItem) {
    [workItem execute];
    [workItem release];
    }
    }

    [pool drain];
    }

    -(void)dispose{
    _isRun
    = NO;
    pthread_join(_nativeThread, NULL);
    _nativeThread
    = NULL;
    [_thread release];
    _thread
    = nil;
    }

    -(void)dealloc{
    [super dealloc];
    }
    @end
  • 相关阅读:
    四,redis6版本的使用部署
    记录篇-浪潮服务器raid卡
    sudo漏洞解决方案--源码转rpm包(spec文件编写)
    关闭 Chrome 浏览器阅读清单功能
    【转译】如何成为一个数据工程师?
    Python 用最小堆获取大量元素 topk 大个元素
    Python 实现二分查找
    Python 排序算法之堆排序,使用 heapq 实现
    Python 排序算法之归并排序
    Python 排序算法之快速排序
  • 原文地址:https://www.cnblogs.com/ligun123/p/2276516.html
Copyright © 2020-2023  润新知