final vs static final in java

For final, it can be assigned different values at runtime when initialized. For example

Class Test{
  public final int a;
}
Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t2.a = 20; //fixed

Thus each instance has different value of field a.

For static final, all instances share the same value, and can’t be altered after first initialized.

Class TestStatic{
      public static final int a;
}
Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t1.a = 20;   // ERROR, CAN'T BE ALTERED AFTER THE FIRST INITIALIZATION.

private final static attribute vs private final attribute
Static vs Instance Variables: Difference?

Spring RestTemplate 消息转换2种使用方式

拦截器

public class MyXmlInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);
    HttpHeaders headers = response.getHeaders();

    // you'd want to check if the value needs to be changed
    if (headers.containsKey("Content-Type")) {
        headers.remove("Content-Type");
    }

    headers.add("Content-Type", "application/xml");

    return response;
}
RestTemplate t = new RestTemplate();
t.getInterceptors().add(new MyXmlInterceptor());

自定义

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);

Unsafe.park & Object.wait

如果是搞应用程序的程序员,就不应该使用这两种方法中的任何一种。两种都太底层了,建议使用 java.util.concurrent.locks , 毕竟类库的就是让你多快好省的写代码。

park 直接操作线程,线程作为参数直接传入,线程会sleep知道调用了 unpark 方法。这种操作需要明确知道那个线程需要 block/unblock。可以使用 java.util.concurrent.locks.LockSupport 完成操作。

park 操作可以理解为Thread.sleep 操作,不会释放锁资源,使用不当会造成死锁。park 操作能够设置一个 blocker,并通过 getBlocker 获取阻塞的信息,方便做一些调试之类的工作。

wait 操作会释放锁。wait 操作必须在同步语句块。如果不在同步语句块会抛出 IllegalMonitorStateException 异常。使用 notify 和 notifyAll 唤醒线程。

wait用于锁机制,sleep不是,这就是为啥sleep不释放锁,wait释放锁的原因,sleep是线程的方法,跟锁没半毛钱关系,wait,notify,notifyall 都是Object对象的方法,是一起使用的,用于锁机制

It all eventually makes its way down to the OS’s scheduler, which hands out timeslices to processes and threads.

sleep(n) says “I’m done with my timeslice, and please don’t give me another one for at least n milliseconds.” The OS doesn’t even try to schedule the sleeping thread until requested time has passed.

yield() says “I’m done with my timeslice, but I still have work to do.” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.

wait() says “I’m done with my timeslice. Don’t give me another timeslice until someone calls notify().” As with sleep(), the OS won’t even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).

Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if yield() had been called, so that other processes can run.

You rarely need yield(), but if you have a compute-heavy app with logical task boundaries, inserting a yield() might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren’t free). Measure and test against goals you care about, as always.

继续阅读“Unsafe.park & Object.wait”

热河 – 李志

读到古龙的《多情剑客无情剑》里一个场景:一对厨子,饭馆打烊后,他们在后厨,自己给自己炒了盘菜,找点小酒,很惬意的饮食一番,消磨了那一两个时辰的舒服时光。古龙说:他们还活着,就是因为一天还有那么一两个时辰。

热河路就像八十年代的金坛县,梧桐垃圾灰尘和各式各样的杂货店
人们总是早早的离开拉上卷帘门,在天黑前穿上毛衣点一根烟
热河路有一家开了好多年的理发店,不管剪什么样的发型你只要付五块钱
老板和她的妹妹坐在椅子上对着镜子一言不发
他们的老家在身后在岸边在安徽全椒县
没有人在热河路谈恋爱,总有人在天亮时伤感
如果年轻时你没来过热河路,那你现在的生活是不是很幸福
纪念碑旁有一家破旧的电影院,往北走五百米就是南京火车西站
每天都有外地人在直线和曲线之间迷路,气喘嘘嘘眼泪模糊奔跑跌倒奔跑
秋林龙虾换了新的地方32路还是穿过挹江门
高架桥拆了修了新的隧道,走来走去走不出我的盐仓桥
来到城市已经八百九十六天,热河路一直是相同的容颜
偶尔有干净的潘西路过,她不会说你好再见
没有人在热河路谈恋爱,总有人在天黑时伤感
如果年轻时你来过热河路,那你现在是不是已经被他们淹没
没有新的衣服能让你爱恋,总有一种天气让我怀念
醒来或者吃饱又是一年,相遇然后分别就在一天

linux 网络相关命令

linux查看httpd进程数

ps -ef | grep httpd | wc -l

查看Apache的并发请求数及其TCP连接状态

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

返回结果示例:

LAST_ACK 5
SYN_RECV 30
ESTABLISHED 1597
FIN_WAIT1 51
FIN_WAIT2 504
TIME_WAIT 1057

其中的SYN_RECV表示正在等待处理的请求数;ESTABLISHED表示正常数据传输状态;TIME_WAIT表示处理完毕,等待超时结束的请求数。

来源http://blog.s135.com/post/269/

linux并发连接数查看

1、查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
netstat -n|grep  ^tcp|awk '{print $NF}'|sort -nr|uniq -c

或者:

netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}'

返回结果一般如下:

 
LAST_ACK 5 (正在等待处理的请求数)
SYN_RECV 30
ESTABLISHED 1597 (正常数据传输状态)
FIN_WAIT1 51
FIN_WAIT2 504
TIME_WAIT 1057 (处理完毕,等待超时结束的请求数)

