com.google.bitcoin.utils
Class Threading

java.lang.Object
  extended by com.google.bitcoin.utils.Threading

public class Threading
extends Object

Various threading related utilities. Provides a wrapper around explicit lock creation that lets you control whether bitcoinj performs cycle detection or not. Cycle detection is useful to detect bugs but comes with a small cost. Also provides a worker thread that is designed for event listeners to be dispatched on.


Nested Class Summary
static class Threading.UserThread
           
 
Field Summary
static com.google.common.util.concurrent.CycleDetectingLockFactory factory
           
static Executor SAME_THREAD
          A dummy executor that just invokes the runnable immediately.
static com.google.common.util.concurrent.ListeningExecutorService THREAD_POOL
          A caching thread pool that creates daemon threads, which won't keep the JVM alive waiting for more work.
static Thread.UncaughtExceptionHandler uncaughtExceptionHandler
          An exception handler that will be invoked for any exceptions that occur in the user thread, and any unhandled exceptions that are caught whilst the framework is processing network traffic or doing other background tasks.
static Executor USER_THREAD
          An executor with one thread that is intended for running event listeners on.
 
Constructor Summary
Threading()
           
 
Method Summary
static com.google.common.util.concurrent.CycleDetectingLockFactory.Policy getPolicy()
           
static void ignoreLockCycles()
           
static ReentrantLock lock(String name)
           
static void setPolicy(com.google.common.util.concurrent.CycleDetectingLockFactory.Policy policy)
           
static void throwOnLockCycles()
           
static void waitForUserCode()
          Put a dummy task into the queue and wait for it to be run.
static void warnOnLockCycles()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_THREAD

public static Executor USER_THREAD
An executor with one thread that is intended for running event listeners on. This ensures all event listener code runs without any locks being held. It's intended for the API user to run things on. Callbacks registered by bitcoinj internally shouldn't normally run here, although currently there are a few exceptions.


SAME_THREAD

public static final Executor SAME_THREAD
A dummy executor that just invokes the runnable immediately. Use this over MoreExecutors.sameThreadExecutor() because the latter creates a new object each time in order to implement the more complex ExecutorService interface, which is overkill for our needs.


uncaughtExceptionHandler

@Nullable
public static volatile Thread.UncaughtExceptionHandler uncaughtExceptionHandler
An exception handler that will be invoked for any exceptions that occur in the user thread, and any unhandled exceptions that are caught whilst the framework is processing network traffic or doing other background tasks. The purpose of this is to allow you to report back unanticipated crashes from your users to a central collection center for analysis and debugging. You should configure this before any bitcoinj library code is run, setting it after you started network traffic and other forms of processing may result in the change not taking effect.


factory

public static com.google.common.util.concurrent.CycleDetectingLockFactory factory

THREAD_POOL

public static com.google.common.util.concurrent.ListeningExecutorService THREAD_POOL
A caching thread pool that creates daemon threads, which won't keep the JVM alive waiting for more work.

Constructor Detail

Threading

public Threading()
Method Detail

waitForUserCode

public static void waitForUserCode()
Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all tasks submitted before this point are now completed. Usually you won't want to use this method - it's a convenience primarily used in unit testing. If you want to wait for an event to be called the right thing to do is usually to create a SettableFuture and then call set on it. You can then either block on that future, compose it, add listeners to it and so on.


lock

public static ReentrantLock lock(String name)

warnOnLockCycles

public static void warnOnLockCycles()

throwOnLockCycles

public static void throwOnLockCycles()

ignoreLockCycles

public static void ignoreLockCycles()

setPolicy

public static void setPolicy(com.google.common.util.concurrent.CycleDetectingLockFactory.Policy policy)

getPolicy

public static com.google.common.util.concurrent.CycleDetectingLockFactory.Policy getPolicy()


Copyright © 2014. All rights reserved.