Skip to content

Instantly share code, notes, and snippets.

View jigewxy's full-sized avatar

Cliff Wang jigewxy

  • Singapore
View GitHub Profile
@jigewxy
jigewxy / cluster.js
Created February 9, 2018 11:40
Use cluster for master/child process communication
const cluster = require('cluster');
const app = require('express')();
const bodyParser = require('body-parser');
const cpus = 4;
var index = 0;
//next();
@jigewxy
jigewxy / MethodHiding.java
Created February 9, 2018 11:45
methodhiding on static method
package study.tonight;
class Animal {
public static void showlog(){
System.out.println("static: parent method invoked");
}
@jigewxy
jigewxy / DemoHttpRequest
Created February 11, 2018 17:28
http request sample - java
import java.net.*;
import java.io.*;
public class DemoHttpRequest{
public static void main(String[] args) {
try{
String payload="{\"jsonrpc\":\"2.0\",\"method\":\"changeDetail\",\"params\":[{\"id\":11376}],\"id\":2}";
@jigewxy
jigewxy / EchoHello.java
Created February 12, 2018 09:18
lambda function with pre-defined interface (RequestHandler <Request, Response> )
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class EchoHello implements RequestHandler <Request, Response> {
public Response handleRequest(Request request, Context context) {
String greetingString = String.format("Hello %s %s.", request.firstName, request.lastName);
return new Response(greetingString);
}
@jigewxy
jigewxy / index.js
Created February 13, 2018 16:34
node-thumbnail
const thumb = require('node-thumbnail').thumb;
thumb({
source: './img', //can be directory
destination: './pics', // can be directory
concurrency:4,
width: 50
}, (files, err, stdout, stderr)=> console.log('all done!'));
/**
@jigewxy
jigewxy / index.js
Created February 14, 2018 17:24
ASYNC method - async.forEachOf
const async = require('async');
var arr =[5,4,3,2,1];
async.forEachOf(arr, (index,i, callback)=>{
setTimeout(()=>{console.log(`Wait for ${index} seconds`); callback(null);}, index*1000);
//这里的关键在于callback必须是在循环的异步方法里面,否则是无法起作用的。
}, function(err){
@jigewxy
jigewxy / index.js
Created February 14, 2018 17:26
Async method - promise.js
const fs = require('fs');
const Promise = require('promise');
const assert = require('assert');
/*
var p1 = new Promise(function(reject, resolve){
//let result= fs.readFileSync('./index.html');
let result= fs.readFileSync('./main.html');
@jigewxy
jigewxy / ObserverDemo.java
Created February 17, 2018 03:37
Observable Class and Observer Interface
package com.company;
import java.util.Observable;
import java.util.Observer;
class ObservableObj extends Observable{
private int watched;
ObservableObj (int watched){
@jigewxy
jigewxy / DecoratorDemo
Created February 17, 2018 06:00
Decorator design pattern
package com.company;
interface Shape{
void draw();
}
class Circle implements Shape{
function sleep(ms){
return new Promise(function(resolve){
setTimeout(function(){
resolve('sleep for '+ ms + 'ms');
}, ms);
});
}