Giter Site home page Giter Site logo

java11-concurrency-thread-local's Introduction

Build Status

java11-concurrency-thread-local

The main goal of this project is to show basic features of ThreadLocal.

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html
Reference: https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

preface

ThreadLocal - each thread has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
Since Java 1.2.

Garbage collector: Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

  • Conceptually, you can think of a ThreadLocal<T> as holding a Map<Thread,T> that stores the thread-specific values, though this is not how it is actually implemented.
  • The thread-specific values are stored in the Thread object itself; when the thread terminates, the thread-specific values can be garbage collected.
  • @Transational in Spring (or in general - transaction management) is a good example of using ThreadLocal as the entity manager and the connection are both bound to the current thread using ThreadLocal variables - This is convenient in that it reduces the need to pass execution context information into every method.
  • Like global variables, thread-local variables can detract from reusability and introduce hidden couplings among classes, and should therefore be used with care.

project description

  • ThreadId assigns id to thread.
  • MyCallable represents unit work that has to be done by a thread.
    @Override
    public int[] call() {
        return new int[]{ThreadId.get(), ThreadId.get(), ThreadId.get()}; // if ThreadId is OK array should have the same values
    }    
    
  • Main, ThreadIdTest - showcase

tests

  • if tasks are done by one thread, ids should be the same (ids_same_oneThread)
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    
    List<Future<int[]>> futures = executorService.invokeAll(Arrays.asList(
            sharedCallableInstance,
            sharedCallableInstance,
            sharedCallableInstance));
            
    int[] ids1 = futures.get(0).get();
    int[] ids2 = futures.get(1).get();
    int[] ids3 = futures.get(2).get();
    
    assertThat(ids1, is(ids2));
    assertThat(ids2, is(ids3));
    assertThat(ids3, is(ids1));
    
  • if tasks are done by 3 thread, each id should be different (ids_difference_atLeast3Threads)
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    
    List<Future<int[]>> futures = executorService.invokeAll(Arrays.asList(
            sharedCallableInstance,
            sharedCallableInstance,
            sharedCallableInstance));
    
    
    int[] ids1 = futures.get(0).get();
    int[] ids2 = futures.get(1).get();
    int[] ids3 = futures.get(2).get();
    
    assertThat(ids1, not(ids2));
    assertThat(ids2, not(ids3));
    assertThat(ids3, not(ids1));
    
  • we verify that array returned by MyCallable has the same values (ids_array_has_same_values)
    ExecutorService executorService = Executors.newCachedThreadPool();
    
    int[] ids = executorService.submit(sharedCallableInstance).get();
    
    assertThat(ids[0], is(ids[1]));
    assertThat(ids[1], is(ids[2]));
    assertThat(ids[2], is(ids[0]));
    

java11-concurrency-thread-local's People

Contributors

mtumilowicz avatar

Watchers

 avatar

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.