在指定的线程里执行任务

package com.example.foraddapptest;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class ExecutorHelper {
    private final ThreadLocal<Boolean> WORK_THREAD_FLAG = new ThreadLocal<>();
    private final String mThreadNamePre /*= "LearnToolModel-thread-"*/;
    private final Executor mExecutor;

    public ExecutorHelper(String threadNamePre, int nThreads){
        if(threadNamePre == null || nThreads < 1){
            throw new IllegalArgumentException("threadNamePre = " + threadNamePre +
                    ", nThreads = " + nThreads);
        }
        mThreadNamePre = threadNamePre;

        mExecutor = Executors.newFixedThreadPool(nThreads,
            new ThreadFactory() {

                AtomicInteger threadNumber = new AtomicInteger(0);

                @Override
                public Thread newThread(Runnable r) {
                    String threadName = mThreadNamePre + threadNumber.getAndIncrement();
                    Runnable wrapRunner = new Runnable() {
                        @Override
                        public void run() {
                            WORK_THREAD_FLAG.set(true);
                            r.run();
                        }
                    };
                    Thread thread = new Thread(wrapRunner, threadName);
                    //WORK_THREAD_FLAG.set(true);
                    System.out.println("newThread run on " + Thread.currentThread().getName());
                    return thread;
                }
            });

    }

    public void runOnWorkThread(Runnable runnable) {
        if ( isWorkThread() ) {
            runnable.run();
        } else {
            mExecutor.execute(runnable);
        }
    }

    public boolean isWorkThread() {
        Boolean flag = WORK_THREAD_FLAG.get();
        if (flag != null && Boolean.TRUE.equals(flag)) {
            final String threadName = Thread.currentThread().getName();
            if(!threadName.startsWith(mThreadNamePre)){
                System.err.println("isWorkThread() return true, name not match : " +
                        threadName );
            }
            System.out.println("isWorkThread() return true");
            return true;
        }
        System.err.println("isWorkThread() return false, " + Thread.currentThread());
        return false;
    }

    public static void main(String[] args){
        final ExecutorHelper executorHelper = new ExecutorHelper("test-", 1);
        executorHelper.runOnWorkThread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                executorHelper.runOnWorkThread(()->{
                    System.out.println("Thread.currentThread().getName()..1..");
                });

                executorHelper.runOnWorkThread(()->{
                    System.out.println("Thread.currentThread().getName()..2..");
                });

                executorHelper.runOnWorkThread(()->{
                    System.out.println("Thread.currentThread().getName()..3..");
                });
            }
        });

        executorHelper.runOnWorkThread(()->{
            System.out.println("22222222");
        });
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值