Java algorithm tutorial

This series of blog describes common algorithms.

Java reversing single linked list

In this page I will show you how to reverse single linked list in loop. Before starting write logic we need defined Node class.

Linux find which Java thread consuming cpu

Sometimes Java process used 100% or 200% CPU. It depends on the core number of your CPU. If your CPU has 8 cores that means the highest usage of a process is 800%. When Java process consuming most CPU you can use the following steps to find which Java threads is consuming the most CPU.

Linux locate command example

locate command can help you to find the location of file in Linux. Before using locate you need use updatedb command to update the database. locate command searchs file name in database , find command searchs file in real time. locate is quicker than find.

HttpClient post form example

In this page I will show you how to post a form programmatically. Before testing HttpClient we need to write a API by Spring Boot(or use SpringMVC). The code is here.

Linux yum install rpm file locally

As we all known yum install can help you install the program easily. If the program doesn't exist in yum list you can download them and install locally.

Java find n'th node from the end of linked list

linked list is one the most common data structure. You can find the next node by current node, but you can not find the previous node by current(for single linked list). The question is how to find n'th node from the end of linked list.

Java binary search example

"Binary Search" is used for finding the target number in an ordered array. There are two ways to find the index of target number, recursion and looping. The example code is here.

Java ThreadLocalRandom example

ThreadLocalRandom is less contention and better performance than Random. If you need to use random number in multi-thread environment. The example code is here.

Java Thread join example

join() method of Thread class will wait the sub thread to finish then to continue the current thread. "join" looks like the sub-thread join the main thread. If you want to know the result of sub-thread, join can help you blocked the main thread until sub-thread finish(dead).

Java wait notify consumer producer example

wait, notify and notifyAll are all method of Object. It means that every object in java can be a lock monitor. When you use synchronized you get the lock of object. Use synchronized on the method current thread will get the lock of instance. If you add synchronized on a static method that means current thread will get the lock of this Class. These 3 methods can not be used without synchronized (you can release the lock without geting it).

Java URL decode encode example

When you send GET request to server, you need to encode the data which contained in the url.In this way can make sure the data you send could be received by server correctly. Certainly, serve has to decode the data before using them. The example is like following.

Java quick sort example

Quicksort is one of the most common sorting algorithms. The logic of Quicksort is choosing a number (called pivot) and divide the rest into two parts. After first sorting the numbers on the left side are all less than pivot and the numbers on the right side are all bigger than pivot. Then do the same thing for left and right part.

MySQL create multiple indexes in one sql

The statement of creating index on table will lock the table.It will cost a long time if you create multiple indexes in a big table. You can use ALTER TABLE ... to create or drop multiple indexes in one sql. The example is here.

Linux no space left on device

If the message No space left on device appears that means you need clear the file on your system or add new disk for your computer. "No space" not only means no free disk, it may also mean there is no iNodes in your system.