locking

What's the difference between an exclusive lock and a shared lock?

被刻印的时光 ゝ 提交于 2019-12-02 13:49:18
According to wikipedia, Shared locks are sometimes called "read locks" and exclusive locks are sometimes called "write locks". Can you explain the reasoning behind the terms "shared" and "exclusive"? I wrote this answer down because I thought this would be a fun (and fitting) analogy: Think of a lockable object as a blackboard (lockable) in a class room containing a teacher (writer) and many students (readers). While a teacher is writing something (exclusive lock) on the board: Nobody can read it, because it's still being written, and she's blocking your view => If an object is exclusively

How to make threads go through a gate in order using C#

大憨熊 提交于 2019-12-02 13:25:03
I have three threads and some part of the code can run in parallel, some parts are locked(only one thread at the time). However one lock needs to only let them in in order. Since this is a loop it gets more complex. How do I make this behavior? If i had a print statement I would like to receive the following output: 1,2,3,1,2,3,1,2,3.... currently I receive 2,3,1,3,1,3,2,1,2 A.K.A. random order. The code which is executed in three threads in parallel: while (true){ lock (fetchLock){ if(done){ break; } //Do stuff one at the time } //Do stuff in parralell lock (displayLock){ //Do stuff one at

Asynchronous ajax request locking browser

十年热恋 提交于 2019-12-02 13:14:55
问题 This is a simple snippet of code to launch an aynchronous ajax request. The processing time of the request is deliberately long (10 seconds or more). Why browser prevent my users to click on a href link during the process of the async request ? (tried with Firefox and Chrome) The async request is normally called and the 'Ready' message is immediately displayed in console. Snippet : new Ajax.Request('index.php', { method: 'post', asynchronous: true, parameters: { 'sleep': 10 }, onSuccess:

How to lock with ReentrantLock?

南楼画角 提交于 2019-12-02 13:12:48
I would expect the following test to only print "has been locked" once. BUT it consequently prints the line. public class LocKTest { @Test public void testLock() { Lock lock = new ReentrantLock(); while (true) { if (lock.tryLock()) { //lock.lock(); same result if I include an explicit lock here System.out.println("has been locked"); } } } } As far as I understood, tryLock will lock the ReentrantLock if possible (ie if not locked yet). But obviously this is not the case. How can I set such a lock threadsafe? The name is ReentrantLock , meaning you can re-enter if you already own the lock. If

Flexible alternatives for locking (selective lock)

大城市里の小女人 提交于 2019-12-02 12:34:51
问题 I need to resolve situation for equal objects with different memory location (it happens for REST request because of multithreading). So as part solution I have implemented service. I'm sharing here most important parts: private Map<T, ReentrantLock> lockHolder = new HashMap(); void unLock(T monitorMarker) { synchronized (lockHolder) { ReentrantLock lock = lockHolder.get(monitorMarker); if (lock == null || lock.getHoldCount() == 0) { return; } lock.unlock(); if (lock.getHoldCount() == 0) {

How can I create a non-file lock in PHP?

為{幸葍}努か 提交于 2019-12-02 10:30:28
I have a maintenance script in PHP that updates and repairs the database. In theory, running two scripts simultaneously shouldn't be a problem, but I want to be extra safe by putting a lock in a variable in PHP. The problem, of course, is that I can't store it in a $_SESSION variable because sessions only apply to one user. Is there any way I can store this lock in a variable? I'd prefer to not create and delete a file in case the server dies in the middle of the script. MySQL has GET_LOCK() function. Since you're working with the database, you can lock the tables you need while the script

Part-2: Web Start Application: Concurrency Issue

▼魔方 西西 提交于 2019-12-02 08:53:13
问题 With your suggestions given on this thread, I tried using FileLock, however, when I write something in the file, somehow excel file gets corrupted and there is nothing in the file (it gets empty, no contents in there) I have the following method: void writeIntoTheFile(XSSFWorkbook defectWorkBook, File fileToWrite) { FileLock lock = null; FileChannel channel = null; FileOutputStream out = null; try { //fileToWrite contains an excel .xlsx file channel = new RandomAccessFile(fileToWrite, "rw")

In Java, how do I test if an object's monitor is locked? [duplicate]

家住魔仙堡 提交于 2019-12-02 08:46:26
问题 This question already has answers here : How do determine if an object is locked (synchronized) so not to block in Java? (7 answers) Java: How to check if a lock can be acquired? [duplicate] (3 answers) Closed 2 years ago . In Java, how do I test if an object's monitor is locked? In other words, given a object obj, does any thread own obj's monitor? I do not care which thread owns the monitor. All I need to test is if ANY thread owns a given object's monitor. Since a thread other than the

File segment/section/record locks in Linux threads

主宰稳场 提交于 2019-12-02 08:39:13
I have a multi-threaded process where a file is shared (read and written) by multiple threads. Is there any way a thread can lock one file segment so that other threads cannot access it? I have tried fcntl(fd, F_SETLKW, &flock) , but this lock only works for processes, not threads (a lock is shared between all threads in an process). Yes - but not with the same mechanism. You'll have to use something like pthread mutexes, and keep track of the bookkeeping yourself. Possible outline for how to make this work Wait on and claim a process-level mutex over a bookkeeping structure make sure no other

Device IMEI locking for tablets

假装没事ソ 提交于 2019-12-02 06:57:18
In my application I used to lock application with IMEI code. I mean during startup application checks device IMEI and compares it with list of allowed devices. If device is in list user can continue to work, otherwise it bails out: public boolean checkIMEI(Activity activity) { TelephonyManager tm=(TelephonyManager )activity.getSystemService(Context.TELEPHONY_SERVICE); if(tm==null) { Log.v(TAG, "Can't get telephony service"); new MessageBox(activity, "Can't get telephony service. Forcing shut down!"); return false; } //encrypted IMEIs list String[] vals=activity.getResources().getStringArray(R