简体   繁体   中英

Is it safe to call a static synchronized method from unrelated classes?

I have a class that uses a static synchronized method to make HTTP calls.

I also have an ExecutorService .

There are multiple Runnable classes calling this method. The method will accept URLs, POST/GET information and arguments, and return either an error code if the response is not 200, or an XML string to be processed further by the Runnable class.

If multiple threads call this method after being placed in the ExecutorService , will this be okay? Will it fail?

What is the ideal way to process multiple HTTP calls through ExecutorService ?

Any question about "thread safety" has to be a question about mutable, shared data . Unless your threads are sharing mutable data, then there is no thread safety issue.

A mutable object or a group of mutable objects in your program is "Thread safe" if it is not possible for the action of any one thread to put the object/group into a "bad" state that can be seen by other threads.

The easiest, most common way to guarantee thread safety of an object/group is to use mutual exclusion (ie, synchronized blocks and/or methods). If every method that modifies or READS the data does so only inside blocks of code that are all synchronized on the same object, and if the mutator functions always insure that the object/group is in a valid state before leaving any synchronized block, then that object/group will be "thread safe."


Your question does not mention any data. Your question does not mention whether there are any possible "invalid" states that the data could be in. Your question talks about one method, but it doesn't mention whether there are any other methods that access the same data.

If the method in question is static and synchronized , then that will guarantee that no two threads will ever be able to enter the method at the same time, but that won't make the data "thread safe" if there are any other methods that accesses or modify the same data.

All of the code blocks and/or methods that access the data must be synchronized , and they must all be synchronized on the same lock object.


Actually, synchronizing every access might be overkill. But there's no way to know that without knowing what data the threads are sharing, and what they are doing with the data.

It's all about the data.

Yes, it's the main point with the synchronize label in Java :)

It's like locking a mutex at the beginning of the function et unlocking this same mutex at the end at the return.

In this case, the mutex is Object-wise, so calling a synchonized method will lock the others one. Be aware

Having ExecutorService tasks call a static synchronized method is going to be a bottleneck. Only one thread can execute this method at a time.

If all the tasks in the ExecutorService need to make this call then you are going to be defeating the purpose of having multiple threads, as most of the threads may be spending their time waiting to acquire the monitor on the class so they can call the static method.

The static synchronized method is there to protect shared state from unsafe concurrent access. It's a lot better to give each task its own state and eliminate sharing as much as possible than to have all the threads taking turns to access the shared resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM