public final class ComThread extends Thread
For each user thread that works with COM objects,
one ComThread
is created to manage those objects.
This is because COM objects are inherently tied to the thread that created it, and therefore all the invocations must be routed through the creator thread. See http://msdn.microsoft.com/en-us/library/ms809971.aspx for more discussions.
This model is rather alien to Java developers, where objects can be passed between threads more freely. (This is a separate issue from whether those objects can be safely accessed concurrently.)
To bridge these gaps, we don't let application threads touch COM objects at all,
and instead create ComThread
as a shadow thread for each application thread who wants to
create a COM object.
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
static int |
GARBAGE_COLLECTION_INTERVAL |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Modifier and Type | Method and Description |
---|---|
void |
addListener(ComObjectListener listener)
Adds a
ComObjectListener to this ComThread |
void |
addLiveObject(Com4jObject r)
Adds a
Com4jObject to the live objects of this ComThread |
<T> T |
execute(com4j.Task<T> task)
Executes a
Task in a ComThread
and returns its result. |
static void |
flushFreeList()
This method calls System.gc() and executes a dummy task to initiate the corresponding
ComThread to call dispose0() on all waiting objects.
|
void |
kill()
Kills this
ComThread gracefully
and blocks until a thread dies. |
void |
removeListener(ComObjectListener listener)
Removes the
ComObjectListener from this ComThread |
void |
run() |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public void kill()
ComThread
gracefully
and blocks until a thread dies.public <T> T execute(com4j.Task<T> task)
Task
in a ComThread
and returns its result.T
- The type of the return value.task
- The task to be executedpublic void addLiveObject(Com4jObject r)
Com4jObject
to the live objects of this ComThread
This method increases the live object count of this thread and fires an
ComObjectListener.onNewObject(Com4jObject)
event to all listeners.
r
- The new Com4jObject
public void addListener(ComObjectListener listener)
ComObjectListener
to this ComThread
listener
- the new listenerIllegalArgumentException
- if the listener
is null
or if the listener is already registered.public void removeListener(ComObjectListener listener)
ComObjectListener
from this ComThread
listener
- The listener to removeIllegalArgumentException
- if the listener was not registered to this ComThread
public static void flushFreeList()
Copyright © 2014. All rights reserved.