tag:blogger.com,1999:blog-29549475355128316952024-02-20T12:34:25.491-08:00Little Fish PondAndrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.comBlogger50125tag:blogger.com,1999:blog-2954947535512831695.post-81279986437354634912012-02-22T21:16:00.001-08:002012-02-22T21:16:56.434-08:00Java 7 new features - 9. Fork/Join : CDR Loading example<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/02/java-7-new-features-8-nio2-f-reviewe.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter"></a></td>
</tr>
</table>
<hr />
<br/>
In the telecommunication environment, CDR(Call Detail Record) files are widely used. It is very important to load CDR files efficiently.
Fortunately, the fork/join framework helps developers take advantage of multiple processors which are used in almost every server.
<br/>
<pre class="brush:java;">
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
public class CDRLoadingForkJoin
extends RecursiveAction
{
static final int FILE_COUNT_THRESHOLD = 1;
File[] cdrFiles = null;
public CDRLoadingForkJoin(File[] cdrFiles)
{
this.cdrFiles = cdrFiles;
}
@Override
protected void compute()
{
try
{
// Check the number of files
if (cdrFiles.length <= FILE_COUNT_THRESHOLD)
{
loadCDRFiles(cdrFiles);
}
else
{
// Split the array of CDR files into two equal parts
int center = cdrFiles.length / 2;
File[] part1 = splitArray(cdrFiles, 0, center);
File[] part2 = splitArray(cdrFiles, center, cdrFiles.length);
invokeAll(new CDRLoadingForkJoin(part1), new CDRLoadingForkJoin(part2));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected File[] splitArray(File[] array, int start, int end)
{
int length = end - start;
File[] part = new File[length];
for (int i = start; i < end; i++)
{
part[i - start] = array[i];
}
return part;
}
protected void loadCDRFiles(File[] filesToLoad)
{
for (File file: filesToLoad)
{
if (file.getName().endsWith(".cdr"))
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null)
{
//load to database ..
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
// Start the CDR file loading process with the Java SE 7 Fork/Join framework
public static void main(String[] args)
{
String cdrDirPath = "D:\\CDR\\";
File cdrDir = new File(cdrDirPath);
if(!cdrDir.isDirectory())
{
System.err.println(cdrDirPath + " is not a valid directory");
}
File[] files = cdrDir.listFiles();
CDRLoadingForkJoin process = new CDRLoadingForkJoin(files);
int processors = Runtime.getRuntime().availableProcessors();
ForkJoinPool pool = new ForkJoinPool(processors);
pool.invoke(process);
}
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-82210708947724087652012-02-07T16:16:00.001-08:002012-02-22T21:22:57.963-08:00Java 7 new features - 8. NIO2 : F. Review - Traditional TCP server/client example<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/02/java-7-new-features-7-nio2-e.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/02/java-7-new-features-9-forkjoin-cdr.html">Next >></a></td>
</tr>
</table>
<hr />
<br/>
After we learnt how the NIO2 works, let's review the traditional socket programming
<br />
<b>Traditional TCP Server</b>
<pre class="brush:java;">
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class TraditionalTcpSocketServer
{
public static void main(String[] argv)
throws Exception
{
String clientSentence;
ServerSocket welcomeSocket = new ServerSocket(9001);
while (true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("FROM CLIENT: " + clientSentence);
outToClient.writeBytes("World" + System.getProperty("line.separator"));
}
}
}
</pre>
<b>Traditional TCP Client</b>
<pre class="brush:java;">
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class TraditionalTcpSocketClient
{
public static void main(String[] argv)
throws Exception
{
String sentence = "Hello";
String modifiedSentence;
Socket clientSocket = new Socket("localhost", 9001);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(sentence + System.getProperty("line.separator"));
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-12438476614718362322012-02-06T21:22:00.000-08:002012-02-22T21:11:07.954-08:00Java 7 new features - 7. NIO2 : E. Asynchronous TCP server/client example<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-6-nio2-d-non.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/02/java-7-new-features-8-nio2-f-reviewe.html">Next>></a></td>
</tr>
</table>
<hr />
<br/>
To illustrate how Asynchronous works, there are some delay in both the server and the client. You can easily play this on your PC to understand how the Asynchronous channels work.
<br />
<b>Asynchronous TCP Server</b>
<pre class="brush:java;">
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class AsynchronousTcpServer
{
public static void main(String[] args)
{
final int SERVER_PORT = 9001;
final String SERVER_IP = "127.0.0.1";
//create asynchronous server-socket channel bound to the default group
try (AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open())
{
if (asynchronousServerSocketChannel.isOpen())
{
//bind to local address
asynchronousServerSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT));
//display a waiting message
System.out.println("Waiting for connections ...");
while (true)
{
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture =
asynchronousServerSocketChannel.accept();
try (AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get())
{
System.out.println("Incoming connection from: " + asynchronousSocketChannel.getRemoteAddress());
ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);
//receiving data
asynchronousSocketChannel.read(incomingBuffer, incomingBuffer, new CompletionHandler<Integer, ByteBuffer>()
{
public void completed(Integer result, ByteBuffer buffer)
{
buffer.flip();
String msgReceived = Charset.defaultCharset().decode(buffer).toString();
System.out.println("Msg received from the client : " + msgReceived);
}
public void failed(Throwable exc, ByteBuffer buffer)
{
throw new UnsupportedOperationException("read failed!");
}
});
try
{
Thread.sleep(5000);
}
catch(Exception e){}
//replying data
ByteBuffer outgoingBuffer = ByteBuffer.wrap("World".getBytes());
asynchronousSocketChannel.write(outgoingBuffer).get();
}
catch (IOException | InterruptedException | ExecutionException ex)
{
System.err.println(ex);
}
}
}
else
{
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
</pre>
<b>Asynchronous TCP Client</b>
<pre class="brush:java;">
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;
public class AsynchronousTcpClient
{
static boolean completed = false;
public static void main(String[] args)
{
final int SERVER_PORT = 9001;
final String SERVER_IP = "127.0.0.1";
ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(1024);
ByteBuffer sendingBuffer = ByteBuffer.wrap("Hello".getBytes());
//create asynchronous socket channel
try (final AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open())
{
if (asynchronousSocketChannel.isOpen())
{
//connect this channel's socket
Void connect = asynchronousSocketChannel.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT)).get();
if (connect == null)
{
System.out.println("Local address: " + asynchronousSocketChannel.getLocalAddress());
//sending data
asynchronousSocketChannel.write(sendingBuffer).get();
asynchronousSocketChannel.read(receivingBuffer, receivingBuffer, new CompletionHandler<Integer, ByteBuffer>()
{
public void completed(Integer result, ByteBuffer buffer)
{
buffer.flip();
String msgReceived = Charset.defaultCharset().decode(buffer).toString();
System.out.println("Msg received from server : " + msgReceived);
completed = true;
}
public void failed(Throwable exc, ByteBuffer buffer)
{
completed = false;
throw new UnsupportedOperationException("read failed!");
}
});
while(!completed)
{
try
{
Thread.sleep(1000);
}
catch(Exception e){}
System.out.println("Waiting for response from the server");
}
}
else
{
System.out.println("The connection cannot be established!");
}
}
else
{
System.out.println("The asynchronous socket channel cannot be opened!");
}
}
catch (IOException | InterruptedException | ExecutionException ex)
{
System.err.println(ex);
}
}
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com4tag:blogger.com,1999:blog-2954947535512831695.post-25822589920358442622012-01-30T17:00:00.000-08:002012-02-07T16:21:24.157-08:00Java 7 new features - 6. NIO2 : D. Non Blocking TCP server/client example<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-5-nio2-c-blocking.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/02/java-7-new-features-7-nio2-e.html">Next>></a></td>
</tr>
</table>
<hr />
<br/>
To illustrate how non-blocking works, the server will sleep for 1 second. You can understand this better by runing thest two classes and check the log messages
<br />
<b>Non Blocking TCP Server</b>
<pre class="brush:java;">
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;
public class NonBlockingTcpServer
{
public static void main(String[] args) throws Exception
{
final int DEFAULT_PORT = 9001;
ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer outgoingBuffer = ByteBuffer.wrap("World".getBytes());
//Open Selector and ServerSocketChannel
try (Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open())
{
//Check if both of them were opened
if ((serverSocketChannel.isOpen()) && (selector.isOpen()))
{
//Configure non-blocking mode
serverSocketChannel.configureBlocking(false);
//Bind to the specific port number
serverSocketChannel.bind(new InetSocketAddress(DEFAULT_PORT));
//Register the current channel with the given selector
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Waiting for incoming connections ...");
while (true)
{
//wait for incomming events
selector.select();
//there is something to process on selected keys
Iterator keys = selector.selectedKeys().iterator();
while (keys.hasNext())
{
SelectionKey key = (SelectionKey) keys.next();
//prevent the same key from coming up again
keys.remove();
if (!key.isValid())
{
continue;
}
//Accept incoming connection
if(key.isAcceptable())
{
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverChannel.accept();
socketChannel.configureBlocking(false);
System.out.println("Accept incomming connection from: " + socketChannel.getRemoteAddress());
socketChannel.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable())
{
SocketChannel socketChannel = (SocketChannel) key.channel();
incomingBuffer.clear();
int numRead = -1;
try
{
numRead = socketChannel.read(incomingBuffer);
}
catch (Exception e)
{
e.printStackTrace();
key.cancel();
continue;
}
incomingBuffer.flip();
String requestMsg = decoder.decode(incomingBuffer).toString();
System.out.println("Request from " + socketChannel.getRemoteAddress() + " : " + requestMsg);
Thread.sleep(1000);
socketChannel.write(outgoingBuffer);
outgoingBuffer.flip();
socketChannel.shutdownOutput();
key.cancel();
}
}
}
}
else
{
System.out.println("The server socket channel or selector cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
</pre>
<b>Non Blocking TCP Client</b>
<pre class="brush:java;">
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;
import java.util.Set;
public class NonBlockingTcpClient
{
public static void main(String[] args)
{
final int DEFAULT_PORT = 9001;
final String IP = "127.0.0.1";
ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(1024);
ByteBuffer sendingBuffer = ByteBuffer.wrap("Hello".getBytes());
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
String responseMsg = "";
//open Selector and ServerSocketChannel
try (Selector selector = Selector.open();
SocketChannel socketChannel = SocketChannel.open())
{
//check that both of them were opened
if ((socketChannel.isOpen()) && (selector.isOpen()))
{
//configure non-blocking mode
socketChannel.configureBlocking(false);
//register the current channel with the given selector
socketChannel.register(selector, SelectionKey.OP_CONNECT);
socketChannel.connect(new java.net.InetSocketAddress(IP, DEFAULT_PORT));
//waiting for the connection
while(selector.select(1000) > 0)
{
//get keys
Set keys = selector.selectedKeys();
Iterator its = keys.iterator();
//process each key
while (its.hasNext())
{
SelectionKey key = (SelectionKey) its.next();
//remove the current key
its.remove();
//get the socket channel for this key
try (SocketChannel keySocketChannel = (SocketChannel) key.channel())
{
//attempt a connection
if (key.isConnectable())
{
//make sure the connection estqablishment has been finished
if (keySocketChannel.isConnectionPending())
{
keySocketChannel.finishConnect();
}
keySocketChannel.write(sendingBuffer);
keySocketChannel.shutdownOutput();
long startTime = System.currentTimeMillis();
while (keySocketChannel.read(receivingBuffer) != -1)
{
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("elapsedTime=" + elapsedTime);
receivingBuffer.flip();
String msgReceived = decoder.decode(receivingBuffer).toString();
System.out.println("Msg received in this loop : " + msgReceived);
responseMsg = responseMsg + msgReceived;
if (receivingBuffer.hasRemaining())
{
receivingBuffer.compact();
}
else
{
receivingBuffer.clear();
}
}
System.out.println("Response from server : " + responseMsg);
}
else
{
System.out.println("The connection cannot be established!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
}
else
{
System.out.println("The socket channel or selector cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com1tag:blogger.com,1999:blog-2954947535512831695.post-6633433568450300862012-01-16T19:19:00.001-08:002012-02-06T21:28:11.376-08:00Java 7 new features - 5. NIO2 : C. Blocking TCP server/client example<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-3-nio2-b-watch.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/java-7-new-features-6-nio2-d-non.html">Next>></a></td>
</tr>
</table>
<hr />
<br/>
Java 7 introduces a new interface - NetworkChannel that provides common methods to all network channel classes and a new SocketOption<T> interface and StandardSocketOptions class.
Please check the following exmaple. To demonstrate how ByteBuffer works, I just set buffer size as 2.
<br/>
<br/>
<b>Blocking TCP Server</b>
<pre class="brush:java;">
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class BlockingTcpServer
{
public static void main(String[] args)
{
final int SERVER_PORT = 9001;
final String SERVER_IP = "127.0.0.1";
ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(2);
ByteBuffer outgoingBuffer = ByteBuffer.wrap("World".getBytes());
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
String requestMsg = "";
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open())
{
if (serverSocketChannel.isOpen())
{
serverSocketChannel.configureBlocking(true);
//set options
serverSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024);
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
//bind the server socket channel to local address
serverSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT));
while (true)
{
try (SocketChannel socketChannel = serverSocketChannel.accept())
{
while (socketChannel.read(incomingBuffer) != -1)
{
incomingBuffer.flip();
String msgReceived = decoder.decode(incomingBuffer).toString();
System.out.println("Msg received in this loop : " + msgReceived);
requestMsg = requestMsg + msgReceived;
if (incomingBuffer.hasRemaining())
{
incomingBuffer.compact();
}
else
{
incomingBuffer.clear();
}
}
System.out.println("Request from " + socketChannel.getRemoteAddress()
+ " : " + requestMsg);
socketChannel.write(outgoingBuffer);
outgoingBuffer.flip();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
else
{
System.out.println("The server socket channel cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
</pre>
<b>Blocking TCP Client</b>
<pre class="brush:java;">
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class BlockingTcpClient
{
public static void main(String[] args)
throws IOException
{
final int SERVER_PORT = 9001;
final String SERVER_IP = "127.0.0.1";
ByteBuffer receivingBuffer = ByteBuffer.allocateDirect(2);
ByteBuffer sendingBuffer = ByteBuffer.wrap("Hello".getBytes());
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
String responseMsg = "";
//create a new socket channel
try (SocketChannel socketChannel = SocketChannel.open())
{
if (socketChannel.isOpen())
{
//set the blocking mode
socketChannel.configureBlocking(true);
socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 1024);
socketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024);
socketChannel.setOption(StandardSocketOptions.SO_LINGER, 10);
//establish channel connection
socketChannel.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT));
if (socketChannel.isConnected())
{
//sending data
socketChannel.write(sendingBuffer);
socketChannel.shutdownOutput();
//receving data
while (socketChannel.read(receivingBuffer) != -1)
{
receivingBuffer.flip();
String msgReceived = decoder.decode(receivingBuffer).toString();
System.out.println("Msg received in this loop : " + msgReceived);
responseMsg = responseMsg + msgReceived;
if (receivingBuffer.hasRemaining())
{
receivingBuffer.compact();
}
else
{
receivingBuffer.clear();
}
}
System.out.println("Response from server : "+ responseMsg);
}
else
{
System.out.println("The connection cannot be established!");
}
}
else
{
System.out.println("The socket channel cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-7579991525996329802012-01-11T16:41:00.001-08:002012-01-30T17:03:50.512-08:00Java 7 new features - 4. NIO2 : B. Watch Service<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-3-nio2-copy-files.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a href="/2012/01/java-7-new-features-5-nio2-c-blocking.html" class="chapter">Next >></a></td>
</tr>
</table>
<hr />
<br/>
In some high volume traffic system (e.g. telecommunication environment), some resource files (for example configuration files) needed to be cached in the memory.
When the files are changed, the system needs to be aware and reload them into the memory. Another case is that when we need to reduce the system down time
as little as possible, we also need to have a system which can support hot deployment and dynamic reloading because customers may not be able to afford to do
stop and restart frequently. In both cases, we need to have a mechanism to detect whether the original resource has been changed or not.
In previous Java version(<7), one of methods we used is checking the last modified timestamps of those files. The following code snippet can be seen everywhere:
<br />
<pre class="brush:java;">
...
private long lastModifiedTime = 0;
...
long fileModified = file.lastModified();
if (fileModified > lastModifiedTime)
{
//reload the resource files
reload();
lastModifiedTime = fileModified;
}
...
</pre>
From Java 7, we can easily use the Watch Service API which uses the underlying file system functionalities to watch the file system for changes (Create, Modify, Overflow, Delete).
To meet the requirement mentioned above, we can write some code like below:
<br />
<pre class="brush:java;">
...
private final static String DITECTORY_TO_WATCH = "some_directory";
private final static String FILE_TO_WATCH = "some_file";
...
FileSystem fs = FileSystems.getDefault();
WatchService ws = fs.newWatchService();
Path path = fs.getPath(DITECTORY_TO_WATCH);
path.register(ws, StandardWatchEventKinds.ENTRY_MODIFY);
while(true)
{
WatchKey key = ws.take();
List<Watchevent<?>> events = key.pollEvents();
for (WatchEvent event: events)
{
if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY)
{
if(event.context().toString().equals(FILE_TO_WATCH))
{
//reload the resource files
reload();
}
}
}
key.reset();
}
...
</pre>
This will be much more efficient than the previous solution.
Howerver, you need to pay attention of the following points:<br/>
<ol>
<li>please don't forget put the following line (which can be ignored by some developer) in your infinite loop:<br/>
key.reset()<br/>
Without this, the system is not able to detect the future change.
</li>
<li>Inside the "WatchService", "ReentrantLock" is used (inside the LinkedBlockingDeque class).<br/>
So the Watch Service needs to be in a separate Thread from your other paralleling running tasks.
</li>
<li>Instead of using ws.take(), you can also use ws.poll. Their differencs are described below:<br/>
<ul>
<li>pull(): Retrieves and removes the next watch key, or if no key is availabe, a null will be returned immediately.</li>
<li>poll(long timeout, TimeUnit unit): Retrieves and removes the next watch key, waiting up to the specified wait time if still no key available, then it will return null.</li>
<li>take(): Retrieves and removes next watch key, keeps waiting until there is a key available.</li>
</ul>
</li>
</ol>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-69031467657229216482012-01-09T17:01:00.000-08:002012-01-17T20:13:52.803-08:00Java 7 new features - 3. NIO2 : A. Copy files with Path API<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-2-automatic.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a href="/2012/01/java-7-new-features-3-nio2-b-watch.html" class="chapter">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
Java 7 has a new generation of NIO which is NIO2.
There are some significant enhancements in NIO2. One of them is the Path API.
<pre class='brush:java;'>
/**
* Feel free to copy and use this method
* If you have any comments or better solutions, please paste on my blog:
* http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
*
* When we copy files, there are several options for us now. I think copying with NIO2
* is the easiest one now.(Even though "copyFileWithOSCommand" is the fast one, but it may
* not be allowed to use sometimes).
*/
</pre>
Copy files by using Windows/Liunx/Unix system command
<pre class='brush:java;'>
public static void copyFileWithOSCommand(String sourceFilename, String targetFilename)
throws IOException
{
String cmd = "cmd /C copy ";
String os = System.getProperty("os.name");
os = os.toLowerCase();
if(os.indexOf("windows")>=0)
{
os = "cp ";
}
cmd = cmd + sourceFilename + " " + targetFilename;
Runtime.getRuntime().exec(cmd);
}
</pre>
Copy files by using traditional Stream
<pre class='brush:java;'>
public static void copyFileWithStream(String sourceFilename, String targetFilename)
throws IOException
{
InputStream in = null;
OutputStream out = null;
try
{
in = new FileInputStream(sourceFilename);
out = new FileOutputStream(targetFilename);
byte[] buf = new byte[4098];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
</pre>
Copy files by using the first generation NIO transfer
<pre class='brush:java;'>
public static void copyFileWithNIO(String sourceFilename, String targetFilename)
throws IOException
{
FileChannel source = null;
FileChannel target = null;
try
{
source = new FileInputStream(sourceFilename).getChannel();
target = new FileOutputStream(targetFilename).getChannel();
target.transferFrom(source, 0, source.size());
}
finally
{
if (source != null)
{
source.close();
}
if (target != null)
{
target.close();
}
}
}
</pre>
Copy files by using NIO2 which is provided by Java 7
<pre class='brush:java;'>
public static void copyFileWithNIO2(String sourceFilename, String targetFilename)
throws IOException
{
Path source = Paths.get(sourceFilename);
Path target = Paths.get(targetFilename);
Path copy = Files.copy(source, target);
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-62999418325606581402012-01-08T22:24:00.000-08:002012-01-17T20:18:33.556-08:00Java 7 new features - 2. Automatic Resource Management<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/java-7-new-features-1-switch-on-string.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a href="/2012/01/java-7-new-features-3-nio2-copy-files.html" class="chapter">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
With this feature, resources such as Connections, Files, Input/OutStreams,
etc. will be closed automatically in the "try" block rather than done
manually by the developer.
I personally think this is one of the most important features.
Prior to Java 7, developers have to remember to close resource
when programming in JDBC, Socket, File IO and etc.
They need to do something like:
<pre class='brush:java;'>
try
{
...
}
catch (SQLException e)
{
// handle exception
}
finally
{
...
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
}
conn = null;
}
...
}
</pre>
This is not only annoying, but also error-prone. Especially for JDBC, if there are too
many pending connections, the database will stop working in the end. In Java 7, you can
use and test the automatic resource management like following:
<pre class='brush:java;'>
...
Connection conn = null;
try(Connection connection=DriverManager.getConnection(url, "username", "password"))
{
conn=connection;
...
}
//the following code is just for testing
try
{
System.out.println("connection isClosed? "+conn.isClosed());
}
catch(Exception e){}
</pre>
Unfortunately however not all Java IDEs have a good support for this feature, some of them
will complain "not a valid expression statement".
So when you want to use this feature, please make sure that you have the latest IDE.
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com1tag:blogger.com,1999:blog-2954947535512831695.post-26410263090087891162012-01-08T18:46:00.001-08:002012-01-17T20:18:53.128-08:00Java 7 new features - 1. Switch on Strings<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/find-starting-index-of-sub-array-from.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a href="/2012/01/java-7-new-features-2-automatic.html" class="chapter">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<pre class='brush:java;'>
/**
* If you have any comments or better examples, please paste on my blog:
* http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
*
* From Java 7, developers can use "switch" statement on String.
* Please refer to the following example about how to use it.
*/
public static String getAustralianCapitalCity(String stateOrTerritory)
{
String capitalCity = "unknown";
switch (stateOrTerritory)
{
case "NSW":
case "New South Wales":
capitalCity = "Sydney";
break;
case "QLD":
case "Queensland":
capitalCity = "Brisbane";
break;
case "VIC":
case "Victoria":
capitalCity = "Melbourne";
break;
case "SA":
case "South Australia":
capitalCity = "Adelaide";
break;
case "TAS":
case "Tasmania":
capitalCity = "Hobart";
break;
case "NT":
case "Northern Territory":
capitalCity = "Darwin";
break;
case "ACT":
case "Australian Capital Territory":
capitalCity = "Canberra";
break;
default:
break;
}
return capitalCity;
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com1tag:blogger.com,1999:blog-2954947535512831695.post-75079533151144092042012-01-07T22:43:00.000-08:002012-01-17T20:20:22.771-08:00Find the starting index of a sub-array from the first array<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/string-concatenation-in-java.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a href="/2012/01/java-7-new-features-1-switch-on-string.html" class="chapter">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<pre class='brush:java;'>
/**
* Please note: even though I just used Integer Array in this task, the algorithm could be
* used for any Array
*/
public static int firstIndexOfSubArray(int[] arr1, int[] arr2)
{
if (arr1 == null || arr2 == null)
{
return -1;
}
int len1 = arr1.length;
int len2 = arr2.length;
if (len1 == 0 || len2 == 0 || len1 < len2)
{
return -1;
}
//start the sliding window from the left side
int slidingFrom = 0;
int i = slidingFrom;
int j = 0;
int firstEqualIndex = -1;
while (true)
{
//if the sliding window is out of boundary,just return
if (len2 > (len1 - slidingFrom))
{
return -1;
}
//find the first equal element
if (firstEqualIndex == -1)
{
if (arr1[i] == arr2[0])
{
firstEqualIndex = i;
slidingFrom = i;
j = 1;
}
i++;
continue;
}
//if already found the first equal element, compare the following elements
if (arr1[i] == arr2[j])
{
//if equal, contiue to compare until the last element in array 2
i++;
j++;
if (j == len2)
{
break;
}
}
//if not equal, slide to the next one and compare every element in array 2 again
else
{
firstEqualIndex = -1;
i = slidingFrom + 1;
}
}
return firstEqualIndex;
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-75742332047270900172012-01-06T23:02:00.001-08:002012-01-17T21:00:14.489-08:00Ireland<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/women-in-china-today.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/goodness-of-life.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCMeKvS-hQyoiz1_n5f4ZMFUT6Fz_H7IrLLkKd0R7Z9pXnn6n6aPrUW9fxyRid_iXWbGUQQ6oJEhHlCNJ-TUd4Apd4yQRJVnNOCSi7yfpQkW8J0wEFDYMX1SDllBnFTxpzoYK0IOx73I4/s1600/ireland1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCMeKvS-hQyoiz1_n5f4ZMFUT6Fz_H7IrLLkKd0R7Z9pXnn6n6aPrUW9fxyRid_iXWbGUQQ6oJEhHlCNJ-TUd4Apd4yQRJVnNOCSi7yfpQkW8J0wEFDYMX1SDllBnFTxpzoYK0IOx73I4/s320/ireland1.jpg" width="320" /></a></div>
I knew there is a city called Dublin from one of my text books when I was a middle school student. In 1990s, news about North Ireland had often been in the headlines of newspapers because of the conflict between the nationalist community and the unionist community, especially about its Sinn Fein party. From then on Ireland has become a mysterious country in my mind. That has driven me to learn about Ireland and Irish people.
<br />
<br />
As the third largest island in Europe, the population of Ireland is just approximately 6.2 million people. Of them 4.5 million live in the Republic of Ireland and 1.8 million live in Northern Ireland. But the most interesting thing is that the largest number of Irish people live in the United States—about ten times more than in Ireland itself.
<br />
<br />
For a comparatively small island, Ireland has made a disproportionately large contribution to English literature and world literature. So far four people have won the Nobel literature prize. One of the famous ones is George Bernard Shaw. He wrote more than 60 dramas. He is the only person to have been awarded both a Nobel Prize for Literature (1925) and an Oscar (1938), for his contributions to literature and for his work on film respectively. I was inspired by what Shaw said, “Imagination is the beginning of creation. You imagine what you desire, you will what you imagine and at last you create what you will.
<br />
<br />
Ireland is a sacred place in which great writings and writers are born. It is a place which can spark your inspiration about literature. On one rainy day, when you are walking with an umbrella in your hand in one of small lanes in Dublin, in the sound of church’s bell you can feel the rich literature deposits created by Irish great masters.
<br />
<br />
Let’s end my writing with one of Yeats’ poems.
<br />
<br />
<div style="text-align: center;">
<i>WHEN YOU ARE OLD
</i></div>
<div style="text-align: center;">
<i><br /></i></div>
<div style="text-align: center;">
<i>When you are old and grey and full of sleep,</i></div>
<div style="text-align: center;">
<i>And nodding by the fire, take down this book,</i></div>
<div style="text-align: center;">
<i>And slowly read, and dream of the soft look</i></div>
<div style="text-align: center;">
<i>Your eyes had once, and of their shadows deep;</i></div>
<div style="text-align: center;">
<i><br /></i></div>
<div style="text-align: center;">
<i>How many loved your moments of glad grace,</i></div>
<div style="text-align: center;">
<i>And loved your beauty with love false or true,</i></div>
<div style="text-align: center;">
<i>But one man loved the pilgrim soul in you,</i></div>
<div style="text-align: center;">
<i>And loved the sorrows of your changing face;</i></div>
<div style="text-align: center;">
<i><br /></i></div>
<div style="text-align: center;">
<i>And bending down beside the glowing bars,</i></div>
<div style="text-align: center;">
<i>Murmur, a little sadly, how Love fled</i></div>
<div style="text-align: center;">
<i>And paced upon the mountains overhead</i></div>
<div style="text-align: center;">
<i>And hid his face amid a crowd of stars.</i></div>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-59270887229894285222012-01-05T17:06:00.001-08:002012-01-17T20:22:00.516-08:00String Concatenation in Java<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/parse-string-to-properties.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/find-starting-index-of-sub-array-from.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<pre class='brush:java;'>
/**
* Feel free to copy and use this method
* If you have any comments or better solutions, please paste on my blog:
* http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
*
* Using "+" operator to cocat multiple Strings is convenient
* but it is not efficient because String is immutable in Java.
* We all know that we can use StringBuffer or StringBuilder to do this.
* StringBuffer is synchronized and StringBuilder is not.
*
* But creaging a StringBuffer or StringBuilder object every time
* is boring when we need to concat strings.
* So it's more convenient to just call a function with variable-length argument
* which is available from Java 5
*
* A piece of testing source code is listed as the following:
*
* String str1 = "StringA";
* String str2 = "StringB";
* String str3 = "StringC";
* String result = concatStringByStringBuffer(str1, str2, str3);
* or
* String result = concatStringByStringBuilder(str1, str2, str3);
*/
public static String concatStringByStringBuffer(String... strs)
{
if(strs==null)
{
return null;
}
StringBuffer stringBuffer = new StringBuffer();
for (String s : strs)
{
stringBuffer.append(s);
}
return stringBuffer.toString();
}
public static String concatStringByStringBuilder(String... strs)
{
if(strs==null)
{
return null;
}
StringBuilder stringBuilder = new StringBuilder();
for (String s : strs)
{
stringBuilder.append(s);
}
return stringBuilder.toString();
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-18502675487987158972012-01-04T20:16:00.001-08:002012-01-17T20:40:48.938-08:00Get a Random Integer Number (Oracle)<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/get-next-token-in-string-oracle.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/display-all-key-values-in-properties.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
-- Feel free to copy and use this method<br />
-- If you have any comments or better solutions, please paste on my blog:<br />
-- http://blog.yanmingyu.com or email me: feedback.andrewyu@gmail.com <br />
<br />
-- The following function will return a random integer which is<br />
-- between the P_LOW(>=) and P_HIGH (<)<br />
<br />
create or replace<br />
FUNCTION NEXT_RAND_INT(P_LOW IN NUMBER, P_HIGH IN NUMBER)<br />
RETURN NUMBER IS<br />
V_NUM NUMBER(11);<br />
BEGIN<br />
SELECT floor(dbms_random.value(P_LOW, p_HIGH)) INTO V_NUM FROM DUAL;<br />
RETURN V_NUM;<br />
END NEXT_RAND_INT;<br />Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-46751305569934658532012-01-04T09:38:00.000-08:002012-01-17T21:14:28.340-08:00A Father's Song<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/london-fireworks-2012-new-years-eve_03.htm"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/crises-in-world.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: large;">As a father, I am moved deeply by this song, even though it was first released almost 10 years ago (2003). </span>
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<embed autostart="true" flashvars="audioUrl=https://sites.google.com/site/yanmingyublog/docs/ToughLittleBoys.mp3" height="27" loop="true" quality="best" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" type="application/x-shockwave-flash" width="400"></embed>
</div>
<h1 id="article_title" style="text-align: center;">
<span style="font-size: large;">Tough Little Boys Lyrics</span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-size: large;">Artist<span style="font-weight: normal;">:Gary Allen</span></span><br style="font-weight: normal;" /><span style="font-weight: normal;">
</span><br style="font-weight: normal;" /><span style="font-weight: normal;">
<span style="font-size: large;">Well I never once backed down from a punch<br />Well I take it square on the chin<br />Well I found out fast that bullies just that<br />And you've got to stand up him</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />So I didn't cry when I got a black eye<br />As bad as it hurt I'd just grinned<br />But when tough little boys grow up to be dads<br />They turn into big babies again</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />Scared me to death when you took your first steps<br />Well I'd fall every time you fell down<br />Your first day to school I cried like a fool<br />And I followed your school bus to town</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />Well I didn't cry when Old Yeller died<br />At least not in front of my friends<br />But when tough little boys grow up to be dads<br />They turn into big babies again</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />Well I'm a grown man but as strong as I am<br />Well sometimes its hard to believe<br />That one little girl with little blond curls<br />Can totally terrify me</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />If you were to ask my wife would just laugh<br />Shed say I know all about men<br />Now tough little boys grow up to be dads<br />They turn into big babies again</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />Well I know one day I'll give you away<br />And I'm gonna stand there and smile<br />But when I get home and I'm all alone<br />And Ill sit in your room for a while</span></span></h1>
<h1 id="article_title" style="text-align: center;">
<span style="font-weight: normal;"><span style="font-size: large;"><br />Well I didn't cry when Old Yeller died<br />At least not in front of my friends<br />But when tough little boys grow up to be dads<br />They turn into big babies again<br />When tough little boys grow up to be dads<br />They turn into big babies again </span></span></h1>
<h1 id="article_title" style="text-align: center;">
<b id="internal-source-marker_0.2438354140613228"><span style="background-color: white; font-family: Arial; font-size: 13px; font-weight: normal; vertical-align: baseline; white-space: pre-wrap;">Reference: <a href="http://www.songlyrics.com/gary-allan/tough-little-boys-lyrics/">http://www.songlyrics.com/gary-allan/tough-little-boys-lyrics/</a></span></b></h1>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-14824798022175287052012-01-04T08:53:00.001-08:002012-01-17T21:00:30.872-08:00The Rotten Apple Theory<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/excuses-in-software-development.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/subtract-dates-in-batch-file.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
One rotten apple will spread its mold or decay to its neighbours. This is known as “One bad apple spoils the whole bunch”. A person can be called a rotten apple for being a bad individual among a good group. Some research has show that the efficiency of a team is dependent on its laziest member, rather than the best one or average member. It reminds me of another famous theory called “short plate theory” which says that the amount of water that a wooden barrel can contain is not determined by the longest piece of wood, but it depends on the shortest piece of wood.
<br>
<br>
In general speaking, I agree with these theories. If one of the team members is lazier than the others, gradually other members will feel that is unfair if they always work harder than him/her. So other members will slow down their pace inwardly until in the end the whole team has a similar speed to the laziest person.
<br>
<br>
In the information industry, a bad apple will not only encumber the process of a team, but also damage other people’s work. This is because he or she may introduce more issues which need other people to fix them. That will bring extra work for other team members.
<br>
<br>
We can easily throw away a rotten apple to prevent it from spoiling a bunch. However we cannot easily remove a bad person in a team. Such people often spend more time to curry favour with bosses even though they are not good at work.
<br>
<br>
How much difference one apple can make is really dependent on what role he or she plays. If he or she is just a low level member in a team, other people may still be able to compensate for that member. If he/she is a manager, that person will definitely corrupt a group.
<br>
<br>
There is another side of the bad apple theory. Once a person is marked as a “bad apple” by people, he or she will be often accused of something wrong which is not even his/her fault at all.Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-87932176030585857032012-01-03T16:59:00.001-08:002012-01-17T21:15:16.325-08:00London Fireworks 2012 New Years Eve<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/math-in-australian-lottery.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/fathers-song.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='600' height='480' src='https://www.youtube.com/embed/J--WBdwnyvI?feature=player_embedded' frameborder='0'></iframe></div>
<br />Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com1tag:blogger.com,1999:blog-2954947535512831695.post-72194486778441711282012-01-03T13:55:00.001-08:002012-01-17T21:01:55.188-08:00Women in China today<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/idiom-value.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/ireland.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<br />
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 12pt; line-height: 115%;">Even though in rural areas in China,
sex discrimination still exists-especially with the one child policy, the
status of women in society has been promoted dramatically. Some people even
think that women actually have higher status than men by taking advantage of the
so-called “</span><span class="hps"><span lang="EN" style="font-size: 12pt; line-height: 115%;">vulnerable group”. For example, in
contrast to other countries, a married woman in China still keeps her own
surname instead of using her husband’s one.</span></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 12pt; line-height: 115%;">Traditionally</span><span lang="EN" style="font-size: 12pt; line-height: 115%;"> the</span><span style="font-size: 12pt; line-height: 115%;"> family has been
the most important unit of Chinese society, and <span class="slicetext">a woman
was considered subordinate to her
husband</span>, because normally men took more responsibility for the
budget in the family. But in modern society, both the husband and the wife will
work. So with the financial independence, women have more discourse right in
their families.</span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 12pt; line-height: 115%;">Traditionally due to the lack of
pension system, when parents got old, it was the son who would raise them.
However, nowadays it is interesting that husbands often spend more time with
their parents-in-law than his own parents in many urban families. So the preference
to have a boy has been changing quietly.</span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 12pt; line-height: 115%;">Traditionally a woman had to be loyal
to her husband throughout her whole life even though she was abandoned by her
husband. Remarriages were often scoffed at by the community. With rapid
economic growth and western influence, this concept has been subverting. The
divorce rate has surged remarkably in recent years. At the same time, women in
China have been deeply influenced by the internet. With the help of chatting
online tools, they can easily pursue their own romantics in the virtual world. </span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 12pt; line-height: 115%;">Traditionally women had the thrifty
virtue. But there has been an undesirable trend in recent years towards the
worship of money. Wealth has become their only standard scale to measure all
things. They are unwilling to live in poverty and are chasing the life of
luxury. It is estimated that <span class="chead">China will account for</span>
about 20 precent, or $27 billion, of the global luxury market.</span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="font-size: 12pt; line-height: 115%;"><span class="hps"></span></span></div>
<div class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span class="hps"><span lang="EN" style="font-size: 12pt; line-height: 115%;">Almost everything has been changing in China. So have Chinese women. They have
the right to enjoy the freedom and opportunity to master their own fate as long
as they won’t lose themselves on their journeys.</span></span></div>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-41300965910396704252012-01-02T20:43:00.000-08:002012-01-17T20:54:43.647-08:00Youth<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/happy-door.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/galaxy-nexus-vs-iphone-4s.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<br />
<div class="MsoNormal">
Youth is not a time of life; it is a state of mind; it is
not a matter of rosy cheeks, red lips and supple knees; it is a matter of the
will, a quality of the imagination, a vigor of the emotions; it is the
freshness of the deep springs of life.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Youth means a temperamental predominance of courage over
timidity of the appetite for adventure over the love of ease. This often exists
in a man of 60 more than a boy of 20. Nobody grows old merely by number of
years. We grow old by deserting our ideals.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Years may wrinkle the skin, but to give up enthusiasm
wrinkles the soul. Worry, fear, self-distrust bows the heart and turns the
spirit back to dust.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Whether 60 or 16, there is in every human being’s heart the
lure of wonders, the unfailing appetite for what’s next and the joy if the game
of living. In the centre of your heart and my heart, there is a wireless
station; so long as it receives messages of beauty, hope, courage and power
from man and from the infinite, so long as you are young.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
When your aerial are down, and your spirit is covered with snows
of cynicism and the ice of pessimism, then you’ve grown old, even at 20; but as
long as your aerials are up, to catch waves of optimism, there’s hope you may
die young at 80.<o:p></o:p></div>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-90981694293762053612012-01-02T19:10:00.001-08:002012-01-17T21:16:05.124-08:00"Secrete" Math in Australian Lottery<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2012/01/power-of-apple.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/london-fireworks-2012-new-years-eve_03.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4F60Ge6S39YQLG6rg5joqqb7Rd_iGgVOXF1ENK6dcBZScrVfzl-3-R-JO9Glg64u23IyQ3Zof4brk_Af5zyUaKMVxOlIa68qMc9sgDl1f_MrZMJnIc2GUha4au1D4NhMO7vH_ojZdwZw/s1600/lotto.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4F60Ge6S39YQLG6rg5joqqb7Rd_iGgVOXF1ENK6dcBZScrVfzl-3-R-JO9Glg64u23IyQ3Zof4brk_Af5zyUaKMVxOlIa68qMc9sgDl1f_MrZMJnIc2GUha4au1D4NhMO7vH_ojZdwZw/s1600/lotto.jpg" /></a></div>
Everyone wants to be a millionaire. One of the easiest ways to achieve this goal is through winning a lottery. In Australia, lots of people do have this dream. But have you ever considered about the probability (in mathematics) to win the first division?<br />
<br />
Let me take the Saturday’s lotteries (Monday and Wednesday’s are same) as an example to illustrate this. In this lotto, you need to choose 6 numbers from 45 numbers. If all match, you will win the first division.<br />
<br />
For each game you buy, the probability to win will be calculated as:<br />
<div style="color: red;">
P = (6*5*4*3*2*1)/(45*44*43*42*41*40) </div>
After calculated,<span style="color: red;"> P =0.0000001227738 </span><br />
<br />
In other words, you have a<span style="color: red;"> 1 in 8,145,060</span> chance of winning the lottery.<br />
<br />
If you are rich enough, you can buy 8,145,060 games and you buy them in “Megapick” (each has 36 games). For each Megapick, you need pay $23.90 to a Newsagency.<br />
<br />
So the total cost = <span style="color: red;">23.90 * 8145060 / 36 = AU$5,407,414.83 </span><br />
<br />
That is about $5.4 million. The New Year eve’s (31 December 2011) lottery is 31 million. So if you were the only winner, it would be a very profitable game, because you would make a profit of 25.6 million dollars.<br />
<br />
Unfortunately for the fact is that for the New Year eve’s lottery there were 32 winners and each them can only get $968,750.00. If you bought 8,145,060 games as mentioned above, you would lose about 4.43 million dollars. So it’s not a good business for buyers.<br />
<br />
Since each winner has a 1 in 8,145,060 chance of winning, we can roughly estimate how much the lottery company and Newsagencies can make from this time.<br />
<br />
<div style="color: red;">
5.4 million * 32 = 172.8 million dollars.</div>
<br />
Obviously it’s a very good business for them.Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0Sydney NSW, Australia-33.873651 151.2068896-33.8868315 151.1871486 -33.860470500000005 151.22663060000002tag:blogger.com,1999:blog-2954947535512831695.post-9292486094130053512012-01-02T16:01:00.000-08:002012-01-17T20:23:07.681-08:00Parse a String to a Properties<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/isall-numeric.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/string-concatenation-in-java.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<pre class='brush:java;'>
/**
* Feel free to copy and use this method
* If you have any comments or better solutions, please paste on my blog:
* http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com
*
* Sometimes we need to parse a string with key-value pairs into
* a Properties object for later use.
* For example:
* s = "a=1, b=2, c=3";
* Instead of using "split" method, we can parse this easily
* with the Properties load method.
* A piece of testing source code is listed as the following:
*
* String s= "a=1, b=2, c=3";
* Properties properties = parseStringToProperties(s, ',');
* System.out.println(properties.getProperty("a"));
*/
public static Properties parseStringToProperties(String s, char delimiter)
{
Properties properties = new Properties();
try
{
s = s.replace(delimiter, '\n');
StringReader reader = new StringReader(s);
properties.load(reader);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return properties;
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-73414413144383018112012-01-01T02:59:00.000-08:002012-01-17T21:17:23.400-08:00The Power of Apple<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/sydney-new-years-eve-fireworks-2012.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/math-in-australian-lottery.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='600' height='480' src='https://www.youtube.com/embed/m2mac7mZLm0?feature=player_embedded' frameborder='0'></iframe></div>
<br />Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-14751559553810568582011-12-31T22:44:00.001-08:002012-01-17T21:18:17.148-08:00Sydney New Years Eve Fireworks 2012<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/world-welcomes-2012.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/power-of-apple.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<object class="BLOGGER-youtube-video" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" data-thumbnail-src="http://0.gvt0.com/vi/_crNUYe3q8g/0.jpg" height="480" width="600"><param name="movie" value="http://www.youtube.com/v/_crNUYe3q8g&fs=1&source=uds" />
<param name="bgcolor" value="#FFFFFF" />
<embed width="600" height="480" src="http://www.youtube.com/v/_crNUYe3q8g&fs=1&source=uds" type="application/x-shockwave-flash"></embed></object></div>
<br />Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-58842793726825306302011-12-31T22:37:00.001-08:002012-01-17T21:19:04.729-08:00World welcomes 2012<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/aircraft-picture.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2011/12/sydney-new-years-eve-fireworks-2012.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='600' height='480' src='https://www.blogger.com/video.g?token=AD6v5dwHaKodkQp-3Imjvt4b9xGylCBQJqSQk6hHahVbEJ2jSBgJZS2Fo6TUSnqZjc5P-m-78zkBSmim517n2jzieA' class='b-hbp-video b-uploaded' frameborder='0'></iframe></div>
<br />Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-70311429812752831742011-12-31T15:40:00.000-08:002012-01-17T21:03:23.146-08:00Idiom-Value<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/human-cloning.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/women-in-china-today.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
We all work hard to make money. However we always find that the currency is not worth the paper it’s printed on because of the inflation. From 1937 until 2010, the average inflation rate in Australia was 6.02 percent. So we have to find a way to get our money's worth.
<br>
<br>
Investing in property offers the best bang for the buck. The beauty of it is, if have properties for investment, you can claim a tax rebate in every financial year. Moreover, according to the historic record, the price of houses in Australia doubled every 7 years. So it's well worth it.
<br>
<br>
Before we purchase a property, we should not take everything for granted and we’re better to do some investigation by ourselves. We also don’t have to believe everything which the agent tells us. Instead, we should make a mockery of the property when we negotiate with the agent. Normally the property market is quiet around the Christmas holiday, but it will pick up speed after that.
<br>
<br>
Even though owners with many properties often look down on someone who has fewer properties, we should not risk purchasing too many properties at one time. Otherwise if the price of house goes south, which even just happened once in a blue moon in Australia, the bottom may fail out of the property market.Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0tag:blogger.com,1999:blog-2954947535512831695.post-3439250492186430322011-12-31T15:11:00.000-08:002012-01-17T20:24:10.216-08:00Is All Numeric?<style type="text/css">
a.chapter:link {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:visited {text-decoration:none;color:#98bf21;background-color:transparent}
a.chapter:hover {text-decoration:underline;color:#98bf21;background-color:transparent}
a.chapter:active {text-decoration:none;color:#98bf21;background-color:transparent}
table.chapter
{
width:100%;
height:18px;
font-size:100%;
margin:0px;
padding:0px;
}
table.chapter td.prev {width:20%;text-align:left;}
table.chapter td.category{width:*;text-align:center;}
table.chapter td.next {width:20%;text-align:right;}
hr
{
background-color:#d4d4d4;
color:#d4d4d4;
height:1px;
border:0px;
clear:both;
width:98%;
}
</style>
<br/>
<table class="chapter">
<tr>
<td class="prev"><a class="chapter" href="/2011/12/get-value-for-xml-element.html"><< Previous</a></td>
<td class="category"><a class="chapter" href="/p/table-of-categories.html">Table of Categories</a></td>
<td class="next"><a class="chapter" href="/2012/01/parse-string-to-properties.html">Next >></a></td>
</tr>
</table>
<hr/>
<br/>
<pre class='brush:java;'>
/**
* Feel free to copy and use this method
* If you have any comments or better solutions, please paste on my blog:
* http://blog.yanmingyu.com
* or email me feedback.andrewyu@gmail.com
*
* This method uses Regular Expression to check if a string just contains numeric
*
*/
public static boolean isAllNumeric(String s)
{
return s.matches("\\d+");
}
</pre>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script language="javascript" type="text/javascript">
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>Andrew YUhttp://www.blogger.com/profile/07579848436229741684noreply@blogger.com0