Class ApfloatContext
- java.lang.Object
-
- org.apfloat.ApfloatContext
-
- All Implemented Interfaces:
java.lang.Cloneable
public class ApfloatContext extends java.lang.Object implements java.lang.CloneableThis class encapsulates the information needed by the apfloat implementation to perform computations.All environment related settings of an apfloat implementation are accessed through an
ApfloatContext. Such settings include for example the implementation provider class, maximum memory size to be used, and the file names that are used for temporary files.For performance reasons, access to an
ApfloatContextis not synchronized. Presumably, this won't be a problem in most cases. But, if the code needs to concurrently modify and access an ApfloatContext, all access to it should be externally synchronized.At simplest, there is just one
ApfloatContext, the global apfloat context. All settings in your application are retrieved through it. The global context is created when theApfloatContextclass is loaded, and it's thus always available.Values for the different settings in the global apfloat context are specified in the
apfloat.propertiesfile, found in the class path. Since they are loaded via aResourceBundlenamed "apfloat", you can alternatively deploy aResourceBundleclass named "apfloat" in your class path to avoid having a .properties file, or to define properties dynamically at run time.The different settings that can be specified in the
apfloat.propertiesfile are as follows:builderFactory, name of the class set as insetBuilderFactory(BuilderFactory)defaultRadix, set as insetDefaultRadix(int)maxMemoryBlockSize, set as insetMaxMemoryBlockSize(long)cacheL1Size, set as insetCacheL1Size(int)cacheL2Size, set as insetCacheL2Size(int)cacheBurst, set as insetCacheBurst(int)memoryThreshold, set as insetMemoryThreshold(long)shredMemoryTreshold, set as insetSharedMemoryTreshold(long)blockSize, set as insetBlockSize(int)numberOfProcessors, set as insetNumberOfProcessors(int)filePath, set as insetProperty(String,String)with property nameFILE_PATHfileInitialValue, set as insetProperty(String,String)with property nameFILE_INITIAL_VALUEfileSuffix, set as insetProperty(String,String)with property nameFILE_SUFFIXcleanupAtExit, set as insetCleanupAtExit(boolean)
It is also possible to override the settings in
apfloat.propertieswith system properties. They can be defined with the property names listed above, prefixed with"apfloat.".An example
apfloat.propertiesfile could contain the following:builderFactory=org.apfloat.internal.IntBuilderFactory defaultRadix=10 maxMemoryBlockSize=50331648 cacheL1Size=8192 cacheL2Size=262144 cacheBurst=32 memoryThreshold=65536 sharedMemoryTreshold=65536 blockSize=65536 numberOfProcessors=1 filePath= fileInitialValue=0 fileSuffix=.ap cleanupAtExit=true
A system property could be used to override any of the above, e.g. by setting on the command line"-Dapfloat.defaultRadix=11".The total memory size and the number of processors are detected automatically, as reported by the Java runtime, if they are not specified in the configuration bundle.
If you need to create a complex multithreaded application that performs apfloat calculations in parallel using multiple threads, you may need to change the ApfloatContext settings for the different working threads.
If thread-specific apfloat contexts are not specified, all threads will use the global context. To set a thread specific context, you would typically create a
clone()of the global (or other parent) context, and then set that context to the thread usingsetThreadContext(ApfloatContext,Thread). Note that if you do not create a clone of the context, the same context will still be used, since it's passed by reference.To optimize thread usage while waiting for another thread in a multithreaded application it's recommended to use
wait(Future)instead of just e.g.Future.get(). This allows threads that the library uses to perform useful work (instead of just being idle) while waiting for theFutureto complete.Typically you may need to set the following properties for each thread:
setNumberOfProcessors(int): Since the number of physical processors available is fixed, you may want to limit the amount of processors each thread can use. In many cases you will want each thread to use exactly one processor, and create as many threads as there are processors.setMaxMemoryBlockSize(long): The physical memory is global and its amount is fixed as well. Since all threads share the global memory, you may want to limit the maximum amount of memory each thread can use. If you do this, you will probably just split the amount of memory between the threads, e.g. by dividing it equally. In this case you should set each thread to have a separate shared memory lock withsetSharedMemoryLock(Object). In this solution all threads can allocate their maximum allowed memory block at the same time, and still the VM won't run out of memory.
Another possibility is to set the whole global memory size as the maximum available for each thread, and use the same shared memory lock for every thread. This is actually the default behavior, if you don't callsetMaxMemoryBlockSize(long)norsetSharedMemoryLock(Object). This way all threads can access the maximum amount of physical memory available. The drawback is that the threads will synchronize on the same memory block, so only one thread can use it at a time. This can have a major effect on performance, if threads are idle, waiting to acquire the shared memory lock for most of the time. To work around this, some mechanism can be set up for pooling the threads competing for the same lock, and executing the task using parallel threads from the thread pool. For example the default apfloat multiplication algorithm uses such a mechanism. Note that synchronization against the shared memory lock will be used for all data blocks larger than the shared memory threshold (seegetSharedMemoryTreshold()).setFilenameGenerator(FilenameGenerator): When you clone an ApfloatContext, the filename generator is by default shared. For most situations this is fine. If you for some reason want to separate the files generated in each thread, you can just set a new FilenameGenerator for each thread. In this case it's essential to configure the FilenameGenerators not to generate conflicting file names. You can do this easily by specifying a different directory or file suffix for each filename generator, or by specifying a different starting value for the file names (e.g. 1000000, 2000000, 3000000, ...).
Setting the filename generator may also be relevant, if you use a distributed computing platform where separate physical machines (or at least separate VM processes) create temporary files in the same shared disk storage. In this case it's also essential to configure the different processes so that they do not generate conflicting file names.
The other settings are generally global and do not typically need to be set differently for each thread.
Unfortunately, Java doesn't allow detecting automatically many of the settings, such as cache sizes. Also, for optimal performance, it would usually be desirable to set each thread's processor affinity (which physical processor runs which thread), which is also not possible. If these features are added to the Java platform in the future, they may be added to the
ApfloatContextAPI as well.- Version:
- 1.14.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classApfloatContext.CleanupThread
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object>attributesstatic java.lang.StringBLOCK_SIZEProperty name for specifying the I/O block size.private intblockSizestatic java.lang.StringBUILDER_FACTORYProperty name for specifying the apfloat builder factory class.private BuilderFactorybuilderFactorystatic java.lang.StringCACHE_BURSTProperty name for specifying the level 1 cache burst size.static java.lang.StringCACHE_L1_SIZEProperty name for specifying the level 1 cache size.static java.lang.StringCACHE_L2_SIZEProperty name for specifying the level 2 cache size.private intcacheBurstprivate intcacheL1Sizeprivate intcacheL2Sizestatic java.lang.StringCLEANUP_AT_EXITProperty name for specifying if clean-up should be done at program exit.private ApfloatContext.CleanupThreadcleanupThreadstatic java.lang.StringDEFAULT_RADIXProperty name for specifying the default radix.private static java.util.concurrent.ExecutorServicedefaultExecutorServiceprivate static java.util.PropertiesdefaultPropertiesprivate intdefaultRadixprivate java.util.concurrent.ExecutorServiceexecutorServicestatic java.lang.StringFILE_INITIAL_VALUEProperty name for specifying the temporary file initial value.static java.lang.StringFILE_PATHProperty name for specifying the temporary file path.static java.lang.StringFILE_SUFFIXProperty name for specifying the temporary file suffix.private FilenameGeneratorfilenameGeneratorprivate static ApfloatContextglobalContextstatic java.lang.StringMAX_MEMORY_BLOCK_SIZEProperty name for specifying the maximum memory block size.private longmaxMemoryBlockSizestatic java.lang.StringMEMORY_THRESHOLDProperty name for specifying the apfloat memory threshold.static java.lang.StringMEMORY_TRESHOLDDeprecated.UseMEMORY_THRESHOLD.private longmemoryThresholdstatic java.lang.StringNUMBER_OF_PROCESSORSProperty name for specifying the number of processors available.private intnumberOfProcessorsprivate java.util.Propertiespropertiesstatic java.lang.StringSHARED_MEMORY_TRESHOLDProperty name for specifying the apfloat shared memory threshold.private java.lang.ObjectsharedMemoryLockprivate longsharedMemoryTresholdprivate static java.util.Map<java.lang.Thread,ApfloatContext>threadContexts
-
Constructor Summary
Constructors Constructor Description ApfloatContext(java.util.Properties properties)Create a new ApfloatContext using the specified properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcheckInterrupted()Checks if the current thread was interrupted.static voidclearThreadContexts()Removes all thread-specific ApfloatContexts.java.lang.Objectclone()Creates a copy of this object.java.lang.ObjectgetAttribute(java.lang.String name)Get an arbitrary object as an attribute for this ApfloatContext.java.util.Enumeration<java.lang.String>getAttributeNames()Get names of all attributes for this ApfloatContext.intgetBlockSize()Get the I/O block size.BuilderFactorygetBuilderFactory()Get the BuilderFactory.intgetCacheBurst()Get the level 1 cache burst size.intgetCacheL1Size()Get the level 1 cache size.intgetCacheL2Size()Get the level 2 cache size.booleangetCleanupAtExit()Get if clean-up should be performed at the time the program exits.static ApfloatContextgetContext()Get the ApfloatContext for the calling thread.static java.util.concurrent.ExecutorServicegetDefaultExecutorService()Returns a new instance of a default ExecutorService.intgetDefaultRadix()Get the default radix.java.util.concurrent.ExecutorServicegetExecutorService()Get the ExecutorService.FilenameGeneratorgetFilenameGenerator()Get the FilenameGenerator.static ApfloatContextgetGlobalContext()Get the global ApfloatContext.longgetMaxMemoryBlockSize()Get the maximum memory block size.longgetMemoryThreshold()Get the memory threshold.intgetMemoryTreshold()Deprecated.UsegetMemoryThreshold().intgetNumberOfProcessors()Get the number of processors that should be used for parallel calculations.java.util.PropertiesgetProperties()Get the values of all properties as strings.java.lang.StringgetProperty(java.lang.String propertyName)Get the value of a property as string.java.lang.StringgetProperty(java.lang.String propertyName, java.lang.String defaultValue)Get the value of a property as string, with the provided default value if the property is not set.java.lang.ObjectgetSharedMemoryLock()Get the shared memory lock object.longgetSharedMemoryTreshold()Get the shared memory threshold.static ApfloatContextgetThreadContext()Get the thread-specific ApfloatContext for the calling thread.static ApfloatContextgetThreadContext(java.lang.Thread thread)Get the thread-specific ApfloatContext for the specified thread.static java.util.PropertiesloadProperties()Loads properties from a properties file or resource bundle.private static java.util.PropertiesloadSystemOverrides(java.util.Properties properties)java.lang.ObjectremoveAttribute(java.lang.String name)Remove an attribute from this ApfloatContext.static voidremoveThreadContext()Removes the thread-specific context for the current thread.static voidremoveThreadContext(java.lang.Thread thread)Removes the thread-specific context for the specified thread.java.lang.ObjectsetAttribute(java.lang.String name, java.lang.Object value)Set an arbitrary object as an attribute for this ApfloatContext.voidsetBlockSize(int blockSize)Set the efficient I/O block size in bytes for this context.voidsetBuilderFactory(BuilderFactory builderFactory)Set the BuilderFactory.voidsetCacheBurst(int cacheBurst)Set the L1 cache burst block size in bytes.voidsetCacheL1Size(int cacheL1Size)Set the L1 cache size in bytes.voidsetCacheL2Size(int cacheL2Size)Set the L2 cache size in bytes.voidsetCleanupAtExit(boolean cleanupAtExit)Set if clean-up should be performed at the time the program exits.voidsetDefaultRadix(int radix)Set the default radix.voidsetExecutorService(java.util.concurrent.ExecutorService executorService)Set the ExecutorService.voidsetFilenameGenerator(FilenameGenerator filenameGenerator)Set the FilenameGenerator.voidsetMaxMemoryBlockSize(long maxMemoryBlockSize)Set the maximum allowed memory block size in bytes.voidsetMemoryThreshold(long memoryThreshold)Set the maximum size of apfloats in bytes that are stored in memory within this context.voidsetMemoryTreshold(int memoryThreshold)Deprecated.voidsetNumberOfProcessors(int numberOfProcessors)Set the number of processors available to parallel calculations using this context.voidsetProperties(java.util.Properties properties)Set the values of all properties as strings.voidsetProperty(java.lang.String propertyName, java.lang.String propertyValue)Set the value of a property as string.voidsetSharedMemoryLock(java.lang.Object lock)Set the shared memory lock object.voidsetSharedMemoryTreshold(long sharedMemoryTreshold)Set the maximum size of apfloats in bytes that can be used without synchronizing against the shared memory lock.static voidsetThreadContext(ApfloatContext threadContext)Set the thread-specific ApfloatContext for the calling thread.static voidsetThreadContext(ApfloatContext threadContext, java.lang.Thread thread)Set the thread-specific ApfloatContext for the specified thread.voidwait(java.util.concurrent.Future<?> future)While waiting for aFutureto be completed, do some useful work instead of just being idle.
-
-
-
Field Detail
-
BUILDER_FACTORY
public static final java.lang.String BUILDER_FACTORY
Property name for specifying the apfloat builder factory class.- See Also:
- Constant Field Values
-
DEFAULT_RADIX
public static final java.lang.String DEFAULT_RADIX
Property name for specifying the default radix.- See Also:
- Constant Field Values
-
MAX_MEMORY_BLOCK_SIZE
public static final java.lang.String MAX_MEMORY_BLOCK_SIZE
Property name for specifying the maximum memory block size.- See Also:
- Constant Field Values
-
CACHE_L1_SIZE
public static final java.lang.String CACHE_L1_SIZE
Property name for specifying the level 1 cache size.- See Also:
- Constant Field Values
-
CACHE_L2_SIZE
public static final java.lang.String CACHE_L2_SIZE
Property name for specifying the level 2 cache size.- See Also:
- Constant Field Values
-
CACHE_BURST
public static final java.lang.String CACHE_BURST
Property name for specifying the level 1 cache burst size.- See Also:
- Constant Field Values
-
MEMORY_THRESHOLD
public static final java.lang.String MEMORY_THRESHOLD
Property name for specifying the apfloat memory threshold.- See Also:
- Constant Field Values
-
MEMORY_TRESHOLD
@Deprecated public static final java.lang.String MEMORY_TRESHOLD
Deprecated.UseMEMORY_THRESHOLD.Property name for specifying the apfloat memory threshold.- See Also:
- Constant Field Values
-
SHARED_MEMORY_TRESHOLD
public static final java.lang.String SHARED_MEMORY_TRESHOLD
Property name for specifying the apfloat shared memory threshold.- See Also:
- Constant Field Values
-
BLOCK_SIZE
public static final java.lang.String BLOCK_SIZE
Property name for specifying the I/O block size.- See Also:
- Constant Field Values
-
NUMBER_OF_PROCESSORS
public static final java.lang.String NUMBER_OF_PROCESSORS
Property name for specifying the number of processors available.- See Also:
- Constant Field Values
-
FILE_PATH
public static final java.lang.String FILE_PATH
Property name for specifying the temporary file path.- See Also:
- Constant Field Values
-
FILE_INITIAL_VALUE
public static final java.lang.String FILE_INITIAL_VALUE
Property name for specifying the temporary file initial value.- See Also:
- Constant Field Values
-
FILE_SUFFIX
public static final java.lang.String FILE_SUFFIX
Property name for specifying the temporary file suffix.- See Also:
- Constant Field Values
-
CLEANUP_AT_EXIT
public static final java.lang.String CLEANUP_AT_EXIT
Property name for specifying if clean-up should be done at program exit.- See Also:
- Constant Field Values
-
globalContext
private static ApfloatContext globalContext
-
threadContexts
private static java.util.Map<java.lang.Thread,ApfloatContext> threadContexts
-
defaultProperties
private static java.util.Properties defaultProperties
-
defaultExecutorService
private static java.util.concurrent.ExecutorService defaultExecutorService
-
builderFactory
private volatile BuilderFactory builderFactory
-
filenameGenerator
private volatile FilenameGenerator filenameGenerator
-
defaultRadix
private volatile int defaultRadix
-
maxMemoryBlockSize
private volatile long maxMemoryBlockSize
-
cacheL1Size
private volatile int cacheL1Size
-
cacheL2Size
private volatile int cacheL2Size
-
cacheBurst
private volatile int cacheBurst
-
memoryThreshold
private volatile long memoryThreshold
-
sharedMemoryTreshold
private volatile long sharedMemoryTreshold
-
blockSize
private volatile int blockSize
-
numberOfProcessors
private volatile int numberOfProcessors
-
cleanupThread
private volatile ApfloatContext.CleanupThread cleanupThread
-
properties
private volatile java.util.Properties properties
-
sharedMemoryLock
private volatile java.lang.Object sharedMemoryLock
-
executorService
private volatile java.util.concurrent.ExecutorService executorService
-
attributes
private volatile java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> attributes
-
-
Constructor Detail
-
ApfloatContext
public ApfloatContext(java.util.Properties properties) throws ApfloatConfigurationExceptionCreate a new ApfloatContext using the specified properties.- Parameters:
properties- The properties for the ApfloatContext.- Throws:
ApfloatConfigurationException- If a property value can't be converted to the correct type.
-
-
Method Detail
-
getContext
public static ApfloatContext getContext()
Get the ApfloatContext for the calling thread. If a thread-specific context has not been specified, the global context is returned.- Returns:
- The ApfloatContext for the calling thread.
-
getGlobalContext
public static ApfloatContext getGlobalContext()
Get the global ApfloatContext.- Returns:
- The global ApfloatContext.
-
getThreadContext
public static ApfloatContext getThreadContext()
Get the thread-specific ApfloatContext for the calling thread.- Returns:
- The ApfloatContext for the calling thread, or
nullif one has not been specified.
-
getThreadContext
public static ApfloatContext getThreadContext(java.lang.Thread thread)
Get the thread-specific ApfloatContext for the specified thread.- Parameters:
thread- The thread whose ApfloatContext is to be returned.- Returns:
- The ApfloatContext for the specified thread, or
nullif one has not been specified.
-
setThreadContext
public static void setThreadContext(ApfloatContext threadContext)
Set the thread-specific ApfloatContext for the calling thread.- Parameters:
threadContext- The ApfloatContext for the calling thread.
-
setThreadContext
public static void setThreadContext(ApfloatContext threadContext, java.lang.Thread thread)
Set the thread-specific ApfloatContext for the specified thread.- Parameters:
threadContext- The ApfloatContext for the specified thread.thread- The thread whose ApfloatContext is to be set.
-
removeThreadContext
public static void removeThreadContext()
Removes the thread-specific context for the current thread.
-
removeThreadContext
public static void removeThreadContext(java.lang.Thread thread)
Removes the thread-specific context for the specified thread.- Parameters:
thread- The thread whose ApfloatContext is to be removed.
-
clearThreadContexts
public static void clearThreadContexts()
Removes all thread-specific ApfloatContexts.
-
getBuilderFactory
public BuilderFactory getBuilderFactory()
Get the BuilderFactory.- Returns:
- The BuilderFactory for this ApfloatContext.
-
setBuilderFactory
public void setBuilderFactory(BuilderFactory builderFactory)
Set the BuilderFactory.- Parameters:
builderFactory- The BuilderFactory for this ApfloatContext.
-
getFilenameGenerator
public FilenameGenerator getFilenameGenerator()
Get the FilenameGenerator.- Returns:
- The FilenameGenerator for this ApfloatContext.
-
setFilenameGenerator
public void setFilenameGenerator(FilenameGenerator filenameGenerator)
Set the FilenameGenerator.- Parameters:
filenameGenerator- The FilenameGenerator for this ApfloatContext.
-
getDefaultRadix
public int getDefaultRadix()
Get the default radix.- Returns:
- The default radix for this ApfloatContext.
-
setDefaultRadix
public void setDefaultRadix(int radix)
Set the default radix. The default value is 10.- Parameters:
radix- The default radix for this ApfloatContext.
-
getMaxMemoryBlockSize
public long getMaxMemoryBlockSize()
Get the maximum memory block size.- Returns:
- The maximum memory block size.
- See Also:
setMaxMemoryBlockSize(long)
-
setMaxMemoryBlockSize
public void setMaxMemoryBlockSize(long maxMemoryBlockSize)
Set the maximum allowed memory block size in bytes. Apfloat will allocate an array at most of this size for calculations using this context. The minimum value for this setting is 65536.If you set the value of this parameter too low, performance will suffer greatly as data is unnecessarily paged to disk. If you set this value too high, your application can crash with an
OutOfMemoryError.The default value for this setting is 80% of the total memory available to the VM at application startup, as reported by
Runtime.totalMemory(), rounded down to the nearest power of two or three times a power of two.- Parameters:
maxMemoryBlockSize- Maximum allocated memory block size in bytes.
-
getCacheL1Size
public int getCacheL1Size()
Get the level 1 cache size.- Returns:
- The level 1 cache size.
- See Also:
setCacheL1Size(int)
-
setCacheL1Size
public void setCacheL1Size(int cacheL1Size)
Set the L1 cache size in bytes. The minimum value for this setting is 512.This setting has a minor performance impact on some memory intensive operations. Unless you really want to tweak the performance, it's better to not touch this setting.
The default value for this setting is 8kB.
- Parameters:
cacheL1Size- The level 1 cache size in bytes.
-
getCacheL2Size
public int getCacheL2Size()
Get the level 2 cache size.- Returns:
- The level 2 cache size.
- See Also:
setCacheL2Size(int)
-
setCacheL2Size
public void setCacheL2Size(int cacheL2Size)
Set the L2 cache size in bytes. The minimum value for this setting is 2048. This setting has a minor performance impact on some memory intensive operations. Unless you really want to tweak the performance, it's better to not touch this setting.The default value for this setting is 256kB.
- Parameters:
cacheL2Size- The level 2 cache size in bytes.
-
getCacheBurst
public int getCacheBurst()
Get the level 1 cache burst size.- Returns:
- The cache burst size.
- See Also:
setCacheBurst(int)
-
setCacheBurst
public void setCacheBurst(int cacheBurst)
Set the L1 cache burst block size in bytes. This value is also known as "L1 cache line size".Some common values are:
- 16 for 486 processors
- 32 for Pentium MMX/II/III/Celeron series and Itanium processors
- 64 for Pentium 4 and Itanium 2 processors
This setting has a minor performance impact on some memory intensive operations. Unless you really want to tweak the performance, it's usually better to not touch this setting. Though, if you have e.g. a Pentium 4 processor, you may want to increase the value of this setting to 64 from the default value of 32.
- Parameters:
cacheBurst- The number of bytes in a L1 cache line.
-
getMemoryTreshold
@Deprecated public int getMemoryTreshold()
Deprecated.UsegetMemoryThreshold().Get the memory threshold.If the value is larger than the maximum value that can be presented in an integer, then
Integer.MAX_VALUEis returned.- Returns:
- The memory threshold.
-
setMemoryTreshold
@Deprecated public void setMemoryTreshold(int memoryThreshold)
Deprecated.Set the maximum size of apfloats in bytes that are stored in memory within this context.- Parameters:
memoryThreshold- The number of bytes that apfloats that are stored in memory will at most have within this context.
-
getMemoryThreshold
public long getMemoryThreshold()
Get the memory threshold.- Returns:
- The memory threshold.
-
setMemoryThreshold
public void setMemoryThreshold(long memoryThreshold)
Set the maximum size of apfloats in bytes that are stored in memory within this context. The minimum value for this setting is 128.If the memory threshold is too small, performance will suffer as small numbers are stored to disk, and the amount of disk I/O overhead becomes significant. On the other hand, if the memory threshold is too big, you can get an
OutOfMemoryError.The optimal value depends greatly on each application. Obviously, if you have plenty of heap space and don't create too many too big numbers you are not likely to have problems. The default value of this setting is 64kB, or the maximum heap size divided by 1024, whichever is larger.
- Parameters:
memoryThreshold- The number of bytes that apfloats that are stored in memory will at most have within this context.
-
getSharedMemoryTreshold
public long getSharedMemoryTreshold()
Get the shared memory threshold.- Returns:
- The shared memory threshold.
- See Also:
setSharedMemoryTreshold(long)
-
setSharedMemoryTreshold
public void setSharedMemoryTreshold(long sharedMemoryTreshold)
Set the maximum size of apfloats in bytes that can be used without synchronizing against the shared memory lock. The minimum value for this setting is 128.If only one thread is used then this setting has no effect. If multiple threads are used, and this setting is too small, performance will suffer as the synchronization blocking and other overhead becomes significant. On the other hand, if the numbers are being stored in memory, and the shared memory threshold is too big, you can get an
OutOfMemoryError.The optimal value depends on the application and the way parallelism is used. As a rule of thumb, this should be set to a value that is the maximum memory block size divided by the number of parallel threads. The default is somewhat more conservatively this number divided by 32.
- Parameters:
sharedMemoryTreshold- The number of bytes that apfloats will at most have, without synchronizing against the shared memory lock, within this context.
-
getBlockSize
public int getBlockSize()
Get the I/O block size.- Returns:
- The I/O block size.
- See Also:
setBlockSize(int)
-
setBlockSize
public void setBlockSize(int blockSize)
Set the efficient I/O block size in bytes for this context. The minimum value for this setting is 128.If the block size is too small, the overhead of each I/O call will definetely have an adverse effect on performance. Setting the block size very big will not affect performance significantly, but can increase intermediate memory consumption a lot, possibly resulting in running out of memory with an
OutOfMemoryError. A recommended minimum value is at least a few kilobytes.In many places, data in files is accessed in reverse order, fetching blocks of this size. Probably the optimal value of this setting is roughly half of the read-ahead buffer size of you hard disk. The default value is 64kB.
- Parameters:
blockSize- The I/O block size in bytes to be used in calculations using this context.
-
getNumberOfProcessors
public int getNumberOfProcessors()
Get the number of processors that should be used for parallel calculations.- Returns:
- The number of processors.
- See Also:
setNumberOfProcessors(int)
-
setNumberOfProcessors
public void setNumberOfProcessors(int numberOfProcessors)
Set the number of processors available to parallel calculations using this context. The minimum value for this setting is 1. The default is to use all processors (CPU cores) available.Note that if you change the number of processors after the library has been initialized, the number of threads available to the ExecutorService is not changed. If you want to change that too, it can be done easily with
setExecutorService(ApfloatContext.getDefaultExecutorService()).- Parameters:
numberOfProcessors- Number of processors available to parallel calculations using this context.- See Also:
getDefaultExecutorService(),setExecutorService(ExecutorService)
-
getCleanupAtExit
public boolean getCleanupAtExit()
Get if clean-up should be performed at the time the program exits.- Returns:
trueif clean-up will be done at JVM exit, orfalseif not.- See Also:
setCleanupAtExit(boolean)
-
setCleanupAtExit
public void setCleanupAtExit(boolean cleanupAtExit)
Set if clean-up should be performed at the time the program exits. The clean-up runs garbage collection and finalization to remove any remaining temporary files that may have been created. The default behavior istrue.For example unsigned applets must have this property set to
false, since they do not have access to setting shutdown hooks.Note that if this setting is ever set to
truein anyApfloatContext(and never set tofalsesubsequently in that context), then clean-up will be performed.Also note that having the shutdown hook set will prevent class garbage collection i.e. the apfloat classes can't be unloaded if the shutdown hook still references the ApfloatContext class. If class unloading is desired then the cleanupAtExit property should be set to false first.
- Parameters:
cleanupAtExit-trueif clean-up should be done at JVM exit, orfalseif not.
-
getProperty
public java.lang.String getProperty(java.lang.String propertyName)
Get the value of a property as string. The name of the property can be any of the constants defined above.- Parameters:
propertyName- The name of the property.- Returns:
- The value of the property as a
String.
-
getProperty
public java.lang.String getProperty(java.lang.String propertyName, java.lang.String defaultValue)Get the value of a property as string, with the provided default value if the property is not set.- Parameters:
propertyName- The name of the property.defaultValue- The default value to be returned, if the property is not set.- Returns:
- The value of the property as a
String. - Since:
- 1.7.0
-
setProperty
public void setProperty(java.lang.String propertyName, java.lang.String propertyValue) throws ApfloatConfigurationExceptionSet the value of a property as string. The name of the property can be any of the constants defined above.- Parameters:
propertyName- The name of the property.propertyValue- The value of the property as aString.- Throws:
ApfloatConfigurationException- If the property value can't be converted to the correct type.
-
getProperties
public java.util.Properties getProperties()
Get the values of all properties as strings. The names of the properties are all of the constants defined above.- Returns:
- The properties.
-
getSharedMemoryLock
public java.lang.Object getSharedMemoryLock()
Get the shared memory lock object. All internal functions that allocate a memory block larger than the shared memory threshold should synchronize the allocation and memory access on the object returned by this method.- Returns:
- The object on which large memory block allocation and access should be synchronized.
-
setSharedMemoryLock
public void setSharedMemoryLock(java.lang.Object lock)
Set the shared memory lock object. All internal functions that allocate a memory block larger than the shared memory threshold should synchronize the allocation and memory access on the object passed to this method.The object is not used for anything else than synchronization, so the class of the object should really be
java.lang.Object. One would typically call this method e.g. asctx.setSharedMemoryLock(new Object()).- Parameters:
lock- The object on which large memory block allocation and access should be synchronized.
-
getExecutorService
public java.util.concurrent.ExecutorService getExecutorService()
Get the ExecutorService. It can be used for executing operations in parallel.By default the ExecutorService is a thread pool that is shared by all the ApfloatContexts. The threads in the pool are daemon threads so the thread pool requires no clean-up at shutdown time.
- Returns:
- The ExecutorService.
- Since:
- 1.1
- See Also:
getDefaultExecutorService()
-
setExecutorService
public void setExecutorService(java.util.concurrent.ExecutorService executorService)
Set the ExecutorService.If a custom ExecutorService is used, e.g. a thread pool, then the number of available threads in the pool should match the number of processors set to all ApfloatContexts with
setNumberOfProcessors(int).Note that if a custom ExecutorService that requires shutdown is used, it is the caller's responsibility to clean up the ExecutorService at shutdown.
- Parameters:
executorService- The ExecutorService.- Since:
- 1.1
- See Also:
getDefaultExecutorService()
-
wait
public void wait(java.util.concurrent.Future<?> future)
While waiting for aFutureto be completed, do some useful work instead of just being idle.This method is intended to coordinate with the executor service from
getExecutorService()to keep all threads busy at all times.- Parameters:
future- The Future to wait for.- Since:
- 1.9.0
-
checkInterrupted
public static void checkInterrupted() throws ApfloatInterruptedExceptionChecks if the current thread was interrupted. If yes, throws anApfloatInterruptedException.This method calls
Thread.interrupted()so if the thread was interrupted, it clears the interrupted status.- Throws:
ApfloatInterruptedException- If the current thread was interrupted.- Since:
- 1.14.0
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Get an arbitrary object as an attribute for this ApfloatContext.- Parameters:
name- Name of the attribute.- Returns:
- Value of the attribute or
nullif the attribute doesn't exist.
-
setAttribute
public java.lang.Object setAttribute(java.lang.String name, java.lang.Object value)Set an arbitrary object as an attribute for this ApfloatContext.- Parameters:
name- Name of the attribute.value- Value of the attribute.- Returns:
- Previous value of the attribute or
nullif the attribute didn't exist.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Remove an attribute from this ApfloatContext.- Parameters:
name- Name of the attribute.- Returns:
- Value of the attribute or
nullif the attribute didn't exist.
-
getAttributeNames
public java.util.Enumeration<java.lang.String> getAttributeNames()
Get names of all attributes for this ApfloatContext.- Returns:
- Names of all attributes as strings.
-
loadProperties
public static java.util.Properties loadProperties() throws ApfloatRuntimeExceptionLoads properties from a properties file or resource bundle. First theResourceBundleby the name "apfloat" is located, then all properties found from that resource bundle are put to aPropertiesobject.The resource bundle is found basically using the following logic (note that this is standard Java
ResourceBundlefunctionality), in this order whichever is found first:- From the class named
apfloat(that should be a subclass ofResourceBundle), in the current class path - From the file "apfloat.properties" in the current class path
- Returns:
- Properties found in the "apfloat" resource bundle, or an empty
Propertiesobject, if the resource bundle is not found. - Throws:
ApfloatRuntimeException
- From the class named
-
getDefaultExecutorService
public static java.util.concurrent.ExecutorService getDefaultExecutorService()
Returns a new instance of a default ExecutorService.The default executor service is a
ForkJoinPoolwhere the number of threads is one less than the number of processors set withsetNumberOfProcessors(int).- Returns:
- A new instance of a default ExecutorService.
- Since:
- 1.3
-
setProperties
public void setProperties(java.util.Properties properties) throws ApfloatConfigurationExceptionSet the values of all properties as strings. The names of the properties can be all of the constants defined above.- Parameters:
properties- The properties.- Throws:
ApfloatConfigurationException- If a property value can't be converted to the correct type.
-
clone
public java.lang.Object clone()
Creates a copy of this object.The clone has the same BuilderFactory and FilenameGenerator members and the same shared memory lock and ExecutorService as the original ApfloatContext.
A shallow copy of the property set and the attribute set is created. Thus setting a property or attribute on the clone will not set it in the original object. Since the actual attributes (values) are shared, if an attribute is mutable and is modified in the clone, the modified value will appear in the original also.
- Overrides:
clonein classjava.lang.Object- Returns:
- A mostly shallow copy of this object.
-
loadSystemOverrides
private static java.util.Properties loadSystemOverrides(java.util.Properties properties)
-
-