并行执行静态同步方法

发布于 2024-12-15 07:02:21 字数 109 浏览 2 评论 0原文

我正在使用具有静态同步方法的第三方库,我想并行执行此方法而不修改其源。

我能做些什么 ?

我应该使用设置新上下文类加载器的线程工厂创建执行程序服务吗?

谢谢

I'm using a thrid-party lib that has a static synchronized method, i want to execute this method in parallel without modifiying its source.

What can i do ?

Should i create an executor service with a thread factory that sets a new context class loader ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏璃陌 2024-12-22 07:02:21

静态同步方法意味着它锁定在类上。只有一个线程可以获取该类的锁。您甚至不能子类化此类,因为它不允许您重写静态方法。

恐怕,你什么也做不了。

A static synchronized method means its locked on the class. only one thread can grab the lock on the class. You can not even subclass this class at it won't allow you to override the static method.

I am afraid, you can not do anything.

把昨日还给我 2024-12-22 07:02:21

理论上,您当然可以在单独的类加载器中加载该库两次。练习可能会比较困难。

为了链接到不同类加载器中的副本,您还需要在这些类加载器(或子类加载器)中多次加载代码。代码实例之间的公共通信应通过加载到公共基类中的代码进行。将反射保持在绝对最低限度。不同类加载器加载的类即使具有相同的名称也会不兼容,这通常表现为奇怪的 ClassCastException。例如,运行时对象和转换类型具有相同的名称。下列的?您确定要这样做吗?

该方法同步可能是有原因的。大概原因实在是太糟糕了。那里会有可变的静态。当您多次加载代码时,这些可变静态数据将会有多个副本。如果它们只是缓存,那可能还不错。然而,几乎可以肯定,图书馆真的很糟糕。

注意:线程上下文类加载器除了把事情搞砸之外几乎没有什么关系。

You could certainly in theory load the library twice in separate class loaders. Practice might be more difficult.

In order to link to link to the copy in different class loaders you'll need to also load your code multiple times in those class loaders (or a child class loader). Common communication between the code instances should go through code loaded into a common base class. Keep reflection to an absolute minimum. Classes loaded by different class loaders will be incompatible even though they have the same name, which usually manifests as bizarre ClassCastExceptions. For instance, the runtime object and cast type have the same name. Following? Are you sure you want to do this?

Probably there is a reason why the method is synchronised. Probably the reason is really bad. There will be mutable statics in there. As you've loaded the code more than once there will be multiple copies of these mutable statics. If they are just caches that might not be too bad. However, the library almost certainly really sucks.

NB: The thread context class loader has very little to do with anything except screwing things up.

公布 2024-12-22 07:02:21

如果您想并行执行该方法,则必须在不同的进程中执行它 - 即并行运行您的程序两次。

If you want to execute that method in parallel you would have to execute it in different process - i.e. run your program twice in parallel.

妖妓 2024-12-22 07:02:21

如果在两个不同的类加载器中加载该类,则可以独立运行每个类加载器中的方法。但是,我怀疑该类的作者有充分的理由将方法设置为静态同步,我会检查这实际上不是必需的。

If you load the class in two different class loaders, you can run the methods in each class loader independently. However, I suspect the writer of the class had a good reason to make the method static synchronized I would check this isn't actually required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文