下面这段代码试图帮助这个.
代码永远循环并检查是否有任何待处理的请求要处理.如果有,则创建一个新线程来处理请求并将其提交给执行程序.完成所有线程后,它会休眠60秒并再次检查待处理的请求.
public static void main(String a[]){
//variables init code omitted
ExecutorService service = Executors.newFixedThreadPool(15);
ExecutorCompletionService<Long> comp = new ExecutorCompletionService<Long>(service);
while(true){
List<AppRequest> pending = service.findPendingRequests();
int noPending = pending.size();
if (noPending > 0) {
for (AppRequest req : pending) {
Callable<Long> worker = new RequestThread(something, req);
comp.submit(worker);
}
}
for (int i = 0; i < noPending; i++) {
try {
Future<Long> f = comp.take();
long name;
try {
name = f.get();
LOGGER.debug(name + " got completed");
} catch (ExecutionException …Run Code Online (Sandbox Code Playgroud) 我正在使用Vert.x和基于事件循环的服务器相当新,而不是线程/连接模型.
public void start(Future<Void> fut) {
vertx
.createHttpServer()
.requestHandler(r -> {
LocalDateTime start = LocalDateTime.now();
System.out.println("Request received - "+start.format(DateTimeFormatter.ISO_DATE_TIME));
final MyModel model = new MyModel();
try {
for(int i=0;i<10000000;i++){
//some simple operation
}
model.data = start.format(DateTimeFormatter.ISO_DATE_TIME) +" - "+LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
r.response().end(
new Gson().toJson(model)
);
})
.listen(4568, result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
});
System.out.println("Server started ..");
}
Run Code Online (Sandbox Code Playgroud)
根据目前的答案编辑:
对于hibernate支持,我有一个这样的域对象注释.
@Entity
@Table(name = "INPUT")
public class AppInput {
/**
* Unique id for this request
*/
@Id
@GeneratedValue
@Column(name = "INPUT_ID")
private long requestId;
/**
*
*/
@Column(name = "EMAIL_ID")
private String emailId;
/**
*
*/
@Column(name = "REQUEST_DATE")
private Date requestDate;
/**
*
*/
@Column(name = "INPUT_STATUS")
private char status;
/**
*
*/
@Column(name = "EXPECTED_ORDER_DATE")
private Date expectedOrdDt;
//Getter and setters
}
Run Code Online (Sandbox Code Playgroud)
属性emailId是一个外键,指的是User表中的say emailId列.让我说我向AppInput.java添加这样的属性
private User userDetails;
我如何对此进行注释,以便每当我从db获取AppInput时,相应的用户详细信息也会被填充?
我想计算x幂y,x,y都是双值.为什么java给我一个编译错误?这样做的最佳方法是什么?
我目前正在使用以下方法:
x^y // attempt to calculate (x pow y)
Run Code Online (Sandbox Code Playgroud)
谢谢.
我的项目结构有这些基础包.
核心,Web,通用软件包将包含子软件包.我编译了src dir下的所有java文件,并将类文件复制到了dir.
<javac srcdir="${src}" destdir="${build}/myapp" debug="true">
<classpath refid="compile.classpath"/>
<classpath refid="ant.classpath" />
</javac>
Run Code Online (Sandbox Code Playgroud)
现在我想用一个属于abccore及其子包的类文件构建一个名为core.jar的jar.类似地,web.jar和common.jar.有人可以为jar任务提供一个示例代码来合并吗?
谢谢
我只是在玩Java中的线程.我有一个实现runnable的类.
public class MyThread implements Runnable{
private boolean finished;
//Other variables
public void run(){
//Thread code
}
}
Run Code Online (Sandbox Code Playgroud)
我的理解是MyThread类型的每个线程都有自己的成员变量副本,并且不需要同步对这些成员变量的写入.这个假设是否正确?如果正确,访问需要同步的内容?有人可以提供大纲或伪代码.谢谢.