skip to main content
Programmers documentation > Context and Deployment Descriptor > The context services > Synchronization strategy
 
Synchronization strategy
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. 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. Synchronizes the current thread and calls run on the specified Runnable object. 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. Acquires a read lock for the current thread and calls run on the specified Runnable object. Some implementations may not support read/write locks. For this reason, the default implementation will call the synchronizeRun method.
*writeLock._Acquires a write lock for the current thread and calls run on the specified Runnable object. Some implementations may not support read/write locks. For this reason, the default implementation will call the synchronizeRun method.
How to use the synchronization strategy through the API
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
How to customize the synchronization strategy through the deployment descriptor
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:
How to 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 © 2018, Rogue Wave Software, Inc. All Rights Reserved.