您的位置:首页 > 编程语言 > Java开发

Some notes for Java

2015-12-09 09:58 597 查看
1. CallableStatement: The interface used to execute SQL stored procedures, not PreparedStatement, which represents a precompiled SQL statement.

PreparedStatement usage:

PreparedStatement ps = con.prepareStatement(updateString);

ps.setInt(1, e.getValue().intValue());
ps.setString(2, e.getKey());
ps.executeUpdate();


2. Hashtable: Neither the key nor the value can be
null
.

HashMap: This implementation provides all of the optional map operations, and permitsnull values and thenull key. (TheHashMap class is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.)

3. HttpSessionListener接口的方法:

public abstract interface HttpSessionListener extends EventListener {

public abstract void sessionCreated(HttpSessionEvent paramHttpSessionEvent);

public abstract void sessionDestroyed(HttpSessionEvent paramHttpSessionEvent);

}

4. Java NIO的通道类似流,但又有些不同:

既可以从通道中读取数据,又可以写数据到通道。但流的读写通常是单向的。

通道可以异步地读写。

通道中的数据总是要先读到一个Buffer,或者总是要从一个Buffer中写入。

5. 单例模式以及spring bean单例模式如何实现线程安全(ThreadLocal)

6. sql语句

7. perm区GC, CPU每半小时升高一次,如何定位和解决?

8. 输出下面的打印结果:

public class Test {
public static void main(String[] args) {
P b = new B();
System.out.println(b.a);
}

static class P {
public int a = 11;

public P() {
a = 22;
diplay();
}

public void diplay() {
System.out.println("I am in P, value is " + a);
}
}

static class B extends P {
public int a = 33;

public B() {
a = 44;
diplay();
}

public void diplay() {
System.out.println("I am in B, value is " + a);
}
}
}


输出结果为:

I am in B, value is 0
I am in B, value is 44
22


9. java collections graph,



10 事务隔离的级别

from: http://www.cnblogs.com/zemliu/archive/2012/06/17/2552301.html
数据库隔离级别有四种,应用《高性能mysql》一书中的说明:







11. java的变量名可以$ 、字母、下划线开头,后面可以是数字、字母、下划线

12. 反向代理的功能:

from:https://en.wikipedia.org/wiki/Reverse_proxy

a. secure website

b. load balance

c. caching static content

13. cookie 和session 的区别详解

from: http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html
二者的定义:

当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,

都纪录下来。当下次你再光临同一个网站,WEB 服务器会先看看有没有它上次留下的 Cookie 资料,有的话,就会依据 Cookie

里的内容来判断使用者,送出特定的网页内容给你。 Cookie 的使用很普遍,许多有提供个人化服务的网站,都是利用 Cookie

来辨认使用者,以方便送出使用者量身定做的内容,像是 Web 接口的免费 email 网站,都要用到 Cookie。

具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案。

同时我们也看到,由于采用服务器端保持状态的方案在客户端也需要保存一个标识,所以session机制可能需要借助于cookie机制

来达到保存标识的目的,但实际上它还有其他选择。

cookie机制。正统的cookie分发是通过扩展HTTP协议来实现的,服务器通过在HTTP的响应头中加上一行特殊的指示以提示

浏览器按照指示生成相应的cookie。然而纯粹的客户端脚本如JavaScript或者VBScript也可以生成cookie。而cookie的使用

是由浏览器按照一定的原则在后台自动发送给服务器的。浏览器检查所有存储的cookie,如果某个cookie所声明的作用范围

大于等于将要请求的资源所在的位置,则把该cookie附在请求资源的HTTP请求头上发送给服务器。

cookie的内容主要包括:名字,值,过期时间,路径和域。路径与域一起构成cookie的作用范围。若不设置过期时间,则表示这

个cookie的生命期为浏览器会话期间,关闭浏览器窗口,cookie就消失。这种生命期为浏览器会话期的cookie被称为会话cookie。

会话cookie一般不存储在硬盘上而是保存在内存里,当然这种行为并不是规范规定的。若设置了过期时间,浏览器就会把cookie

保存到硬盘上,关闭后再次打开浏览器,这些cookie仍然有效直到超过设定的过期时间。存储在硬盘上的cookie可以在不同的浏

览器进程间共享,比如两个IE窗口。而对于保存在内存里的cookie,不同的浏览器有不同的处理方式

session机制。session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。

当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否已包含了一个session标识

(称为session id),如果已包含则说明以前已经为此客户端创建过session,服务器就按照session id把这个session检索出来

使用(检索不到,会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相

关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以仿造的字符串,这个session id将被在本次响应

中返回给客户端保存。保存这个session id的方式可以采用cookie,这样在交互过程中浏览器可以自动的按照规则把这个标识发送给

服务器。一般这个cookie的名字都是类似于SEEESIONID。但cookie可以被人为的禁止,则必须有其他机制以便在cookie被禁止时

仍然能够把session id传递回服务器。

经常被使用的一种技术叫做URL重写,就是把session id直接附加在URL路径的后面。还有一种技术叫做表单隐藏字段。就是服务器

会自动修改表单,添加一个隐藏字段,以便在表单提交时能够把session id传递回服务器。比如:
<form name="testform" action="/xxx">
<input type="hidden" name="jsessionid" value="ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764">
<input type="text">
</form>
实际上这种技术可以简单的用对action应用URL重写来代替。

cookie 和session 的区别:

1、cookie数据存放在客户的浏览器上,session数据放在服务器上。

2、cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗
考虑到安全应当使用session。

3、session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能
考虑到减轻服务器性能方面,应当使用COOKIE。

4、单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。

5、所以个人建议:
将登陆信息等重要信息存放为SESSION
其他信息如果需要保留,可以放在COOKIE中


14. interface可以多重继承,例如 public abstract interface Attributes extends Serializable, Cloneable

15. Applet extends Panel extends Container extends Component

16.
ThreadPoolExecutor


Core and maximum pool sizes A
ThreadPoolExecutor
will automatically adjust the pool size (see
getPoolSize
) according to the bounds set by corePoolSize (see
getCorePoolSize
) and maximumPoolSize (see
getMaximumPoolSize
). When a new task is submitted in method
execute
, and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads
running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as
Integer.MAX_VALUE
, you allow the pool to accommodate an arbitrary number of concurrent tasks. Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynamically using
setCorePoolSize
and
setMaximumPoolSize
.

如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程

如果池中的线程数>corePoolSize and <maximumPoolSize,而又有空闲线程,就给新任务使用空闲线程,如没有空闲线程,则产生新线程

如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入workQueue。(线程的空闲只有在workQueue中不再有任务时才成立)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: