Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the sequence number in JAVA

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to get a sequence number in JAVA. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Take the sequence number in JAVA

When writing a system, it is often encountered to use a unique key value to identify an object, such as the primary key of a record in the database and the file name of the file.

Wait. It is a common practice to change this key value from 1, 2, 2, 3. This value is added up, but it will be time-consuming. Such as the records in the database, you need to

First find the largest serial number, and then add 1, so that you have done a database select operation, such as in the file system, then

It is even more troublesome to read out all the file names and sort them first.

Here is a simple and quick way to use the system clock. In Java, you can use System.currentTimeMillis ()

Get the number of milliseconds corresponding to the previous system time, the time will not be repeated, so you can rest assured to use. In order to meet the situation of concurrent use, you can add

Enter synchronization.

Here is a sample code that you can refer to:

Package comm

Import java.util.Random

Public class UId {

Private static Random random = new java.util.Random ()

Private static final int ID_BYTES = 10

Public synchronized static String generateId () {

StringBuffer result = new StringBuffer ()

Result = result.append (System.currentTimeMillis ())

For (int I = 0; I < ID_BYTES; iTunes +) {

Result = result.append (random.nextInt (10))

}

Return result.toString ()

}

}

In the above code, a 10-digit random number is added after the millisecond of the system to ensure different values for concurrency in the same millisecond.

Of course, there are some limitations in using this method, one is that you cannot modify the time at will, and the other is that the key values are not in order.

Thank you for reading! This is the end of the article on "how to get the sequence number in JAVA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report