QNCFHttpThreadPool.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // QNCFHttpThreadPool.m
  3. // Qiniu
  4. //
  5. // Created by yangsen on 2021/10/13.
  6. //
  7. #import "QNCFHttpThreadPool.h"
  8. #import "QNTransactionManager.h"
  9. @interface QNCFHttpThread()
  10. @property(nonatomic, assign)BOOL isCompleted;
  11. @property(nonatomic, assign)NSInteger operationCount;
  12. @property(nonatomic, strong)NSDate *deadline;
  13. @end
  14. @implementation QNCFHttpThread
  15. + (instancetype)thread {
  16. return [[QNCFHttpThread alloc] init];;
  17. }
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. self.isCompleted = NO;
  21. self.operationCount = 0;
  22. }
  23. return self;
  24. }
  25. - (void)main {
  26. @autoreleasepool {
  27. [super main];
  28. while (!self.isCompleted) {
  29. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
  30. }
  31. }
  32. }
  33. - (void)cancel {
  34. self.isCompleted = YES;
  35. }
  36. @end
  37. @interface QNCFHttpThreadPool()
  38. // 单位:秒
  39. @property(nonatomic, assign)NSInteger threadLiveTime;
  40. @property(nonatomic, assign)NSInteger maxOperationPerThread;
  41. @property(nonatomic, strong)NSMutableArray *pool;
  42. @end
  43. @implementation QNCFHttpThreadPool
  44. + (instancetype)shared {
  45. static QNCFHttpThreadPool *pool = nil;
  46. static dispatch_once_t onceToken;
  47. dispatch_once(&onceToken, ^{
  48. pool = [[QNCFHttpThreadPool alloc] init];
  49. pool.threadLiveTime = 60;
  50. pool.maxOperationPerThread = 1;
  51. pool.pool = [NSMutableArray array];
  52. [pool addThreadLiveChecker];
  53. });
  54. return pool;
  55. }
  56. - (void)addThreadLiveChecker {
  57. QNTransaction *transaction = [QNTransaction timeTransaction:@"CFHttpThreadPool" after:0 interval:1 action:^{
  58. [[QNCFHttpThreadPool shared] checkThreadLive];
  59. }];
  60. [kQNTransactionManager addTransaction:transaction];
  61. }
  62. - (void)checkThreadLive {
  63. @synchronized (self) {
  64. NSArray *pool = [self.pool copy];
  65. for (QNCFHttpThread *thread in pool) {
  66. if (thread.operationCount < 1 && thread.deadline && [thread.deadline timeIntervalSinceNow] < 0) {
  67. [self.pool removeObject:thread];
  68. [thread cancel];
  69. }
  70. }
  71. }
  72. }
  73. - (QNCFHttpThread *)getOneThread {
  74. QNCFHttpThread *thread = nil;
  75. @synchronized (self) {
  76. for (QNCFHttpThread *t in self.pool) {
  77. if (t.operationCount < self.maxOperationPerThread) {
  78. thread = t;
  79. break;
  80. }
  81. }
  82. if (thread == nil) {
  83. thread = [QNCFHttpThread thread];
  84. thread.name = [NSString stringWithFormat:@"com.qiniu.cfclient.%lu", (unsigned long)self.pool.count];
  85. [thread start];
  86. [self.pool addObject:thread];
  87. }
  88. thread.operationCount += 1;
  89. thread.deadline = nil;
  90. }
  91. return thread;
  92. }
  93. - (void)addOperationCountOfThread:(QNCFHttpThread *)thread {
  94. if (thread == nil) {
  95. return;
  96. }
  97. @synchronized (self) {
  98. thread.operationCount += 1;
  99. thread.deadline = nil;
  100. }
  101. }
  102. - (void)subtractOperationCountOfThread:(QNCFHttpThread *)thread {
  103. if (thread == nil) {
  104. return;
  105. }
  106. @synchronized (self) {
  107. thread.operationCount -= 1;
  108. if (thread.operationCount < 1) {
  109. thread.deadline = [NSDate dateWithTimeIntervalSinceNow:self.threadLiveTime];
  110. }
  111. }
  112. }
  113. @end