JSP使用多线程 想要开多个线程去访问远程的数据接口(因为需要模拟登陆所以返回较慢,一般需要1-5s),同时在页面上实时显示接口返回数据,想问一下这种怎么做才好?(接口返回数据耗时基本是随机的,所以用callable的话需要等那个较慢的;而用runnable的话怎么在run函数中调用jsp的out或response又是个问题)刚接触jsp还请各位说得详细点,谢谢~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴一下两种方式的实验代码,Runnable的会报IOExceptionCallable:<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page import="java.util.concurrent.*" %><html><head> <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"> <title>Title</title></head><%!public class MyCallable implements Callable{ private String url; private String param; public MyCallable(String url, String param){ this.url=url; this.param=param; } public String call() throws Exception{ int ran=(int)(Math.random()*5000); Thread.sleep(ran); return Thread.currentThread().getName()+", "+this.url+", "+this.param+" sleeped "+ran+" milliseconds!"; }}%><% out.println(String.format("%256s","")); ExecutorService pool = Executors.newFixedThreadPool(5); Callable[] c=new Callable[5]; Future[] f=new Future[5]; for(int i=0;i<5;i++) c[i]=new MyCallable("A"+i,"B"+i); for(int i=0;i<5;i++) f[i]=pool.submit(c[i]); for(int i=0;i<5;i++){ out.println(f[i].get().toString()+"</br>"); out.flush(); } pool.shutdown();%>Runnable:<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8"> <title>Title</title></head><%! public static void myFunc(String Bits, javax.servlet.jsp.JspWriter myOut){ try{ myOut.println("<div>"+Bits+"</div>"); } catch(Exception eek) { } } public class MyRunnable implements Runnable{ private String url; private String param; private javax.servlet.jsp.JspWriter myOut; public MyRunnable(String url, String param, javax.servlet.jsp.JspWriter myOut){ this.url=url; this.param=param; this.myOut=myOut; } public void run(){ myFunc(Thread.currentThread().getName()+", "+this.url+", "+this.param, this.myOut); } }%><% for(int i=0;i<5;i++) new Thread(new MyRunnable("A"+i, "B"+i, out)).start();%> 如何防止用户在TextBox中输入Html标签....... struts1中的action代码在struts2中的写法(新手请教,望详解) 求教JS中遍历页面中的${list} Java连接mysql 想请教一下,关于EJB中使用多线程的问题。 MyEclipse 6.0如何调用进口包 ACCESS的临时磁盘空间不足,引起整个IIS死掉...如何处理? 剛學程序的困繞﹐大家解釋一下吧﹗'\u0022'和"\t" 兄弟们啊!一个关于EJB的问题!好急! 请问如何配置jsp开发环境? action处理json xml里面$USER_HOME$代表引用的什么
Callable:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.concurrent.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
<title>Title</title>
</head>
<%!
public class MyCallable implements Callable{
private String url;
private String param; public MyCallable(String url, String param){
this.url=url;
this.param=param;
} public String call() throws Exception{
int ran=(int)(Math.random()*5000);
Thread.sleep(ran);
return Thread.currentThread().getName()+", "+this.url+", "+this.param+" sleeped "+ran+" milliseconds!";
}
}
%>
<%
out.println(String.format("%256s",""));
ExecutorService pool = Executors.newFixedThreadPool(5);
Callable[] c=new Callable[5];
Future[] f=new Future[5];
for(int i=0;i<5;i++)
c[i]=new MyCallable("A"+i,"B"+i);
for(int i=0;i<5;i++)
f[i]=pool.submit(c[i]);
for(int i=0;i<5;i++){
out.println(f[i].get().toString()+"</br>");
out.flush();
}
pool.shutdown();
%>Runnable:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
<title>Title</title>
</head>
<%!
public static void myFunc(String Bits, javax.servlet.jsp.JspWriter myOut){
try{ myOut.println("<div>"+Bits+"</div>"); }
catch(Exception eek) { }
}
public class MyRunnable implements Runnable{
private String url;
private String param;
private javax.servlet.jsp.JspWriter myOut; public MyRunnable(String url, String param, javax.servlet.jsp.JspWriter myOut){
this.url=url;
this.param=param;
this.myOut=myOut;
} public void run(){
myFunc(Thread.currentThread().getName()+", "+this.url+", "+this.param, this.myOut);
}
}
%>
<%
for(int i=0;i<5;i++)
new Thread(new MyRunnable("A"+i, "B"+i, out)).start();
%>