In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the case analysis of dirty data in Java program". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Java program dirty data problem example analysis" it!
Dirty data (Out-of-date data) refers to outdated data.
If there is dirty data in your Java program, it will more or less bring some problems to the software system, such as: unable to apply the changed configuration in real time, some inexplicable, difficult to reproduce, serious errors in the software system, and so on. It is very valuable to avoid dirty data as much as possible. This article hopes to give some help to colleagues in this respect.
Fragment 1. Dirty data problem of caching technology
/ * *
* A report printer is used to print a report.
*
* @ version 1.09 Universe 2003
* @ author Bill
, /
Public class ReportPrinter {
/ * *
* Constructs a ReportPrinter instance.
, /
Public ReportPrinter () {
/ / do something...
}
/ * *
* Prints a printable.
*
* @ param printable the specified printable object
, /
Public void print (Printable printable) {
Graphics g = getGraphics ()
G.setFont (getReportFont (printable.getFont ()
Printable.print (g)
}
/ * *
* Returns the corresponding report font of a java font.
*
* @ param javaFont the specified java font
* @ return the corresponding report font
, /
Private Font getReportFont (font javaFont) {
Font reportFont = fontMap.get (javaFont)
If (reportFont = = null) {
ReportFont = loadFont (javaFont)
FontMap.put (javaFont, reportFont)
}
Return reportFont
}
/ * *
* Loads the corresponding report font of a java font.
*
* @ param javaFont the specified java font
* @ param the corresponding report font
, /
Protected static Font loadFont (Font javaFont) {
Font reportFont = null
/ / do something...
Return reportFont
}
/ * *
* The font map (java font- > report font).
, /
Private static HashMap fontMap = new HashMap ()
}
In Fragment 1, caching technology is used to avoid this overhead because of the high report font overhead corresponding to loading a java font. This is a common way to improve performance and works well in general. However, the design and implementation of Fragment 1 may be incomplete, because it is very likely that the report font corresponding to a java font will change after the system is booted, and after this change, it can only be loaded by restarting the software system, which is often one of the complaints of the end users. To make matters worse, the existence of such dirty data could have other serious and unimaginable consequences.
How to avoid the dirty data problems caused by the use of caching technology?
Updates to cached data should be clearly defined when designing, implementing, and testing:
i. It is necessary to restart the software system without considering the update of the cached data.
ii. Cached data cannot become dirty data without considering the update of cached data (but in software systems, it is often "impossible" to become "possible" after refactoring again and again)
iii. Consider the update of the cached data, and update the cached data in real time when the source data changes.
Fragment 2. Dirty data problem in Singleton schema
/ * *
* A storage usage handler is used to query the storage usage of users.
*
* @ version 1.09 Universe 2003
* @ author Bill
, /
Public class StorageUsageHandler {
/ * *
* Returns a StorageUsageHandler instance.
*
* @ return the single StorageUsageHandler instance
, /
Public static StorageUsageHandler getStorageUsageHandler () {
If (handler = = null) {
Handler = new StorageUsageHandler ()
}
Return handler
}
/ * *
* Constructs a StorageUsageHandler instance.
, /
Private StorageUsageHandler () {
Users = Context.getAllUsers ()
}
/ * *
* Returns the storage sizes of all the users.
*
* @ return the storage sizes
, /
Public long [] getSizes () {
Long sizes [] = new long [users.size ()]
For (int I = 0; I
< users.size(); i++) { sizes[i] = getOneSize(users.get(i)); } } /** * Returns the storage size of a user. * * @param user the specified user * @return the storage size */ protected long getSize(User user) { // do something... return 0; } /** * The StorageUsageHandler singleton. */ private static StorageUsageHandler handler; /** * The users. */ private List users; } 您看出了问题所在吗? Fragment 2中,由于没有必要次次实例化StorageUsageHandler而带来不必要的开销,采用了Singleton模式以保证StorageUsageHandler只被实例化一次。 在实例化SotrageUsageHandler时,StorageUsageHandler的类成员users将被赋值。由于不存在任何对users重新赋值的方法,一直驻留在软件系统中的users将不会发生任何变化。在软件系统启动之后,增加、删除或修改用户的操作经常会发生,而一旦发生这类操作,users就成为了脏数据,Fragment 2将无法正常工作。 如何避免使用Singleton模式所带来的脏数据问题呢? 对于Singleton类的类成员: i. 对于与Singleton类外部无依赖关系的类成员,不存在这种问题; ii. 对于依赖于Singleton类外部的类成员,且该类成员不存在更新机制,最好是将其去掉,需要时从Singleton类外部直接获取;如果这种办法不可行,应提供机制以确保在使用该类成员之前,该类成员已经被更新过。 Fragment 3. 类使用的脏数据问题 /** * A storage usage handler is used to query the storage usage of users. * * @version 1.0 9/9/2003 * @author Bill */ public class StorageUsageHandler implements AdminHandler { /** * Constructs a StorageUsageHandler instance. */ private StorageUsageHandler() { users = Context.getAllUsers(); } /** * Returns the storage sizes of all users. * * @return the storage sizes */ public long[] getSizes() { long sizes[] = new long[users.size()]; for(int i = 0; i < users.size(); i++) { sizes[i] = getOneSize(users.get(i)); } } /** * Returns the storage size of a user. * * @param user the specified user * @return the storage size */ protected long getSize(User user) { // do something... return 0; } /** * Displays the storage usage of users. * * @param req the http servlet request * @param res the http servlet response * * @throws IOException * @throws ServletException */ public void process(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); res.setHeader("Cache-Control", "no-cache"); res.setHeader("Pragma","no-cache"); res.setDateHeader("Expires", 0); PrintWriter writer = new PrintWriter(res.getOutputStream()); long sizes[] = getsizes(); writer.println(""); writer.println(" "); for(int i = 0; i < sizes.length; i++) { writer.print(" "); } "); writer.print(users.get(i) + ": " + sizes[i]); writer.println(" writer.println(""); writer.flush(); writer.close(); } /** * The users. */ private List users; } /** * An admin servlet as a http servlet to process the admin http servlet * request and response. * * @version 1.0 9/9/2003 * @author Bill */ public class AdminServlet extends HttpServlet { /** * Initiates the configuration. * * @param config the servlet config * * @throws ServletException */ private void initConfig(ServletConfig config) throws ServletException { // do something... handlerMap.put("__storage_Usage__", new StorageUsageHandler()); } /** * Processes the http servlet request and response. * * @throws IOException * @throws ServletException */ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { AdminHandler handler = handlerMap.get(req.getParameter("handler")); if(handler == null) { // do something... return; } handler.process(req, res); } /** * The admin handler map(handler name->Handler).
, /
Private HashMap handlerMap = new HashMap ()
}
You must have seen the problem.
In Fragment 3, because StorageUsageHandler does not follow the Singleton pattern, although the class member users of StorageUsageHandler can only be assigned when instantiating StorageUsageHandler, in single-threaded mode, there is basically no problem as long as you ensure that each StorageUsageHandler instance used is newly instantiated.
The problem is that during initialization of the AdminServlet, the StorageUsageHandler is instantiated and stored. After that, unless servlet container reloads AdminServlet, you will not be able to reinstantiate StorageUsageHandler or update StorageUsageHandler's class member users. In this way, after adding, deleting, or modifying users, users will become dirty data.
How to avoid the dirty data problems caused by the use of classes?
i. This problem does not exist for class members who have no dependency on the outside of the class
ii. For class members that depend on the outside of the class, and there is no update mechanism for class members. It is best to remove it and obtain it directly from outside the class when needed; if this approach is not feasible, a mechanism should be provided to ensure that the class member has been updated before using the class member; if this approach is not feasible, please state clearly how the class is used to prevent improper class use.
At this point, I believe that everyone on the "Java program dirty data problem example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.