Home  >  Article  >  Java  >  How to use Java's finalize() method

How to use Java's finalize() method

王林
王林forward
2023-04-30 14:43:06924browse

An alternative usage of finalize() in

Java

Anyone who has done JAVA programming knows that there is a garbage collector mechanism in JAVA. When it is running (usually in Automatically runs when the system memory reaches a certain limit), which will recycle the memory occupied by objects that are no longer used. Therefore, in JAVA programs, we usually only consider creating objects and never care about the clearing of objects. Finalize() is a special method provided by JAVA for classes.

The working process of the garbage collector is roughly like this: Once the garbage collector is ready to release the storage space occupied by useless objects, it first calls the finalize() method of those objects, and then actually reclaims the object's memory. By using finalize(), you can perform some special work while the garbage collector is running. The following example illustrates a clever use of finalize().

Nowadays, more and more commercial application systems adopt WEB form. In WEB-style applications, each page visit is independent and not related. Even if multiple users access the same page of the application at the same time, the users do not know each other. What should you do if you want to check which users are currently using the system (for example, when preparing to restore data backup or perform system upgrades, system administrators would like to know this information)? WEB servers based on Servlet and Jsp technology provide hidden It contains Session and Application objects, which can help developers achieve the continuous saving and sharing of some information. When a user accesses a WEB application, the WEB server will automatically create a Session object, which can be used by the user to share data in all pages of the application during the session; Application is a global object of the WEB application. Using Session and Application objects, you can achieve the purpose of tracking all user information.

When the user opens the browser and starts to request the login page of the WEB application, the WEB service creates a session for the customer. After that, within the timeout time of the session, the customer uses this session (the timeout time can be set , such as the Tomcat server is set in the web.xml file of each application). If you use IE browser, Session has a corresponding relationship with the connection jointly identified by the client IP address and client program process ID. Windows with the same IP address and the same process (such as a new window opened through IE-File-New-Window) have the same A session, so session can be used to identify each independent client application connection.

The following is an example:

In order to facilitate processing, first create a simple class (user) to express user information and store sessionId:

package com;  public class user {  public String name="";  public String sessionId="";  }

Another class ( testSession) is used to process the user's login, logout and other action information, so that the system can track the currently connected user information.

package com;  import java.util.Vector;  import com.user;  public class testSession {  public user User;  private Vector vsid;  public testSession()  {  User=new user();  }  public boolean verify(String username,String password)  throws Exception //验证用户/密码  {  return true;  }  public void setSessionVar(String sesid,Vector sid) {  this.User.sessionId=sesid;  this.vsid=sid;  }  private static synchronized void addappses(user puser,  Vector pvsid) { //记录一个新连接的用户  int pos=-1;  user l_user;  if (puser==null || pvsid==null)  return;  for(int i=0;i  l_user=(user)pvsid.get(i);  if(l_user.sessionId.equals(puser.sessionId)){  pos=i;  break;  }  }  if(pos==-1){  pvsid.add(puser);  }  else{  pvsid.set(pos,puser);  }  }  private static synchronized void removeappses(user puser,  Vector pvsid) { //移除一个退出的用户  int pos=-1;  user l_user;  if (puser==null || pvsid==null)  return;  for(int i=0;i  l_user=(user)pvsid.get(i);  if(l_user.sessionId.equals(puser.sessionId)){  pos=i;  break;  }  }  if(pos!=-1){  pvsid.remove(pos);  }  }  protected void finalize() {  this.removeappses(this.User,this.vsid);  }  public boolean login(String username) throws Exception  { //处理登录  this.User.name=username;  this.addappses(this.User,this.vsid);  return true;  }  public boolean logout() throws Exception  { //处理注销  this. finalize();  this.User=null;  this.vsid=null;  return true;  }  }
###Each user creates a testSession object to save the user's information. In order to describe the class testSession, another file logintest.jsp must be introduced at the same time. This JSP file for example provides a simple interface for login and logout processing. The file content is as follows: ###
<%@ page import=" com.testSession,  java.util.Vector"%>  <%@page contentType="text/html;charset=GBK" %>  <% request.setCharacterEncoding(response.  getCharacterEncoding());%>  <%  String actionType=request.getParameter("actiontype");  String actionResult="";  if(actionType!=null) {  if(actionType.equals("login")){ // -1-  String userName=request.getParameter("username");  if(userName==null || userName.equals("")){  ;  }  else{  String password=request.getParameter("password");  if(password==null)  password="";  testSession ts=  (testSession)session.getAttribute("testSession");  if(ts!=null) { //-1.1-  session.removeAttribute("testSession");  if( !ts.User.name.equals(""))  ts.logout();  }  ts=new testSession();  if(!ts.verify(userName,password)) {  //验证用户与密码,看是否合法用户  actionResult="login fail";  //非法用户,显示错误信息  }  else{ //验证成功  session.setAttribute("testSession",ts);  Vector app_vts=  (Vector)application.getAttribute("app_vts");  if(app_vts==null) {  app_vts=new Vector();  application.setAttribute("app_vts",app_vts);  }  ts.setSessionVar(session.getId(),app_vts);  ts.login(userName);  actionResult=userName+" login success";  }  }  }  if(actionType.equals("logout")){  testSession ts=  (testSession)session.getAttribute("testSession");  if(ts!=null) {  session.removeAttribute("testSession");  if( !ts.User.name.equals("")){ //-2-  actionResult=ts.User.name;  ts.logout();  }  session.invalidate();  }  actionResult=actionResult+" logout success";  }  }  else actionResult="null";  %>

The above is the detailed content of How to use Java's finalize() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete