常驻线程的创建--线程不死之谜

  主线程不死是因为主线程里面有一个RunLoop,RunLoop里面有一个do while死循环,保证了程序的不退出

  那么如果我们有一个需求,需要一直在后台进行某个耗时操作,比如检查联网状态,比如扫描用户的某些行为等等.

这时候肯定要在子线程进行,如果能保证一个子线程的不死,就能避免频繁的创建与销毁线程.

  方法:

  模仿主线程不死的操作

1.创建并强引用线程

2.往该线程里添加RunLoop

3.往RunLoop里面添加事务(source,timer,observer),保证RunLoop不退出

4.RunLoop run

这样我们就可以随时调用该线程处理一些任务了,代码如下

#import "ViewController.h"

#import "GQThread.h"

@interface ViewController ()


@property (nonatomic,strong)GQThread * thread; // 重写了dealloc方法,查看线程是够销毁

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   self.thread = [[GQThreadalloc] initWithTarget:selfselector:@selector(threadBegin1)object:nil];

   self.thread.name =@"不死线程";

    [self.threadstart];



}

// threadBegin123都可以让线程不死

- (void)threadBegin1 {

 @autoreleasepool{ //必须的,下面两个方法也应该加上,处理一些autorelease对象

    [[NSRunLoop currentRunLoopaddPort:[NSPortportforMode:NSDefaultRunLoopMode];

    [[NSRunLoopcurrentRunLooprun];

    NSLog(@"threadBegin");//主句代码不会执行了,因为[[NSRunLoopcurrentRunLooprun]一直在跑圈,在RunLoop内部会不断去查看该线程有没有任务要处理,若有,就让它处理一下


}


}

- (void)threadBegin2{

   while (1) {

        [[NSRunLoopcurrentRunLoop] run];

    }

}

- (void)threadBegin3{

    [NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(test)userInfo:nilrepeats:YES];

    

    [[NSRunLoopcurrentRunLoop] run];

    

}

// 自定义的一些任务给该线程执行

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [selfperformSelector:@selector(test)onThread:self.threadwithObject:nilwaitUntilDone:NO];

}

- (void)test

{

    NSLog(@"***********test2*******%@", [NSThreadcurrentThread]);

    

   // NSLog(@"%@", [NSRunLoop currentRunLoop]);

}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以通过多线程来获取数据库数据。一种常见的做法是使用线程池来管理线程,同时启动多个消费者线程来处理数据库查询和结果的处理。 首先,创建一个生产者线程来执行数据库查询操作,将查询结果存放在队列中。这里可以使用Java的线程池来模拟生产者线程的并发执行。 接下来,在主线程中同时启动多个消费者线程,消费者线程从队列中获取查询结果并进行相应的处理。消费者线程常驻线程,当队列中没有数据时就进入阻塞状态等待数据的到来。 具体的代码示例如下: ``` import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class MainThread { public static void main(String[] args) { // 创建线程池 ExecutorService exec = new ThreadPoolExecutor(10, 20, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); for (int i = 0; i < 100; i++) { // 使用线程池模拟生产者生产数据 exec.execute(new ProducerThread()); } for (int i = 0; i < 2; i++) { // 启动两个消费者线程 new ConsumerThread().start(); } } } class ProducerThread extends Thread { public void run() { // 执行数据库查询操作 // 将查询结果存放在队列中 // ... } } class ConsumerThread extends Thread { public void run() { while (!this.isInterrupted()) { try { // 从队列中获取数据进行处理 // ... } catch (InterruptedException e) { e.printStackTrace(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值