Giter VIP home page Giter VIP logo

scope's Introduction

scope

Build Status Coverage Status Total alerts Language grade: Java Maven Central

对ThreadLocal的高级封装

  • 显示的声明Scope的范围
  • 强类型
  • 可以在线程池中安全的使用,并防止泄露
  • 只支持jdk1.8

Usage

private static final ScopeKey<String> TEST_KEY = allocate();

public void basicUse() {
    runWithNewScope(() -> {
         TEST_KEY.set("abc");
         String result = TEST_KEY.get(); // get "abc"
            
         runAsyncWithCurrentScope(()-> {
             String resultInScope = TEST_KEY.get(); // get "abc"
         }, executor);
    });
}

// 或者声明一个Scope友好的ExecutorService,方法如下:
private static class ScopeThreadPoolExecutor extends ThreadPoolExecutor {

    ScopeThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
            TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    /**
     * same as {@link java.util.concurrent.Executors#newFixedThreadPool(int)}
     */ 
    static ScopeThreadPoolExecutor newFixedThreadPool(int nThreads) {
        return new ScopeThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    /**
     * 只要override这一个方法就可以
     * 所有submit, invokeAll等方法都会代理到这里来
     */
    @Override
    public void execute(Runnable command) {
        Scope scope = getCurrentScope();
        super.execute(() -> runWithExistScope(scope, command::run));
    }
}

private ExecutorService executor = ScopeThreadPoolExecutor.newFixedThreadPool(10);

public void executeTest() {
    runWithNewScope(() -> {
       TEST_KEY.set("abc");
       executor.submit(() -> {
           TEST_KEY.get(); // get abc
       });
    });
}

scope's People

Contributors

hsiafan avatar phantomthief avatar ymwangzq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

scope's Issues

Scope的getCurrentScope()为null

您好,我在使用该工具库时,获取getCurrentScope()时候返回null,想请教这是什么原因呢?具体如下图
image

【BUG】ScopeUtils.supplyAsyncWithCurrentScope method can't pass Scope params

When I use this method(ScopeUtils.supplyAsyncWithCurrentScope ) to pass Scope to a new thread, i find it doesn't work.
This method does not pass the Scope of the current thread correctly. The main reason is that Scope.getCurrentScope() is written in a lamda expression, so it is executed in a new thread.
After simple rewriting, the scope can be passed correctly~
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.