| Context and Deployment Descriptor > The Context Services > Synchronization Strategy |
Synchronization Strategy |
INDEX
PREVIOUS
NEXT
|
The synchronization strategy, named after the strategy design pattern, is defined by the class IlSynchronizationStrategy. It makes it possible to access shared resources simultaneously with a choice of synchronization schemes.
The IlSynchronizationStrategy class provides five methods to manage synchronized access to resources:
SetDefault(IlSynchronizationStrategy defaultStrategy) Defines the default strategy to use. The default locking behavior of an application can be changed just by changing this default.
GetDefault() Gets the default synchronization strategy to use.
synchronizeRun(Runnable runnable, Object lock) Synchronizes the current thread and calls run on the Runnable object passed. Depending on the implementation of the strategy, the synchronization may be global (such as in a Swing application), or may use the object passed as the lock parameter to synchronize on.
readLock(Runnable runnable, Object lock) Acquires a read lock for the current thread and calls run on the Runnable object passed. Some implementations may not support read/write locks. For this reason, the default implementation will call the synchronizeRun method.
writeLock(Runnable runnable, Object lock) Acquires a write lock for the current thread and calls run on the Runnable object passed. Some implementations may not support read/write locks. For this reason, the default implementation will call the synchronizeRun method.
The following code extract shows you how you can use the synchronization strategy in your application:
IlpContext context = IltSystem.GetDefaultContext();
IlSynchronizationStrategy strategy = context.getSynchronizationStrategy();
Runnable task = new Runnable() {
public void run() {
...
}
}
IlpNetwork networkComponent = new IlpNetwork(context);
strategy.writeLock(task, networkComponent.getModel());
JViews TGO provides two standard implementations of this service:
IlSwingThreadSyncStrategy for Swing-based applications
IlSynchronizeOnLockStrategy to synchronize on a mutex
The default synchronization strategy service can be customized through the deployment descriptor file using the tag <synchronizationStrategy>.
[<synchronizationStrategy type="application"|"servlet"/>]
where type:
application creates a synchronization strategy for Swing-based applications using the IlSwingThreadSyncStrategy class.
servlet creates a synchronization strategy for servlet applications using the IlSynchronizeOnLockStrategy class.
The following code extract shows you how you can customize the synchronization strategy through the API:
The only thing you can do through the API is to change the synchronization strategy used in the context.
IlpDefaultContext context = ... IlSynchronizationStrategy strategy = new IlSwingThreadSyncStrategy(); context.addService(IlSynchronizationStrategy.class, strategy);
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. All rights reserved. Legal terms. | PREVIOUS NEXT |