Servlet Tread tips

本文探讨了当Servlet处理多个客户端请求时如何确保线程安全。针对非局部变量可能引发的问题,提出了四种解决方法:使用synchronized关键字修饰doGet方法;在方法内部使用synchronized块;通过局部变量操作非局部变量;接受不进行同步带来的后果。

If your servlets only read from the request, write to the response, and save information in local variables, you needn't worry about the interaction among these threads. Once any information is saved in nonlocal variables,  however, you must be aware that each of these client threads has the ability to manipulate a servlet's nonlocal variables. (Java Servlet Programming, Jason Hunter)

we can deal with this problem in 4 methods:

1. in the declare, use " public synchronized void doGet(...,...)"

2.in the function, use " synchronized(this){ //do things about nonlocal variables} "

3.in the function, assign the value of nonlocal variable to a (temp) local variable, then after " synchronized(this){ //do things about nonlocal variables, and assign the value of nonlocal variable to a (temp) local variable} "

4.  decide that we are willing to suffer the consequences of ignoring synchronization issues.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值