其他参数说明:

 
CLOSED:无连接是活动的或正在进行
LISTEN:服务器在等待进入呼叫
SYN_RECV:一个连接请求已经到达,等待确认
SYN_SENT:应用已经开始,打开一个连接
ESTABLISHED:正常数据传输状态
FIN_WAIT1:应用说它已经完成
FIN_WAIT2:另一边已同意释放
ITMED_WAIT:等待所有分组死掉
CLOSING:两边同时尝试关闭
TIME_WAIT:另一边已初始化一个释放
LAST_ACK:等待所有分组死掉

2、查看Nginx运行进程数

ps -ef | grep nginx | wc -l

返回的数字就是nginx的运行进程数,如果是apache则执行

ps -ef | grep httpd | wc -l

3、查看Web服务器进程连接数:

netstat -antp | grep 80 | grep ESTABLISHED -c

4、查看MySQL进程连接数:

ps -axef | grep mysqld -c

来源http://itnihao.blog.51cto.com/1741976/830365
参考https://www.jianshu.com/p/e72ed5504b0c

[转]并发编程:Actors模型和CSP模型

一、前言
不同的编程模型与具体的语言无关,大部分现代语言都可以通过巧妙地结构处理实现不同的模型.杂谈的意思是很杂,想到哪儿写到哪儿,不对正确性负责 :D.

二、Actors模型
传统的并发模型主要由两种实现的形式,一是同一个进程下,多个线程天然的共享内存,由程序对读写做同步控制(有锁或无锁). 二是多个进程通过进程间通讯或者内存映射实现数据的同步.

Actors模型更多的使用消息机制来实现并发,目标是让开发者不再考虑线程这种东西,每个Actor最多同时只能进行一样工作,Actor内部可以有自己的变量和数据.

Actors模型避免了由操作系统进行任务调度的问题,在操作系统进程之上,多个Actor可能运行在同一个进程(或线程)中.这就节省了大量的Context切换.

在Actors模型中,每个Actor都有一个专属的命名”邮箱”, 其他Actor可以随时选择一个Actor通过邮箱收发数据,对于“邮箱”的维护,通常是使用发布订阅的机制实现的,比如我们可以定义发布者是自己,订阅者可以是某个Socket接口,另外的消息总线或者直接是目标Actor.

目前akka库是比较流行的Actors编程模型实现,支持Scala和Java语言.

三、CSP模型
CSP(Communicating Sequential Process)模型提供一种多个进程公用的“管道(channel)”, 这个channel中存放的是一个个”任务”.

目前正流行的go语言中的goroutine就是参考的CSP模型,原始的CSP中channel里的任务都是立即执行的,而go语言为其增加了一个缓存,即任务可以先暂存起来,等待执行进程准备好了再逐个按顺序执行.

四、CSP和Actor的区别
CSP进程通常是同步的(即任务被推送进Channel就立即执行,如果任务执行的线程正忙,则发送者就暂时无法推送新任务),Actor进程通常是异步的(消息传递给Actor后并不一定马上执行).
CSP中的Channel通常是匿名的, 即任务放进Channel之后你并不需要知道是哪个Channel在执行任务,而Actor是有“身份”的,你可以明确的知道哪个Actor在执行任务.
在CSP中,我们只能通过Channel在任务间传递消息, 在Actor中我们可以直接从一个Actor往另一个Actor传输数据.
CSP中消息的交互是同步的,Actor中支持异步的消息交互.

五、参考文档
Scala中的actors和Go中的goroutines对比
CSP Model From Wiki

Lucene Query Syntax

Keyboard Matching

Search for word “­foo­” in the title field
title: foo

Search for phrase “foo bar” in the title field
title: “foo bar”

Search for phrase “foo bar” in the title field AND the phrase “­quick fox” in the body field.
title­:”foo bar” AND body:”quick fox”

Search for either the phrase “foo bar” in the title field AND the phrase “­quick fox” in the body field, or the word “­fox­” in the title field.
(titl­e:”foo bar” AND body:”quick fox”) OR title:fox

Search for word “­foo­” and not “­bar­” in the title field.
title:foo -title­:bar

Wildcard matching

Search for any word that starts with “­foo­” in the title field.
title­:foo*

Search for any word that starts with “­foo­” and ends with bar in the title field.
title­:fo­o*bar

Note that Lucene doesn’t support using a symbol as the first character of a *search.

Proximity matching

Search for “foo bar” within 4 words from each other.
“foo bar”~4

Range Searches

Range Queries allow one to match documents whose field(s) values are between the lower and upper bound specified by the Range Query. Range Queries can be inclusive or exclusive of the upper and lower bounds. Sorting is done lexico­gra­phi­cally.
mod_d­ate­:[2­0020101 TO 20030101]

Boosts

Query-time boosts allow one to specify which terms/­clauses are “more import­ant­”. The higher the boost factor, the more relevant the term will be, and therefore the higher the corres­ponding document scores.

A typical boosting technique is assigning higher boosts to title matches than to body content matches:
(titl­e:foo OR title:­bar­)^1.5 (body:foo OR body:bar)

Boolean Operators

To search for all transa­ctions except MySQL transa­ctions:
NOT type: mysql

To search for all MySQL SELECT queries with large attach­ments:
mysql.me­thod: SELECT AND mysql.s­ize: [10000 TO *]

Lucene also supports parent­heses to group sub queries.
To search for either INSERT or UPDATE MySQL queries with a respon­setime greater or equal with 30ms:
(mysq­l.m­ethod: INSERT OR mysql.m­ethod: UPDATE) AND respon­set­ime:[30 TO *]