Web开发基础_Servlet学习_0011_Servlet中的多线程安全问题与Servlet运行原理

Servlet中的多线程安全问题

Servlet运行原理

Servlet中的多线程安全问题 演示

案例演示:

工程案例目录结构

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.study</groupId>
  <artifactId>Servlet03</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
  		<groupId>javaee</groupId>
  		<artifactId>javaee-api</artifactId>
  		<version>5</version>
  	</dependency>
  </dependencies>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Servlet04</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  

  <servlet>
  	<servlet-name>suibian</servlet-name>
  	<servlet-class>web.SuibianServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>suibian</servlet-name>
  	<url-pattern>/suibian</url-pattern>
  </servlet-mapping>

</web-app>

SuibianServlet.java

package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SuibianServlet extends HttpServlet{
	
	private double sal = 3000;

	@Override
	protected void service(HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		
		//涨工资
		sal += 100;
		
		//模拟网络延迟
		try{
			Thread.sleep(8000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		
		//输出最新工资
		res.setContentType("text/html;charset=utf-8");
		PrintWriter pw	= res.getWriter();
		pw.println("<h1>工资:"+sal+"</h1>");
		pw.close();
	}
	
	

}

 同时使用两个浏览器请求同一路径:http://localhost:8080/Servlet04/suibian;会发现最终显示效果如下:

与我们希望的3100,3200不符。

IE:

Chrome:

 

添加线程锁后:

package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SuibianServlet extends HttpServlet{
	
	private double sal = 3000;

	@Override
	protected void service(HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		synchronized(this){
			//涨工资
			sal += 100;
			
			//模拟网络延迟
			try{
				Thread.sleep(8000);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			
			//输出最新工资
			res.setContentType("text/html;charset=utf-8");
			PrintWriter pw	= res.getWriter();
			pw.println("<h1>工资:"+sal+"</h1>");
			pw.close();
		}
		
	}
	
	

}

 同时使用两个浏览器请求同一路径:http://localhost:8080/Servlet04/suibian;会发现最终显示效果如下:

与我们希望的3100,3200相符合。线程安全解决。

IE:

Chrome:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值