Java Servlet线程处理
我的登录servlet收到一个请求,响应后休眠1分钟 我的目标是允许两个请求具有访问权限,如果第三个事务到达,它必须等待其他线程完成各自的任务 我的代码检查哪些线程正在休眠,哪些线程没有休眠。 我遇到的问题是,当线程唤醒它时,我希望能够为它分配任务。我是否需要轮询或是否有某种同步方法?我该如何处理这个问题Java Servlet线程处理,java,multithreading,servlets,Java,Multithreading,Servlets,我的登录servlet收到一个请求,响应后休眠1分钟 我的目标是允许两个请求具有访问权限,如果第三个事务到达,它必须等待其他线程完成各自的任务 我的代码检查哪些线程正在休眠,哪些线程没有休眠。 我遇到的问题是,当线程唤醒它时,我希望能够为它分配任务。我是否需要轮询或是否有某种同步方法?我该如何处理这个问题 public class Home extends HttpServlet { static HttpSession ses; static List<Thread&g
public class Home extends HttpServlet
{
static HttpSession ses;
static List<Thread> homethread = new ArrayList<Thread>();
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ses = request.getSession();
homethread.add(Thread.currentThread());
for(Thread th : homethread)
{
if(th.getState()!=Thread.State.TIMED_WAITING)
{
System.out.println("This Thread is Not in Waiting State -> "+th.getName()+"Its State -> "+th.getState());
}
else
{
System.out.println("A Thread Is In Waiting State -> "+th.getName()+"Its State -> "+th.getState());
}
System.out.println("Thread Details->"+th+"Thread State->"+th.getState().TIMED_WAITING);
}
response.setContentType("text/html");
PrintWriter writter = response.getWriter();
writter.println
(
"<!DOCTYPE html>" +
"<html>" +
"<body>" +
"<img src=\"C:\\Users\\surya-pt3101\\Pictures\\Screenshots\\Screenshot (1).png\">"+
"<h1 style=\"text-align:center;\">Provide Your Choice</h1>"+
"<form action=\"Upload\" method=\"POST\" enctype=\"multipart/form-data\">" +
"<div style=\"text-align:center\">Upload File :<input type=\"file\" name=\"UploadedFile\" multiple/><input type=\"submit\" value=\"Upload\"><br><br>"+
"</form>" +
"<div style=\"text-align:center\">View Files :<input type=\"submit\" formaction=\"View\" formmethod=\"post\" value=\"View\"/><br><br>" +
"<div style=\"text-align:center\"><input type=\"submit\" formaction=\"LogOut\" formmethod=\"post\" value=\"LogOut\"/>" +
"</body>" +
"</html>"
);
response.flushBuffer();
try
{
Thread.currentThread().sleep(60000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
公共类Home扩展了HttpServlet
{
静态httpses;
静态列表homethread=newArrayList();
受保护的无效服务(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException
{
ses=request.getSession();
添加(Thread.currentThread());
对于(线程th:homethread)
{
if(th.getState()!=Thread.State.TIMED\u WAITING)
{
System.out.println(“此线程未处于等待状态->”+th.getName()+“其状态->”+th.getState());
}
其他的
{
println(“线程处于等待状态->”+th.getName()+“其状态->”+th.getState());
}
System.out.println(“线程详细信息->”+th+“线程状态->”+th.getState().TIMED_WAITING);
}
response.setContentType(“text/html”);
PrintWriter Writer=response.getWriter();
writer.println
(
"" +
"" +
"" +
""+
“提供您的选择”+
"" +
“上载文件:”+ "" + “查看文件:
”+ "" + "" + "" ); response.flushBuffer(); 尝试 { Thread.currentThread().sleep(60000); } 捕捉(中断异常e) { e、 printStackTrace(); } } }
根据定义,servlet必须是线程安全的。Tomcat(或任何其他servlet容器)内部包含一个线程池,用于执行您的请求。因此,我认为生成您自己的线程没有多大意义(当然,如果我们谈论的不是真正困难的计算)。为了使类线程安全,有几个选项:常见的同步方法(如synchronized/wait/notify)或更高级的结构(参见java.util.concurrent包)。应避免使用其他非线程安全的共享对象,如ArrayList。据我所知,您希望在同一时间执行不超过2个线程。您编写了“第三个请求必须在已完成的线程中处理”——在我看来,如果您能够在servlet线程中执行所有需要的计算,这是不合理的。 下面是一个使用java.util.concurrent.Semaphore类的代码段,该类一次只允许处理“n”个线程,其中n是许可数
public class Home extends HttpServlet {
private final Semaphore semaphore = new Semaphore(2);
protected void service(HttpServletRequest request, HttpServletResponse response) {
try {
semaphore.acquire();
// Do what you need
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
semaphore.release();
}
}
}
UPD:加载程序示例
假设您有一个带有字段和提交按钮的表单。单击时,需要执行一些长操作,因此您希望向用户显示一个加载程序gif
html文件的一部分:
输入值:
发送
JS(使用jQuery):
函数sendRequest(内容){
var有效载荷={};
payload.content=内容;
返回$.ajax({
键入:“POST”,
contentType:“应用程序/json”,
url:“/您的端点”,
数据:JSON.stringify(有效负载)
})
}
$(“#提交”)。单击(函数(){
$(“#加载程序”).show();
$('submitBtn').prop('disabled',true);
const response=sendRequest($('#content').val());
响应。然后((结果)=>{
$(“#加载程序”).hide();
$('submitBtn').prop('disabled',false);
$('#content').val(“”);
警报(结果);
}).catch(错误=>{
$(“#加载程序”).hide();
$('submitBtn').prop('disabled',false);
$('#content').val(“”);
警报(错误);
});
返回false;
});
希望能有所帮助。您是否已经编写了一些可以共享的代码?我会用4xx代码和类似“x秒后重试”的消息进行回复,谢谢@Anton我以前使用同步块尝试过同样的方法,但如果第三位用户请求URL,我希望他们的页面显示类似gif的(处理)在前两个线程中的任何一个线程完成后,我希望在不刷新的情况下自动(即)向第三个用户提供响应。Hi@s.suryaPrakash。在后端使用该信号量方案和在前端使用JS很容易实现。每个用户发送请求,显示gif,当服务器响应时(无论是立即响应还是分钟后响应),gif将被接收到的内容替换。只需要一个请求。例如,可以使用JQuery轻松实现此行为。如果你需要一个代码示例,那就写吧。是的,我理解,但如果我能得到一些示例代码,那就太有帮助了。@Anton Shelenkov,提前谢谢你