workingmemorysynchronizedproxy.java

来自「drools 一个开放源码的规则引擎」· Java 代码 · 共 34 行

JAVA
34
字号
package org.drools.util.concurrent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.drools.WorkingMemory;

/**
 * Helper class that synchronizes all public methods of WorkingMemory via a
 * <code>java.lang.reflect.Proxy</code>
 */
public class WorkingMemorySynchronizedProxy {

    /**
     * Returns an instance of a proxy class for the specified WorkingMemory in which
     * all method calls are synchronized.
     * @param workingMemory
     * @return A proxy that invokes workingMemory methods within a synchronized block.
     */
    public static WorkingMemory createProxy(final WorkingMemory workingMemory) {
        return (WorkingMemory) Proxy.newProxyInstance(
                WorkingMemory.class.getClassLoader(),
                new Class[]{WorkingMemory.class},
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        synchronized(workingMemory) {
                            return method.invoke(workingMemory, args);
                        }
                    }
                });
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?