当前位置: 编程技术>移动开发
本页文章导读:
▪newFixedThreadPool与Callable组合用法 newFixedThreadPool与Callable结合用法
package com.sohu.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.........
▪ dwr.util.addOptions步骤的使用 dwr.util.addOptions方法的使用
网页开发中DWR框架对select下拉列表提供了很好的支持,使用 dwr.util.addOptions():用于添加列表项 dwr.util.removeAllOptions(id):用于删除列表中的所有项 以下代码演示了.........
▪ Hibernate:More than one row with the given identifier was found解决方法 Hibernate:More than one row with the given identifier was found解决办法
今天写一个Action 通过 HQL 查询一个表 出现异常 “More than one row with the given identifier was found”问题原因: 数据库出现数据异常 存在多.........
[1]newFixedThreadPool与Callable组合用法
来源: 互联网 发布时间: 2014-02-18
newFixedThreadPool与Callable结合用法
参见
http://www.iteye.com/topic/366591
Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
创建固定数目线程的线程池。
public static ExecutorService newCachedThreadPool()
创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()
创建一个单线程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
package com.sohu.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
/**
* @author qiaowang
* @date 2013-12-26 上午10:39:01
*/
public class TestThreadDemo {
private ExecutorService exec;
private int cpuNum;
private List<Future<Long>> tasks = new ArrayList<Future<Long>>();
class SumCalculator implements Callable<Long> {
private int[] numbers;
private int start;
private int end;
public SumCalculator(final int[] numbers, int start, int end) {
this.numbers = numbers;
this.start = start;
this.end = end;
}
public Long call() throws Exception {
Long sum = 0l;
for (int i = start; i < end; i++) {
sum += numbers[i];
}
return sum;
}
}
public TestThreadDemo() {
cpuNum = Runtime.getRuntime().availableProcessors();
exec = Executors.newFixedThreadPool(cpuNum);
}
public Long sum(final int[] numbers) {
// 根据CPU核心个数拆分任务,创建FutureTask并提交到Executor
System.out.println(cpuNum);
for (int i = 0; i < cpuNum; i++) {
int increment = numbers.length / cpuNum + 1;
int start = increment * i;
int end = increment * i + increment;
if (end > numbers.length)
end = numbers.length;
SumCalculator subCalc = new SumCalculator(numbers, start, end);
FutureTask<Long> task = new FutureTask<Long>(subCalc);
tasks.add(task);
if (!exec.isShutdown()) {
exec.submit(task);
}
}
return getResult();
}
public Long getResult() {
Long result = 0l;
for(Future<Long> task : tasks){
try {
Long subSum = task.get();
result +=subSum;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public void close(){
exec.shutdown();
}
public static void main(String args[]){
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 ,12};
TestThreadDemo calc = new TestThreadDemo();
Long sum = calc.sum(numbers);
System.out.println(sum);
calc.close();
}
}
参见
http://www.iteye.com/topic/366591
Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
创建固定数目线程的线程池。
public static ExecutorService newCachedThreadPool()
创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()
创建一个单线程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
[2] dwr.util.addOptions步骤的使用
来源: 互联网 发布时间: 2014-02-18
dwr.util.addOptions方法的使用
网页开发中DWR框架对select下拉列表提供了很好的支持,使用 dwr.util.addOptions():用于添加列表项 dwr.util.removeAllOptions(id):用于删除列表中的所有项 以下代码演示了removeAllOptions和addOptions方法的使用 jsp代码如下: scripttype=text/javascrip
网页开发中DWR框架对 select 下拉列表提供了很好的支持,使用
dwr.util.addOptions():用于添加列表项
dwr.util.removeAllOptions(id):用于删除列表中的所有项
以下代码演示了 removeAllOptions 和 addOptions方法的使用
jsp 代码如下:
<script type='text/javascript'src='/javacb/dwr/util.js'/>
<script type="text/javascript">
// 往下拉列表项增加元素
function addCity(){
var array = ["北京","上海","广州","深圳"];
//把数组元素的值添加到ID 为‘city’的下拉列表中,city 的text 和 //value 值相同
dwr.util.addOptions("city",array);
}
// 移除所有下拉列表项
function clearCity(){
dwr.util.removeAllOptions("sel");
}
</script>
城市:<select id="city"></select>
<input type="button" value="添加城市" onclick="addCity()">
如果指定select 元素的 text 和 value 则可以修改脚本代码,修改后的javaScript 代码如下:
<script type='text/javascript'src='/javacb/dwr/util.js'/>
<script type="text/javascript">
// 往下拉列表项增加元素
function addCity(){
var array = [
{name:"北京",id:1},
{name:"上海",id:2},
{name:"广州",id:3},
{name:"深圳",id:4}
];
// 设置数组的 name 为 text ,id 为value
dwr.util.addOptions("city",array,"id","name");
}
</script>
[3] Hibernate:More than one row with the given identifier was found解决方法
来源: 互联网 发布时间: 2014-02-18
Hibernate:More than one row with the given identifier was found解决办法
今天写一个Action 通过 HQL 查询一个表 出现异常 “More than one row with the given identifier was found”
问题原因: 数据库出现数据异常 存在多条主键不唯一的数据
问题解决: 找到数据库表 删除全部数据 然后重新导入 问题没了
我的情况:写请假表单的时候,form表单被提交了两次,导致我的@OneToOne 出错,因为里面有
两条数据了,导致主键不唯一!
今天写一个Action 通过 HQL 查询一个表 出现异常 “More than one row with the given identifier was found”
问题原因: 数据库出现数据异常 存在多条主键不唯一的数据
问题解决: 找到数据库表 删除全部数据 然后重新导入 问题没了
我的情况:写请假表单的时候,form表单被提交了两次,导致我的@OneToOne 出错,因为里面有
两条数据了,导致主键不唯一!
最新技术文章: