Package ch.bailu.gtk.glib
Class ThreadPool
java.lang.Object
ch.bailu.gtk.type.Type
ch.bailu.gtk.type.Pointer
ch.bailu.gtk.type.Record
ch.bailu.gtk.glib.ThreadPool
- All Implemented Interfaces:
PointerInterface
The #GThreadPool struct represents a thread pool. It has three
public read-only members, but the underlying struct is bigger,
so you must not copy this struct.
public read-only members, but the underlying struct is bigger,
so you must not copy this struct.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
static interface
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
are all threads exclusive to this poolstatic final String
the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
static final String
the user data for the threads of this pool -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ThreadPool
_new
(ThreadPool.OnFunc func, Pointer user_data, int max_threads, boolean exclusive) This function creates a new thread pool.void
free
(boolean immediate, boolean wait_) Frees all resources allocated for @pool.static ClassHandler
boolean
are all threads exclusive to this poolch.bailu.gtk.glib.JnaThreadPool.OnFunc
the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
the user data for the threads of this poolstatic int
This function will return the maximum @interval that a
thread will wait in the thread pool for new tasks before
being stopped.int
Returns the maximal number of threads for @pool.static int
Returns the maximal allowed number of unused threads.int
Returns the number of threads currently running in @pool.static int
Returns the number of currently unused threads.boolean
moveToFront
(Pointer data) Moves the item to the front of the queue of unprocessed
items, so that it will be processed next.static ThreadPool
newFull
(ThreadPool.OnFunc func, Pointer user_data, ThreadPool.OnDestroyNotify item_free_func, int max_threads, boolean exclusive) This function creates a new thread pool similar to g_thread_pool_new()
but allowing @item_free_func to be specified to free the data passed
to g_thread_pool_push() in the case that the #GThreadPool is stopped
and freed before all tasks have been executed.boolean
Inserts @data into the list of tasks to be executed by @pool.void
setFieldExclusive
(boolean exclusive) are all threads exclusive to this poolvoid
the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
void
setFieldUserData
(Pointer user_data) the user data for the threads of this poolstatic void
setMaxIdleTime
(int interval) This function will set the maximum @interval that a thread
waiting in the pool for new tasks can be idle for before
being stopped.boolean
setMaxThreads
(int max_threads) Sets the maximal allowed number of threads for @pool.static void
setMaxUnusedThreads
(int max_threads) Sets the maximal number of unused threads to @max_threads.void
setSortFunction
(ThreadPool.OnCompareDataFunc func, Pointer user_data) Sets the function used to sort the list of tasks.static void
Stops all currently unused threads.int
Returns the number of tasks still unprocessed in @pool.Methods inherited from class ch.bailu.gtk.type.Pointer
asCPointer, cast, connectSignal, disconnectSignals, disconnectSignals, equals, hashCode, throwIfNull, throwNullPointerException, toString, unregisterCallbacks, unregisterCallbacks
Methods inherited from class ch.bailu.gtk.type.Type
asCPointer, asCPointer, asCPointerNotNull, asJnaPointer, asJnaPointer, asPointer, asPointer, cast, cast, throwIfNull
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.bailu.gtk.type.PointerInterface
asCPointerNotNull, asJnaPointer, asPointer, isNotNull, isNull
-
Field Details
-
FUNC
the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
- See Also:
-
USER_DATA
the user data for the threads of this pool- See Also:
-
EXCLUSIVE
are all threads exclusive to this pool- See Also:
-
-
Constructor Details
-
ThreadPool
-
ThreadPool
public ThreadPool()
-
-
Method Details
-
getClassHandler
-
setFieldFunc
the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
-
getFieldFunc
public ch.bailu.gtk.glib.JnaThreadPool.OnFunc getFieldFunc()the function to execute in the threads of this pool
SeeThreadPool.OnFunc.onFunc(ch.bailu.gtk.lib.handler.CallbackHandler, ch.bailu.gtk.type.Pointer, ch.bailu.gtk.type.Pointer)
-
setFieldUserData
the user data for the threads of this pool -
getFieldUserData
the user data for the threads of this pool -
setFieldExclusive
public void setFieldExclusive(boolean exclusive) are all threads exclusive to this pool -
getFieldExclusive
public boolean getFieldExclusive()are all threads exclusive to this pool -
free
public void free(boolean immediate, boolean wait_) Frees all resources allocated for @pool.
If @immediate is %TRUE, no new task is processed for @pool.
Otherwise @pool is not freed before the last task is processed.
Note however, that no thread of this pool is interrupted while
processing a task. Instead at least all still running threads
can finish their tasks before the @pool is freed.
If @wait_ is %TRUE, this function does not return before all
tasks to be processed (dependent on @immediate, whether all
or only the currently running) are ready.
Otherwise this function returns immediately.
After calling this function @pool must not be used anymore.- Parameters:
immediate
- should @pool shut down immediately?wait_
- should the function wait for all tasks to be finished?
-
getMaxThreads
public int getMaxThreads()Returns the maximal number of threads for @pool.- Returns:
- the maximal number of threads
-
getNumThreads
public int getNumThreads()Returns the number of threads currently running in @pool.- Returns:
- the number of threads currently running
-
moveToFront
Moves the item to the front of the queue of unprocessed
items, so that it will be processed next.- Parameters:
data
- an unprocessed item in the pool- Returns:
- %TRUE if the item was found and moved
-
push
Inserts @data into the list of tasks to be executed by @pool.
When the number of currently running threads is lower than the
maximal allowed number of threads, a new thread is started (or
reused) with the properties given to g_thread_pool_new().
Otherwise, @data stays in the queue until a thread in this pool
finishes its previous task and processes @data.
@error can be %NULL to ignore errors, or non-%NULL to report
errors. An error can only occur when a new thread couldn't be
created. In that case @data is simply appended to the queue of
work to do.
Before version 2.32, this function did not return a success status.- Parameters:
data
- a new task for @pool- Returns:
- %TRUE on success, %FALSE if an error occurred
- Throws:
AllocationError
-
setMaxThreads
Sets the maximal allowed number of threads for @pool.
A value of -1 means that the maximal number of threads
is unlimited. If @pool is an exclusive thread pool, setting
the maximal number of threads to -1 is not allowed.
Setting @max_threads to 0 means stopping all work for @pool.
It is effectively frozen until @max_threads is set to a non-zero
value again.
A thread is never terminated while calling @func, as supplied by
g_thread_pool_new(). Instead the maximal number of threads only
has effect for the allocation of new threads in g_thread_pool_push().
A new thread is allocated, whenever the number of currently
running threads in @pool is smaller than the maximal number.
@error can be %NULL to ignore errors, or non-%NULL to report
errors. An error can only occur when a new thread couldn't be
created.
Before version 2.32, this function did not return a success status.- Parameters:
max_threads
- a new maximal number of threads for @pool, or -1 for unlimited- Returns:
- %TRUE on success, %FALSE if an error occurred
- Throws:
AllocationError
-
setSortFunction
Sets the function used to sort the list of tasks. This allows the
tasks to be processed by a priority determined by @func, and not
just in the order in which they were added to the pool.
Note, if the maximum number of threads is more than 1, the order
that threads are executed cannot be guaranteed 100%. Threads are
scheduled by the operating system and are executed at random. It
cannot be assumed that threads are executed in the order they are
created.- Parameters:
func
- the #GCompareDataFunc used to sort the list of tasks. This function is passed two tasks. It should return 0 if the order in which they are handled does not matter, a negative value if the first task should be processed before the second or a positive value if the second task should be processed first.user_data
- user data passed to @func
-
unprocessed
public int unprocessed()Returns the number of tasks still unprocessed in @pool.- Returns:
- the number of unprocessed tasks
-
getMaxIdleTime
public static int getMaxIdleTime()This function will return the maximum @interval that a
thread will wait in the thread pool for new tasks before
being stopped.
If this function returns 0, threads waiting in the thread
pool for new work are not stopped.- Returns:
- the maximum @interval (milliseconds) to wait for new tasks in the thread pool before stopping the thread
-
getMaxUnusedThreads
public static int getMaxUnusedThreads()Returns the maximal allowed number of unused threads.- Returns:
- the maximal number of unused threads
-
getNumUnusedThreads
public static int getNumUnusedThreads()Returns the number of currently unused threads.- Returns:
- the number of currently unused threads
-
_new
public static ThreadPool _new(ThreadPool.OnFunc func, @Nullable Pointer user_data, int max_threads, boolean exclusive) throws AllocationError This function creates a new thread pool.
Whenever you call g_thread_pool_push(), either a new thread is
created or an unused one is reused. At most @max_threads threads
are running concurrently for this thread pool. @max_threads = -1
allows unlimited threads to be created for this thread pool. The
newly created or reused thread now executes the function @func
with the two arguments. The first one is the parameter to
g_thread_pool_push() and the second one is @user_data.
Pass g_get_num_processors() to @max_threads to create as many threads as
there are logical processors on the system. This will not pin each thread to
a specific processor.
The parameter @exclusive determines whether the thread pool owns
all threads exclusive or shares them with other thread pools.
If @exclusive is %TRUE, @max_threads threads are started
immediately and they will run exclusively for this thread pool
until it is destroyed by g_thread_pool_free(). If @exclusive is
%FALSE, threads are created when needed and shared between all
non-exclusive thread pools. This implies that @max_threads may
not be -1 for exclusive thread pools. Besides, exclusive thread
pools are not affected by g_thread_pool_set_max_idle_time()
since their threads are never considered idle and returned to the
global pool.
@error can be %NULL to ignore errors, or non-%NULL to report
errors. An error can only occur when @exclusive is set to %TRUE
and not all @max_threads threads could be created.
See #GThreadError for possible errors that may occur.
Note, even in case of error a valid #GThreadPool is returned.- Parameters:
func
- a function to execute in the threads of the new thread pooluser_data
- user data that is handed over to @func every time it is calledmax_threads
- the maximal number of threads to execute concurrently in the new thread pool, -1 means no limitexclusive
- should this thread pool be exclusive?- Returns:
- the new #GThreadPool
- Throws:
AllocationError
-
newFull
public static ThreadPool newFull(ThreadPool.OnFunc func, @Nullable Pointer user_data, ThreadPool.OnDestroyNotify item_free_func, int max_threads, boolean exclusive) throws AllocationError This function creates a new thread pool similar to g_thread_pool_new()
but allowing @item_free_func to be specified to free the data passed
to g_thread_pool_push() in the case that the #GThreadPool is stopped
and freed before all tasks have been executed.- Parameters:
func
- a function to execute in the threads of the new thread pooluser_data
- user data that is handed over to @func every time it is calleditem_free_func
- used to pass as a free function to g_async_queue_new_full()max_threads
- the maximal number of threads to execute concurrently in the new thread pool, `-1` means no limitexclusive
- should this thread pool be exclusive?- Returns:
- the new #GThreadPool
- Throws:
AllocationError
-
setMaxIdleTime
public static void setMaxIdleTime(int interval) This function will set the maximum @interval that a thread
waiting in the pool for new tasks can be idle for before
being stopped. This function is similar to calling
g_thread_pool_stop_unused_threads() on a regular timeout,
except this is done on a per thread basis.
By setting @interval to 0, idle threads will not be stopped.
The default value is 15000 (15 seconds).- Parameters:
interval
- the maximum @interval (in milliseconds) a thread can be idle
-
setMaxUnusedThreads
public static void setMaxUnusedThreads(int max_threads) Sets the maximal number of unused threads to @max_threads.
If @max_threads is -1, no limit is imposed on the number
of unused threads.
The default value is 2.- Parameters:
max_threads
- maximal number of unused threads
-
stopUnusedThreads
public static void stopUnusedThreads()Stops all currently unused threads. This does not change the
maximal number of unused threads. This function can be used to
regularly stop all unused threads e.g. from g_timeout_add().
-