synchronization

What is the difference between synchronized methods and blocks?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 03:08:55
问题 What is the difference between synchronized methods and synchronized statements ? If possible, please use an example to make it more clear. 回答1: A synchronized method locks the monitor associated with the instance of the class (ie 'this') or the class (if a static method) and prevents others from doing so until the return from the method. A synchronized block can lock any monitor (you tell it which) and can have a scope smaller than that of the encolsing method. Synchronized blocks are

What is the correct way to prevent reentrancy and ensure a lock is acquired for certain operations?

我是研究僧i 提交于 2019-12-10 02:58:53
问题 I'm designing a base class that, when inherited, will provide business functionality against a context in a multithreaded environment. Each instance may have long-running initialization operations, so I want to make the objects reusable. In order to do so, I need to be able to: Assign a context to one of these objects to allow it to do its work Prevent an object from being assigned a new context while it already has one Prevent certain members from being accessed while the object doesn't have

How to check how many threads are waiting for a synchronized method to become unlocked

房东的猫 提交于 2019-12-10 02:14:08
问题 Is there any way to check how many threads are waiting for a synchronized method to become unlocked? I would like to know when the thread calls a synchronized method: 1) How many threads are already waiting to call the method? 2) Once the method is called how long it needed to wait for the method to become unlocked? Solution: I solved this using stackers answer: public class LockedClass { public static int count; public static void measuringClass() throws IOException{ long startTime = System

Preferring synchronized to volatile

半世苍凉 提交于 2019-12-10 01:45:50
问题 I've read this answer in the end of which the following's written: Anything that you can with volatile can be done with synchronized, but not vice versa. It's not clear. JLS 8.3.1.4 defines volatile fields as follows: A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4). So, the volatile fields are about memory visibility. Also, as far as I got from the answer I cited, reading and writing to volatile

Monitor as synchronization on Windows

自作多情 提交于 2019-12-10 00:35:15
问题 Is there any implementation of monitor on Windows? I didn't see any win32 API references Monitor. 回答1: Windows does not have a monitor implementation of its own. However, Vista introduced Condition Variables and Slim Reader/Writer locks, which can be used together to create a monitor implementation. 回答2: Yes it does. Windows has monitors and monitor functions: EnterCriticalSection is similar to POSIX pthread_mutex_lock (enters the monitor). LeaveCriticalSection is similar to POSIX pthread

Spring @Transactional concurrency

巧了我就是萌 提交于 2019-12-09 23:11:12
问题 class MyService { public void a() { synchronized(somekey) { b(); } } @Transactional(propagation = Propagation.REQUIRES_NEW) public void b() { ...do DB works... } } My aim is 1 - get the key 2 - start transaction 3 - commit transaction 4 - release the key When i call a() method from outside, transaction doesn't work. Any suggestions ? Thanks. 回答1: Unless you're using code weaving, this can't work. The default way Spring handles transactions is through AOP proxies. The call to a transactional

synchronized blocks for static and non-static methods

依然范特西╮ 提交于 2019-12-09 21:56:21
问题 I created two threads and using a single instance of class called the static and non-static methods of that object. Ideally static methods need to be called using the class name and i did that too. I synchronized both the static and non-static methods on a private static member of the class whose methods the threads are calling. I noticed that the output was synchronized! My questions are: Static methods if synchronized using a synchronized block it usually requires the class instance, then

Destruction of static class members in Thread local storage

≯℡__Kan透↙ 提交于 2019-12-09 18:15:11
问题 I'm writing a fast multi-thread program, and I want to avoid syncronization (the function which would need to be syncronized must be called something like 5,000,000 times per second, so even a mutex would be too heavy). The scenario is: I have a single global instance of a class, and each thread can access it. In order to avoid syncronization, all the data inside the class is accessed read-only, except for a bunch of class members, which are then declared in TLS (with __thread or __declspec

How to use wait and notify protocol with multiple threads

吃可爱长大的小学妹 提交于 2019-12-09 18:15:07
问题 Specifically, can somebody tell me what is wrong with this piece of code. It should start the threads, so should print "Entering thread.." 5 times and then wait until notifyAll() is called. But, it randomly prints "Entering.." and "Done.." and still keeps waiting on others. public class ThreadTest implements Runnable { private int num; private static Object obj = new Object(); ThreadTest(int n) { num=n; } @Override public void run() { synchronized (obj) { try { System.out.println("Entering

How can I get the table name in a PostgreSQL trigger function?

不问归期 提交于 2019-12-09 14:22:08
问题 I have a trigger function: CREATE OR REPLACE FUNCTION "trigger_deleteUsers"() RETURNS trigger AS $BODY$ BEGIN INSERT INTO "DeletedEntities" ("uuidKey", "dateCreated", "dateModified", "dateSynced", "username", "entityName") VALUES (OLD."uuidKey", OLD."dateCreated", OLD."dateModified", "dateSynced", OLD."username", 'Users'); RETURN NULL; END; $BODY$ LANGUAGE plpgsql; CREATE TRIGGER "deleteUsers" AFTER DELETE ON "Users" FOR EACH ROW EXECUTE PROCEDURE "trigger_deleteUsers"(); This works for the