Java - propagating a threadlocal by wrapping a runnable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












My aim is to use a thread pool together with a thread local. I cannot use InheritableThreadLocal because the thread data is different per user and there a couple of parallel users at any time.



So I need to push the thread local down from the mother thread to the children.



I use guava so I want to get listenable futures back. Below is my implementation. Please look at the execute function where the logic happens.



My question is what could go wrong here?



import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import myorg.MyService;
import myorg.ThreadData;

import com.google.common.util.concurrent.AbstractListeningExecutorService;

public class ListeningDecorator extends AbstractListeningExecutorService
private final ExecutorService delegate;

ListeningDecorator(final ExecutorService delegate)
this.delegate = checkNotNull(delegate);


@Override
public final boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException
return this.delegate.awaitTermination(timeout, unit);


@Override
public final boolean isShutdown()
return this.delegate.isShutdown();


@Override
public final boolean isTerminated()
return this.delegate.isTerminated();


@Override
public final void shutdown()
this.delegate.shutdown();


@Override
public final List<Runnable> shutdownNow()
return this.delegate.shutdownNow();


@Override
public final void execute(final Runnable command)
final ThreadData threadData = MyService.getThreadData();
if (threadData == null)
throw new RuntimeException("threadData is null");

Runnable runnable = new Runnable()

@Override
public void run()
MyService.setThreadData(threadData);
command.run();


;

this.delegate.execute(runnable);








share|improve this question



























    up vote
    3
    down vote

    favorite












    My aim is to use a thread pool together with a thread local. I cannot use InheritableThreadLocal because the thread data is different per user and there a couple of parallel users at any time.



    So I need to push the thread local down from the mother thread to the children.



    I use guava so I want to get listenable futures back. Below is my implementation. Please look at the execute function where the logic happens.



    My question is what could go wrong here?



    import static com.google.common.base.Preconditions.checkNotNull;

    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.TimeUnit;

    import myorg.MyService;
    import myorg.ThreadData;

    import com.google.common.util.concurrent.AbstractListeningExecutorService;

    public class ListeningDecorator extends AbstractListeningExecutorService
    private final ExecutorService delegate;

    ListeningDecorator(final ExecutorService delegate)
    this.delegate = checkNotNull(delegate);


    @Override
    public final boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException
    return this.delegate.awaitTermination(timeout, unit);


    @Override
    public final boolean isShutdown()
    return this.delegate.isShutdown();


    @Override
    public final boolean isTerminated()
    return this.delegate.isTerminated();


    @Override
    public final void shutdown()
    this.delegate.shutdown();


    @Override
    public final List<Runnable> shutdownNow()
    return this.delegate.shutdownNow();


    @Override
    public final void execute(final Runnable command)
    final ThreadData threadData = MyService.getThreadData();
    if (threadData == null)
    throw new RuntimeException("threadData is null");

    Runnable runnable = new Runnable()

    @Override
    public void run()
    MyService.setThreadData(threadData);
    command.run();


    ;

    this.delegate.execute(runnable);








    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      My aim is to use a thread pool together with a thread local. I cannot use InheritableThreadLocal because the thread data is different per user and there a couple of parallel users at any time.



      So I need to push the thread local down from the mother thread to the children.



      I use guava so I want to get listenable futures back. Below is my implementation. Please look at the execute function where the logic happens.



      My question is what could go wrong here?



      import static com.google.common.base.Preconditions.checkNotNull;

      import java.util.List;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.TimeUnit;

      import myorg.MyService;
      import myorg.ThreadData;

      import com.google.common.util.concurrent.AbstractListeningExecutorService;

      public class ListeningDecorator extends AbstractListeningExecutorService
      private final ExecutorService delegate;

      ListeningDecorator(final ExecutorService delegate)
      this.delegate = checkNotNull(delegate);


      @Override
      public final boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException
      return this.delegate.awaitTermination(timeout, unit);


      @Override
      public final boolean isShutdown()
      return this.delegate.isShutdown();


      @Override
      public final boolean isTerminated()
      return this.delegate.isTerminated();


      @Override
      public final void shutdown()
      this.delegate.shutdown();


      @Override
      public final List<Runnable> shutdownNow()
      return this.delegate.shutdownNow();


      @Override
      public final void execute(final Runnable command)
      final ThreadData threadData = MyService.getThreadData();
      if (threadData == null)
      throw new RuntimeException("threadData is null");

      Runnable runnable = new Runnable()

      @Override
      public void run()
      MyService.setThreadData(threadData);
      command.run();


      ;

      this.delegate.execute(runnable);








      share|improve this question













      My aim is to use a thread pool together with a thread local. I cannot use InheritableThreadLocal because the thread data is different per user and there a couple of parallel users at any time.



      So I need to push the thread local down from the mother thread to the children.



      I use guava so I want to get listenable futures back. Below is my implementation. Please look at the execute function where the logic happens.



      My question is what could go wrong here?



      import static com.google.common.base.Preconditions.checkNotNull;

      import java.util.List;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.TimeUnit;

      import myorg.MyService;
      import myorg.ThreadData;

      import com.google.common.util.concurrent.AbstractListeningExecutorService;

      public class ListeningDecorator extends AbstractListeningExecutorService
      private final ExecutorService delegate;

      ListeningDecorator(final ExecutorService delegate)
      this.delegate = checkNotNull(delegate);


      @Override
      public final boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException
      return this.delegate.awaitTermination(timeout, unit);


      @Override
      public final boolean isShutdown()
      return this.delegate.isShutdown();


      @Override
      public final boolean isTerminated()
      return this.delegate.isTerminated();


      @Override
      public final void shutdown()
      this.delegate.shutdown();


      @Override
      public final List<Runnable> shutdownNow()
      return this.delegate.shutdownNow();


      @Override
      public final void execute(final Runnable command)
      final ThreadData threadData = MyService.getThreadData();
      if (threadData == null)
      throw new RuntimeException("threadData is null");

      Runnable runnable = new Runnable()

      @Override
      public void run()
      MyService.setThreadData(threadData);
      command.run();


      ;

      this.delegate.execute(runnable);










      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 8 at 6:48









      Stack Victor

      1294




      1294









      asked Jun 6 at 14:18









      David Michael Gang

      21516




      21516

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195963%2fjava-propagating-a-threadlocal-by-wrapping-a-runnable%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195963%2fjava-propagating-a-threadlocal-by-wrapping-a-runnable%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation