Skip to content

Instantly share code, notes, and snippets.

View adohe-zz's full-sized avatar

TonyAdo adohe-zz

View GitHub Profile
@adohe-zz
adohe-zz / LeetCodeTwo
Created June 13, 2014 04:25
Find the longest palindromic substring
package com.westudio.java;
public class LeetCodeTwo {
public static void main(String[] argv) {
}
/**
* Naive programing
* @param s
@jdx
jdx / boot.js
Last active October 1, 2025 10:57
zero-downtime node.js app runner
// This script will boot app.js with the number of workers
// specified in WORKER_COUNT.
//
// The master will respond to SIGHUP, which will trigger
// restarting all the workers and reloading the app.
var cluster = require('cluster');
var workerCount = process.env.WORKER_COUNT || 2;
// Defines what each worker needs to run
@adohe-zz
adohe-zz / Programming
Created May 28, 2014 03:17
Find the first non-repeating character within the string
package com.westudio.java;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: tonyhe
* Date: 14-5-28
* Time: 上午10:56
* To change this template use File | Settings | File Templates.
@adohe-zz
adohe-zz / event_pub_sub_01.js
Created May 14, 2014 15:12
event pub&sub in Node.js
/**
* Event_pub_sub with many to one
*
*/
var after = function (times, callback) {
var count = 0,
results = {};
return function (key, value) {
results[key] = value;
@adohe-zz
adohe-zz / AtomicFloat.java
Created May 12, 2014 15:44
add an AtomicFloat implementation
package com.ado.java.odata.util;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.Float.*;
/**
* Created with IntelliJ IDEA.
* User: nankonami
* Date: 14-5-12
* Time: 下午11:39
* To change this template use File | Settings | File Templates.
@adohe-zz
adohe-zz / web
Created May 8, 2014 06:33
Detail description of web.xml
Web.xml常用元素
<web-app>
<display-name></display-name>定义了WEB应用的名字
<description></description> 声明WEB应用的描述信息
<context-param></context-param> context-param元素声明应用范围内的初始化参数。
<filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。
<filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。
<listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
Listener元素指出事件监听程序类。
@adohe-zz
adohe-zz / singleton
Last active August 29, 2015 14:01
A singleton implementation through double-check with auto-close support(this could be useful for thread pool or connection pool etc...)
public abstract class Lazy<T> implements AutoCloseable {
private volatile T instance = null;
protected abstract T makeObject();
protected abstract void destroyObject(T obj);
public T get() {
if (instance == null) {
@adohe-zz
adohe-zz / global
Created May 4, 2014 06:04
Access to the global object in JavaScript
// This should works in ES3, ES5 and ES5-strict
var global = (function () {
return this || (1, eval)('this');
}());
@adohe-zz
adohe-zz / httpproxy01.js
Created April 19, 2014 13:26
simple http proxy implementation in 15 lines of node.js code
var http = require('http');
http.createServer(function(req, res) {
var options = {
hostname: req.headers.host,
port: 80
};
var request = http.request(options, function(response) {
res.writeHead(response.statusCode, response.headers);
response.pipe(res);
@adohe-zz
adohe-zz / cluster_child_process.txt
Created March 30, 2014 17:05
deep info about cluster&child_process
The truth of cluster&child_process module
周五的tech_talk中关于cluster和child_process这块的知识讲的不是很好,同时多少也有些
问题,所以利用周天的时间再写点关于这方面的杂文,加深下自己的了解(PS:这么好的天气实在
是应该出去赏樱花而不是在这里写东西啊,好像出去找个草坪躺在上面写也不错嗒!)
感叹到此为止,言归正传,Node在v0.8版本时新增了cluster模块,用于解决多核CPU的利用率
问题,同时也提供了完善的API,用于处理进程的健壮性问题。在v0.8版本之前,要实现多进程架
构只能通过child_process模块,考虑到有很多细节问题需要处理,可想而知这不是一件简单的事
情。好在我们有cluster,事实上cluster模块就是child_process模块和net模块的结合。cluster
启动时,它会在内部启动TCP服务器,在cluster.fork()子进程时,将这个TCP服务端socket的文件
描述符发送给工作进程,如果工作进程中存在listen()侦听网络端口的调用,它将拿到该文件描述