Java 原型bean之间的共享bean是线程安全的吗?
我有一个Java 原型bean之间的共享bean是线程安全的吗?,java,spring,dependency-injection,Java,Spring,Dependency Injection,我有一个prototypebean,我正在多线程应用程序中使用它。我正在使用ThreadPoolExecutionService来运行原型的不同实例 我想收集一些关于线程正在运行的代码的统计信息,包括创建几个变量,并在原型运行时增加这些变量 我考虑过如何创建一个单例bean,并将bean注入原型,然后调用一个方法来设置统计信息 一些虚拟代码来说明这一点:我对虚拟代码做了一些修改,这要归功于 public static void main( String[] args ) { Appli
prototype
bean,我正在多线程应用程序中使用它。我正在使用ThreadPoolExecutionService
来运行原型的不同实例
我想收集一些关于线程正在运行的代码的统计信息,包括创建几个变量,并在原型运行时增加这些变量
我考虑过如何创建一个单例
bean,并将bean注入原型
,然后调用一个方法来设置统计信息
一些虚拟代码来说明这一点:我对虚拟代码做了一些修改,这要归功于
public static void main( String[] args )
{
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
PrintThread printThread1 = (PrintThread) ctx.getBean("printThread");
printThread1.setName("Thread 1");
PrintThread printThread2 = (PrintThread) ctx.getBean("printThread");
printThread2.setName("Thread 2");
PrintThread printThread3 = (PrintThread) ctx.getBean("printThread");
printThread3.setName("Thread 3");
PrintThread printThread4 = (PrintThread) ctx.getBean("printThread");
printThread4.setName("Thread 4");
PrintThread printThread5 = (PrintThread) ctx.getBean("printThread");
printThread5.setName("Thread 5");
printThread1.start();
printThread2.start();
printThread3.start();
printThread4.start();
printThread5.start();
}
带注入的打印线程:
@Component
@Scope("prototype")
public class PrintThread extends Thread{
@Inject
private StatGatherer statGatherer;
@Override
public void run() {
System.out.println(getName() + " is running");
//is this threadsafe?
statGatherer.writeSomeStat();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + " is running");
}
}
编辑:threadsafe统计类的示例。代码包含三个映射,它们可以通过同步方法递增
@Component("statistics")
public class StatGathererImpl implements StatGatherer {
private ConcurrentHashMap<Character, Integer> mappingType = new ConcurrentHashMap<>();
private ConcurrentHashMap<Character, Integer> mappingGroup = new ConcurrentHashMap<>();
private ConcurrentHashMap<Character, Integer> mappingDirection = new ConcurrentHashMap<>();
@Override
public synchronized void incrementMappingType(char mapType) {
if(mappingType.keySet().contains(mapType)){
mappingType.replace(mapType, mappingType.get(mapType), mappingType.get(mapType)+1);
}else{
mappingType.put(mapType, 1);
}
}
@Override
public synchronized void incrementMappingGroup(char mapGroup) {
if(mappingGroup.keySet().contains(mapGroup)){
mappingGroup.replace(mapGroup, mappingGroup.get(mapGroup), mappingGroup.get(mapGroup)+1);
}else{
mappingGroup.put(mapGroup, 1);
}
}
@Override
public synchronized void incrementMappingDirection(char mapDirection) {
if(mappingDirection.keySet().contains(mapDirection)){
mappingDirection.replace(mapDirection, mappingDirection.get(mapDirection), mappingDirection.get(mapDirection)+1);
}else{
mappingDirection.put(mapDirection, 1);
}
}
}
@组件(“统计”)
公共类STATGATHERRIMPL实现STATGATHER{
私有ConcurrentHashMap映射类型=新ConcurrentHashMap();
私有ConcurrentHashMap映射组=新ConcurrentHashMap();
私有ConcurrentHashMap映射方向=新ConcurrentHashMap();
@凌驾
公共同步的void incrementMappingType(char-mapType){
if(mappingType.keySet().contains(mappingType)){
mappingType.replace(mapType,mappingType.get(mapType),mappingType.get(mapType)+1);
}否则{
mappingType.put(mapType,1);
}
}
@凌驾
公共同步的void incrementMappingGroup(char mapGroup){
if(mappingGroup.keySet()包含(mapGroup)){
mappingGroup.replace(mapGroup,mappingGroup.get(mapGroup),mappingGroup.get(mapGroup)+1);
}否则{
mappingGroup.put(mapGroup,1);
}
}
@凌驾
公共同步的void incrementMappingDirection(char mapDirection){
if(mappingDirection.keySet().contains(mappingDirection)){
mappingDirection.replace(mapDirection,mappingDirection.get(mapDirection),mappingDirection.get(mapDirection)+1);
}否则{
mappingDirection.put(mapDirection,1);
}
}
}
只要writeMestat()
中的代码是线程安全的,就可以工作。我们使用封装了度量库的度量bean来做类似的事情。这是完美的。你有没有关于如何使方法线程安全的例子?@Sam没有任何代码就不可能回答这个问题。线程安全是一个广泛的领域。@JEY我将为threadsafe类添加一些基本代码。