spring 线程池ThreadPoolTaskExecutor
<bean id ="taskExecutor" class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
<!-- 线程池维护线程的最少数量 -->
<property name ="corePoolSize" value ="5" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name ="keepAliveSeconds" value ="30000" />
<!-- 线程池维护线程的最大数量 -->
<property name ="maxPoolSize" value ="1000" />
<!-- 缓冲队列 -->
<property name ="queueCapacity" value ="200" />
ThreadPoolTaskExecutor poolTaskExecutor = (ThreadPoolTaskExecutor)contex.getBean("taskExecutor");
利用线程池启动线程
Thread myThread = new Thread(myRunnable);
poolTaskExecutor.execute(myThread);
获取当前线程池活动的线程数:
int count = poolTaskExecutor.getActiveCount();
Spring 里的mergePropertiesIntoMap 方法:
mergePropertiesIntoMap(Properties props, Map map) {
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (props != null) {
for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
String key = (String) en.nextElement();
Object value = props.getProperty(key);
if (value == null) {
// Potentially a non-String value...
value = props.get(key);
}
map.put(key, value);
}
}
}
props 是一个Properties 的实例 (其实Properties相当于一个 key 和value 都是String 类型 的Map而已), for (Enumeration en = props.propertyNames(); en.hasMoreElements();) { } 如此遍历map 好高级哦,
Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream
.
但是 -------〉
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
Hashtable environment1;
@Test
public void testClone() throws Exception {
environment1 = new Hashtable<String, Integer>(2);
environment1.put("one", 1);
environment1.put("two", 2);
this.testInitialContext(environment1);
}
public void testInitialContext(Hashtable<?,?> environment) throws NamingException
{
if (environment != null) {
environment = (Hashtable)environment.clone();//这样保证我们 在function testInitialContext中的操作不改变原始的environment. BUT: the keys and values are not cloned. This is a relatively expensive operation.
environment.remove("one");
if(environment1==environment){
System.out.println("........why clone?");
}
}
}