use of java.util.concurrent.locks.ReentrantReadWriteLock in project ignite by apache.
the class ReadWriteLockMultiThreadedTest method testTryWriteLock.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
final ReadWriteLock lock = new ReentrantReadWriteLock();
lock.readLock().lock();
X.println("Read lock acquired.");
IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
boolean res = lock.writeLock().tryLock();
X.println("Attempting to try write lock: " + res);
try {
return null;
} finally {
if (res)
lock.writeLock().unlock();
}
}
}, 1, "write-lock");
Thread.sleep(2000);
X.println("Read lock released: " + lock);
lock.readLock().unlock();
fut.get();
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project jena by apache.
the class TestTDBInternal method exclusive_5.
@Test
public void exclusive_5() {
DatasetGraph dsg = TDBFactory.createDatasetGraph();
TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
dsg.begin(ReadWrite.READ);
boolean b = txnmgr.tryExclusiveMode();
Assert.assertFalse(b);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project jena by apache.
the class TestTDBInternal method exclusive_4.
@Test
public void exclusive_4() {
DatasetGraph dsg = TDBFactory.createDatasetGraph();
TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
checkLock(rwx, 0, 0);
boolean b = txnmgr.tryExclusiveMode();
Assert.assertTrue("Exclusive 1", b);
checkLock(rwx, 0, 1);
txnmgr.finishExclusiveMode();
checkLock(rwx, 0, 0);
b = txnmgr.tryExclusiveMode();
Assert.assertTrue("Exclusive 2", b);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project jena by apache.
the class TestTDBInternal method exclusive_3.
@Test
public void exclusive_3() {
DatasetGraph dsg = TDBFactory.createDatasetGraph();
TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
checkLock(rwx, 0, 0);
txnmgr.startExclusiveMode();
checkLock(rwx, 0, 1);
txnmgr.finishExclusiveMode();
checkLock(rwx, 0, 0);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project poi by apache.
the class OPCPackage method close.
/**
* Close the open, writable package and save its content.
*
* If your package is open read only, then you should call {@link #revert()}
* when finished with the package.
*
* @throws IOException
* If an IO exception occur during the saving process.
*/
@Override
public void close() throws IOException {
if (this.packageAccess == PackageAccess.READ) {
logger.log(POILogger.WARN, "The close() method is intended to SAVE a package. This package is open in READ ONLY mode, use the revert() method instead !");
revert();
return;
}
if (this.contentTypeManager == null) {
logger.log(POILogger.WARN, "Unable to call close() on a package that hasn't been fully opened yet");
revert();
return;
}
// Save the content
ReentrantReadWriteLock l = new ReentrantReadWriteLock();
try {
l.writeLock().lock();
if (this.originalPackagePath != null && !"".equals(this.originalPackagePath.trim())) {
File targetFile = new File(this.originalPackagePath);
if (!targetFile.exists() || !(this.originalPackagePath.equalsIgnoreCase(targetFile.getAbsolutePath()))) {
// Case of a package created from scratch
save(targetFile);
} else {
closeImpl();
}
} else if (this.output != null) {
save(this.output);
output.close();
}
} finally {
l.writeLock().unlock();
}
// Clear
this.contentTypeManager.clearAll();
}
Aggregations