开发者

What to do if one library is not multi-threaded?

开发者 https://www.devze.com 2022-12-23 16:58 出处:网络
I would like to multi-thread an application, however one library i\'m using is not multi-thread capable (i don\'t know what\'s the right word ? synchronized ?).

I would like to multi-thread an application, however one library i'm using is not multi-thread capable (i don't know what's the right word ? synchronized ?).

What are my options ?

As far as i know there's nothing in between threads and processes (Runtime.exec) in java (no abstraction in the jvm to have something like an isolated "java process").

How wo开发者_如何学Pythonuld you deal with that ?

EDIT

Thanks for all the answer, once again, one level of indirection does the trick.


You can ensure that the library in question is used only from a single thread at a time. If it contains instantiatable classes, one possibility is to hold these in thread local storage.

Or you can build a thread-safe wrapper around it.

These approaches could also be combined, e.g. you can wrap the library in a class (it would be a Facade in this case), which is not thread safe in itself, but whose instances you access from a single thread at a time.

Update: as @Wim pointed out, if the library manages global state, you must have a thread safe wrapper to ensure that changes are made visible between threads.


I would create a Facade and not use the library directly. The Facade should then synchronize the connection/calls to the library.

Something like:

External Call    External Call    External Call
      |               |                |
      ----------------------------------             
                    Wrapper
                      |
                   Library

Update:

A Facade could be the wrong design pattern, because it is used to hide functionality. Wrapper should used as Design Pattern


Alot depends on the size of the library and what you are using it for. The first thing to do is evaluate where you may actually have concurrency issues. Just be cause you are using the library in more than one thread does not mean it can't be used in multiple threads. Look for items in the API that specifically say "this is not thread safe" and if you will be using that part of the API you will need to synchronize it yourself. The simplest solution is to create a wrapper class that synchronizes all of the methods. For an example look at the source from the collection utilities (such as Collections.synchronizedList, ect.).


I'd write a thin wrapper, where all relevant methods are synchronized, and which I'd then use everywhere in my code, instead of using the library's classes and methods directly. I'd evaluate first, for which parts of the library this is actually necessary.

Sometimes an alternative can be, to use separate instances of the library's objects for each thread. But in that case, the instances can't be shared among threads.


Look how similar problem is solved in Collections API. All collections are not thread safe by default.

But when you need a thread-safe collection you just get it with

Collections.synchronizedCollection(unsafeCollection)

The implementation is hidden from user and it's not a part of the API. You can go the same way.

0

精彩评论

暂无评论...
验证码 换一张
取 消