Java CopyOnWriteArrayList example

It will throw ConcurrentModificationException when multiple threads modify ArrayList. If the elements are not too much, CopyOnWriteArrayList is a good choice. CopyOnWriteArrayList copies elements when add new element. It is not recommend to using it when the elements are too many. (It is expensive to copy too many elements.)

git delete branch

Using git branch -d to delete local branch. Using git push -u origin --delete to delete the remote branch.

git checkout new branch and rename

Using following command to checkout new branch and rename it.

MySQL show current database

Use following SQL to show the database name which you are connecting.

Jackson tutorial

Jackson is a high-performance JSON processor for Java.

git push 403 error

When I use git push to push local changes to github I got this error message:error: The requested URL returned error: 403 Forbidden while accessing https://github.com/ fatal: HTTP request failed. It is easy to solve this problem.

Java clone example

We can use clone method to clone an object. This is a native method in Object (make it faster). If you want to use this method you need to implement cloneable interface. If you want to use this method in different package do not forget change this method to be public. The example code is here.

Java8 calculate time difference milliseconds

In this blog I will show you how to calculate two milliseconds in Java8. Java8 adds a new package java.time. It is easy to handle time by using the class in this package. The example code is here.

Java create deadlock example

Deadlock means multiple workers(threads) are waiting for each other release common resource. In this blog I will create a deadlock example. Two workers both need lock1 and lock2 to finish the task. When one hold lock1 and another hold lock2 they are both waiting for another to release the lock. This state is called dead lock.

Java retainAll example

retainAll method is used to remove the elements which not in both collections. The example code is here.

http header missing

Today I solve a problem about missing http header. I add some extra information in http headers when I request server. The code works well in my local environment. When I deploy it on the remote machine some http headers are missing. After google I solve the problem.

Java initializing order with static inner class

In this page I will show you when static inner class is initialized. I write a demo to help you to understand it. All static fields (include inner class) is initialized when the class is loaded.

SQL group by having example

The key words group by is used to divided records into several groups. Let's say you have a table like following. This table is the score of course for different students. Table name is score_info

Linux mint disable service autostart

For linux mint there are many service scripts in the directory /etc/init.d. Most of them will be executed when user login. Linux uses different directory (such as /etc/rc?.d/) to mark which service and when to run. If you don't want one service auto start you need to remove the link from the directory or to use update-rc.d command like following.

Java read write lock example

ReadWriteLock is used in the scenario that reading operation is more frequent than writing. It is more efficient than synchronized method. Multiple read threads do not block each other, only write thread blocks read thread. The sample code is here.