Git tutorial

Git is a distributed revision-control system.

JMH example

Java Microbenchmark Harness is a good tool to test your code. It can tell you the code is efficiently or not. Today I will show you how to use it to test your code. Let's say you want to implement a functionality about probability. Maybe you want your program have a 1/10 probability of doing something. You have two ways to implement this.

Linux yum show installed packages

Let's say you have installed some packages in your Linux. The following commend help you show installed packages. Use yum and grep commends can help you find installed packages.

PostgreSQL display result vertically

In MySQL we can add \G in the end of sql to show the result vertically. In PostgreSQL you need to enter \x to show the result vertically. It is a toggle. Example sql is here.

Relative time in computer

In this book Systems Performance: Enterprise and the Cloud author make a single CPU cycle equivalent to one second to make us feel how fast CPU is.

Java8 parse string to LocalDateTime

SimpleDateFormat is the only choice when you want to parse String to Date before Java8. But, SimpleDateFormat is unthread-safe class. You may get the wrong result when you use it in multi-thread scenario. I recommend you use DateTimeFormatter to handle date string.

Java8 convert LocalDateTime to Date

There are two ways to convert LocalDateTime to Date in java. Both of them need convert LocalDateTime to Instant then convert Instant to Date. LocalDateTime and Instant are new classes in Java8 I recommend you to use them instead of Date. Here is the sample code.

Verify SimpleDateFormat is not thread safe

Don't use SimpleDateFormat in multi-threaded scenario. It is not thread-safe class. Here is the code to verify it. I start 10 threads to parse string to date with the same instance of SimpleDateFormat.

Spring Boot maven plugin example

spring-boot-maven-plugin can help you build runnable Spring Boot jar. Add following config in your pom file and execute clean install you will get a runnable jar file.

gson parse json to object array

In this page I will show you how to use gson parse json string to object array. Add gson dependency in pom.xml file.

Java8 CompletableFuture example

We can use stream().parallel() to handle tasks in multi-thread. CompletableFuture is very useful when you handle tasks which need to wait IO or network. Let's say you have two services. The one is getting content by contentId and another is getting author information by authorId. Both of them need RPC. In other words, you have to wait the remote server responses. I use Thread.sleep() to mock network delays.

Java convert list to array

We can use Arrays.asList() to init an array with values. This page will show you how to convert List to T[]. For example convert List to String[]. The code is here.

Java determine thread pool size

You may have used ThreadPoolExecutor to submit some task and waiting for the result. In this way program can get the better performance. The question is how to calculate the size of pool size, in other words how to make CPU 100% or close to 100% busy?

Java8 add log in stream

Stream in Java8 can help us make code clear and efficient. We can use Stream to iterate a collection without the keyword for. It is very convenient. The question is how to print the information of item in a stream? In this page I will show you how to add log in stream. JDK provides peek function for debugging.

MyBatis Spring example

In this page I will show you how to integrate MyBatis and Spring. I use maven to build the project. The structure of the project is like following.