- 浏览: 47087 次
最新评论
文章列表
介绍了Servlet多线程机制,通过一个实例并结合Java 的内存模型说明引起Servlet线程不安全的原因,给出了保证Servlet线程安全的三种解决方案,并说明三种方案在实际开发中的取舍。
关键字:Servlet 线程安全 同步 Java内存模型 实例变量
Servlet/JSP技术和ASP、PHP等相比,由于其多线程运行而具有很高的执行效率。由于Servlet/JSP默认是以多线程模式执行的,所以,在编写代码时需要非常细致地考虑多线程的安全性问题。然而,很多人编写Servlet/JSP程序时并没有注意到多线程安全性的问题,这往往造成编写的程序在少量用户访问时没有任何问题,而在 ...
- 2013-12-18 17:48
- 浏览 297
- 评论(0)
【问题描述】最近公司安排我面试Java的FreshMan,面试者一般是工作1年多点的新人(这里我就装老一下,其实我也才工作3年不到),在被问及Struts1和Struts2的Action的线程安全问题的时候,大多是支支吾吾,答不出所以然。所以在这里我整理一下我个人的理解。
【问题答案】
这是由于Servlet的工作原理产生的。我们先来简单回顾一下Servlet的生命周期“初始化->init->service->destroy->卸载”。
这里大家都知道,我们在web.xml里面定义一个servlet的时候,我们可以给他们设置一个“load-on-startup” ...
- 2013-12-18 14:46
- 浏览 252
- 评论(0)
public static int test(){
int i = 0;
try {
i++;
throw new Exception("eeee");
} catch( Exception e) {
return i;
} finally {
i++;
}
}
public static String test2(){
String test = "111";
try {
test = & ...
- 2013-12-18 12:45
- 浏览 371
- 评论(0)
套接字通道
在套接字通道方面的改进是提供了对非阻塞I/O和多路复用I/O的支持。传统的流的I/O操作是阻塞式的。在进行I/O操作的时候,线程会处于阻塞状态等待操作完成。NIO中引入了非阻塞I/O的支持,不过只限于套接字I/O操作。所有继承自SelectableChannel的通道类都可以通过configureBlocking方法来设置是否采用非阻塞模式。在非阻塞模式下,程序可以在适当的时候查询是否有数据可供读取。一般是通过定期的轮询来实现的。
多路复用I/O是一种新的I/O编程模型。传统的套接字服务器的处理方式是对于每一个客户端套接字连接,都新创建一个线程来进行处理。创建线程是很耗时 ...
- 2013-12-17 20:03
- 浏览 419
- 评论(0)
Java线程间通信-回调的实现方式
Java线程间通信是非常复杂的问题的。线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互。
比如举一个简单例子,有一个多线程的类,用来计算文件的MD5码,当多个这样的线程执行的时候,将每个文件的计算的结果反馈给主线程,并从控制台输出。
线程之间的通讯主要靠回调来实现,回调的概念说得抽象了很难理解,等于没说。我就做个比喻:比如,地铁的列车上有很多乘客,乘客们你一句他一句的问“到XX站了没?”,列车长肯定会很烦!于是乎,车长告诉大家,你们都各干各的事情,不用问了,到站了我会通知你们的。 这 ...
- 2013-12-16 14:19
- 浏览 363
- 评论(0)
Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch
thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,a≤b≤c≤d)
The solution set must not contain duplica ...
- 2013-11-26 19:58
- 浏览 401
- 评论(0)
Comparable与Comparator
Comparable和 Comparator 都是用来实现集合中元素的比较、排序的。
只是 Comparable 是在集合内部定义的方法实现的排序,而Comparator 是在集合外部实现的排序,
所以,如想实现排序,就需要在集合外定义 Comparat ...
- 2013-11-26 15:54
- 浏览 336
- 评论(0)
Longest Common Prefix
Total Accepted:2025Total
Submissions:7610My Submissions
Write a function to find the longest common prefix string amongst an array of strings.
这道题很简单,其实最重要的是边界条件,一定要思维缜密,写代码要尽力达到从上到下一次写完而不会出现边界问题的地步!
public class Solution {
public String longestCommonP ...
- 2013-11-06 20:16
- 浏览 238
- 评论(0)
Givennnon-negative integersa1,a2,
...,an, where each represents a point at coordinate (i,ai).nvertical
lines are drawn such that the two endpoints of lineiis at (i,ai) and (i,
0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
...
- 2013-11-05 20:40
- 浏览 331
- 评论(0)
mplement regular expression matching with support for'.'and'*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char * ...
- 2013-11-05 19:44
- 浏览 373
- 评论(0)
Palindrome Number
Total Accepted:2395Total
Submissions:8279My Submissions
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer ...
- 2013-11-04 18:39
- 浏览 400
- 评论(0)
package nlp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
class ChartLine{
public String wordAttr;
public int start;
public int end;
public ChartLine(String wordAttr,int start,int end){
this.wordAttr=wordAttr;
this.start= ...
- 2013-11-03 18:40
- 浏览 1602
- 评论(0)
本文作者王大锤实现了最长正向匹配算法与最长逆向匹配算法,同时,也顺便写了将两种算法判断取最优的一种的算法。
如下:
package nlp;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class Proj2 {
private String dicPath;
private ...
- 2013-11-03 18:38
- 浏览 1179
- 评论(0)
String to Integer (atoi)
Implementatoito convert a string to an integer.
Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes:It is intended for this problem to be specified vaguely (ie, n ...
- 2013-10-28 18:04
- 浏览 369
- 评论(0)
Reverse Integer
Total Accepted:2839Total
Submissions:6946My Submissions
Reverse digits of an integer.
Example1:x = 123, return 321Example2:x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points f ...
- 2013-10-28 15:59
- 浏览 212
- 评论(0)