AOPのSyncInterceptor
来源:互联网 发布:淘宝全屏店招尺寸 编辑:程序博客网 时间:2024/06/06 03:20
1.Count.java
package examples.di;
public interface Count {
public void add();
public int get();
}
2.CountImpl.java
package examples.di.impl;
import examples.di.Count;
public class CountImpl implements Count {
private int _count = 0;
public void add() {
int a = _count;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
_count = a + 1;
System.out.println(a);
}
public int get() {
return _count;
}
}
3.AopSyncClient.java
package examples.di.main;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import examples.di.Count;
public class AopSyncClient {
private String PATH = "Count.dicon";
private Count _count = null;
public void init() {
S2Container container = S2ContainerFactory.create(PATH);
_count = (Count) container.getComponent(Count.class);
}
public void start() {
System.out.println("count: " + _count.get());
Runnable r = new Runnable() {
public void run() {
_count.add();
}
};
Thread[] thres = new Thread[5];
for (int i=0; i<5; i++) {
thres[i] = new Thread(r);
thres[i].start();
}
for (int i=0; i<5; i++) {
try {
thres[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("count: " + _count.get());
}
public static void main(String[] args) {
AopSyncClient asc = new AopSyncClient();
asc.init();
asc.start();
}
}
4.Count.dicon
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components>
<component class="examples.di.impl.CountImpl">
<aspect>
<component class="org.seasar.framework.aop.interceptors.SyncInterceptor"/>
</aspect>
</component>
</components>
如果不加<aspect>则每次运行thres[i] = new Thread(r);的时候都会生成一个新的实例
- AOPのSyncInterceptor
- AOPのTraceInterceptor
- AOPのDelegateInterceptor
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- AOP
- FBI阅人术——用最短的时间了解一个人
- 一个JS函数
- C#反射使用入门
- 远程桌面无法连接的解决办法
- 建在含有null值的字段上的索引会包含null值数据
- AOPのSyncInterceptor
- SEO要学会预测热门关键词
- WCF服务以控制台程序为宿主时的跨域访问问题(附源码)
- ifconfig 用法详解
- 软件工程中使用到的文档
- 无线排队机解决方案
- ZOJ-2109FatMouse' Trade
- 随笔,系统从哪里开始
- BusinessObject Enterprise SDK应用开发(上)