Java pen question-thread programming aspects

Source: Internet
Author: User

Ja

Thread Programming aspects

60. There are several ways to implement a thread in Java? What keyword modifies the synchronization method? How are the Stop () and suspend () methods not recommended?

A: There are two implementation methods, namely inheriting the thread class and implementing the Runnable interface

Using the Synchronized keyword to modify the synchronization method

Against the use of Stop () is because it is unsafe. It unlocks all locks acquired by the thread, and if the object is in an incoherent state, then other threads can examine and modify them in that state. The result is hard to check out the real problem. The suspend () method is prone to deadlocks. When calling suspend (), the target line routines stops, but still holds the lock that was acquired before. At this point, no other thread can access the locked resource unless the thread that is "suspended" resumes running. For any thread, if they want to restore the target thread while trying to use any of the locked resources, it will cause a deadlock. So instead of using suspend (), you should place a flag in your own thread class that indicates whether the thread should be active or suspended. If the flag indicates that the thread should hang, use wait () to enter the wait state. If the flag indicates that the thread should be resumed, restart the thread with a notify ().

61. What is the difference between sleep () and wait ()?

A: Sleep is a thread-class method that causes this thread to pause execution for a specified time, giving the execution opportunity to other threads, but the monitoring state remains and is automatically restored when it is. Calling sleep does not release the object lock.

Wait is a method of the object class that calls the wait method on this object to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.

62. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described.

Answer: If the data is shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then the data is shared and must be accessed synchronously.

When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches.

63. Start a thread with run () or start ()?

A: Starting a thread is calling the start () method so that the virtual processor represented by the thread is in a running state, which means it can be dispatched and executed by the JVM. This does not mean that the thread will run immediately. The run () method can produce a flag that must be exited to stop a thread.

64. When a thread enters an synchronized method of an object, does the other thread have access to other methods of this object?

A: No, an synchronized method of an object can only be accessed by one thread.

65. Please say the thread synchronization method you know.

A: Wait (): causes a thread to be in a wait state and releases the lock of the object it holds.

Sleep (): Makes a running thread sleep, is a static method that calls this method to catch the interruptedexception exception.

Notify (): Wakes up a waiting thread, noting that when this method is called, it does not actually wake up a waiting state thread, but is determined by the JVM to wake up which thread, and not by priority.

Allnotity (): Wakes all the threads that are in the waiting state, noting that they do not give all the wake-up threads an object lock, but instead let them compete.

66, multi-threaded there are several ways to achieve, what is it? How are there several implementations of synchronization?

A: There are two ways to implement multithreading, namely inheriting the thread class and implementing the Runnable interface

There are two implementations of synchronization, namely Synchronized,wait and notify

67. Basic concepts of threads, the basic state of threads, and the relationship between states

A: A thread is an execution unit that executes a program's code during the execution of a program, and each program has at least one thread, the program itself.

There are four types of threads in Java: Run, ready, suspend, end

68, briefly describe the similarities and differences between synchronized and Java.util.concurrent.locks.Lock?

Answer: The main same point: Lock can complete all the functions that synchronized implements

Main differences: Lock has more precise line semantics and better performance than synchronized. The synchronized automatically releases the lock, and lock must require the programmer to release it manually, and must be released in the finally clause.

JSP aspect

69. The difference between forward and redirect

A: Forward is the server requests the resources, the server directly accesses the URL of the destination address, reads the response content of that URL, and then sends the content to the browser, the browser does not know where the server sends the content from, so its address bar is the original address.

Redirect is the server based on logic, send a status code, tell the browser to request the address again, in general, the browser will use all the parameters just requested again, so the session,request parameters can be obtained.

70. What are the built-in objects of JSP?

A: JSP has the following 9 basic built-in components (which can correspond to the 6 internal components of ASP):

Request client requests, this request will contain parameters from the Get/post request

Response Web page back to the client's response

The properties of the PageContext Web page are managed here

Session period associated with the request

What the application servlet is doing

Output used to transmit a response

Config Servlet's architectural parts

Page JSP webpage itself

Exception exceptions for error pages, not captured

71. What are the actions of JSP?

A: JSP has the following 6 basic actions

Jsp:include: A file is introduced when the page is requested.

Jsp:usebean: Find or instantiate a JavaBean.

Jsp:setproperty: Sets the properties of the JavaBean.

Jsp:getproperty: Outputs the properties of a JavaBean.

Jsp:forward: Move the request to a new page.

Jsp:plugin: Generates an object or embed tag for a Java plug-in based on browser type

72. What is the difference between dynamic include and static include in JSP?

A: Dynamic include is implemented with Jsp:include action

<jsp:include page= "included.jsp" flush= "true"/> It always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters

Static include is implemented with the include pseudo-code, and will not check for changes in the included files, for static pages

<%@ include file= "included.htm"%>

73. What are the differences between the two ways of jumping?

Answer: There are two kinds, namely:

<jsp:include page= "included.jsp" flush= "true" >

<jsp:forward page= "nextpage.jsp"/>

The former page does not turn to the page that the include refers to, but displays the result of the page, the main page or the original. It will return after execution, equivalent to a function call. and can take parameters. The latter completely turns to the new page and will not come back. Equivalent to the go to statement.

74, JSP's built-in objects and methods.

A: request represents a HttpServletRequest object. It contains information about browser requests and provides several useful ways to get Cookie,header, and session data.

Response represents the HttpServletResponse object and provides several methods for setting the response to the browser (such as cookies, header information, etc.)

The Out object is an instance of Javax.jsp.JspWriter and provides several methods that you can use to echo the output to the browser.

PageContext represents a Javax.servlet.jsp.pageContext object. It is an API for easy access to a wide range of namespaces, servlet-related objects, and a way to wrap generic servlet-related functionality.

The session represents a requested Javax.servlet.http.HttpSession object. Session can store the user's status information

Applicaton represents a Javax.servle.ServletContext object. This helps to find information about the servlet engine and the servlet environment

Config represents a Javax.servlet.ServletConfig object. This object is used to access the initialization parameters of the servlet instance.

Page represents a servlet instance that is generated from this page

servlet aspect

75. Say a servlet's life cycle?

A: The servlet has a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the init,service and destroy methods of the Javax.servlet.Servlet interface. When the servlet is instantiated by the server, the container runs its Init method, the service method is run when the request arrives, and the service method automatically dispatches the Doxxx method (Doget,dopost) corresponding to the request, etc. The Destroy method is called when the server decides to destroy the instance.

The difference from CGI is that the servlet is in a server process, it runs its service method in a multithreaded manner, one instance can serve multiple requests, and its instances are generally not destroyed, and CGI generates a new process for each request, which is destroyed after the service is completed. So the efficiency is lower than the servlet.

76. What is the difference between forward () and redirect () in the JAVA SERVLET API?

A: The former is only the control in the container of the steering, in the client browser address bar will not show the post-turn address, the latter is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser. Therefore, the former is more efficient, when the former can satisfy the need, try to use the forward () method, and, as such, it also helps to hide the actual link. In some cases, for example, you need to jump to a resource on a different server, you must use the Sendredirect () method.

77, the basic framework of the servlet

For:

public class Servletname extends HttpServlet {

public void Dopost (HttpServletRequest request, httpservletresponse response) throws

Servletexception, IOException {

}

public void doget (HttpServletRequest request, httpservletresponse response) throws

Servletexception, IOException {

}

}

78, under what circumstances call Doget () and Dopost ()?

A: The method property in the form tag in the JSP page is called doget () when it is get, and Dopost () is called when it is post.

79. The life cycle of the servlet

A: The Web container loads the servlet, beginning with the life cycle. The servlet is initialized by calling the servlet's init () method. By invoking the service () method implementation, different do*** () methods are called depending on the request. To end the service, the Web container invokes the servlet's Destroy () method.

80, how to live servlet single-threaded mode

Answer: <% @pageisThreadSafe = "false"%>

81. How to transfer objects between pages

Answer: Request,session,application,cookie, etc.

82. What are the similarities and differences between JSP and servlet, and what are their connections?

A: JSP is an extension of servlet technology, which is essentially a simple way for servlets to emphasize the appearance of the application. The JSP is compiled with a "class servlet". The main difference between Servlets and JSPs is that the application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. The case for JSP is that Java and HTML can be combined into a file with a. jsp extension. JSPs focus on Views, and Servlets are used primarily for control logic.

83. Four kinds of conversation tracking technology

Answer: Session scope servletsjsp page description

Page no is a representation of the objects and properties associated with a page. A page is represented by a compiled Javaservlet class (which can have any include directives, but no include actions). This includes both Servlets and JSP pages that are compiled into Servlets

Request is an object and property that represents a request that is made to a Web client. A request may span multiple pages, involving multiple Web components (due to the relationship of the forward directive and the include action)

A session is a representation of objects and properties that are related to a user experience for a Web client. A web session can and will often be requested across multiple clients

Application is a representation of objects and properties related to the entire Web application. This is essentially a global scope that spans the entire Web application, including multiple pages, requests, and sessions

84. The main method of request object

For:

SetAttribute (Stringname,object): Sets the parameter value of the request named name

GetAttribute (Stringname): Returns the property value specified by name

Getattributenames (): Returns a collection of names of all properties of the request object, resulting in an instance of an enumeration

GetCookies (): Returns all cookie objects for the client, as a result of a cookie array

Getcharacterencoding (): Returns the character encoding in the request

Getcontentlength (): Returns the length of the requested body

GetHeader (stringname): Get header information defined by the HTTP protocol

Getheaders (Stringname): Returns all values of the Requestheader of the specified name, resulting in an instance of an enumeration

Getheadernames (): Returns the name of the Requestheader so that the result is an instance of an enumeration

getInputStream (): Returns the requested input stream for obtaining the data in the request

GetMethod (): Obtaining a client's way of transmitting data to the server side

GetParameter (Stringname): Gets the parameter value specified by the client that is passed to the server side by name

Getparameternames (): Gets the name of all parameters that the client sends to the server side, and the result is an instance of the enumeration

Getparametervalues (Stringname): Gets all values with the parameter specified by name

Getprotocol (): Gets the protocol name on which the client transmits data to the server

GetQueryString (): Get query string

Getrequesturi (): Gets the client address that issued the request string

GETREMOTEADDR (): Gets the IP address of the client

Getremotehost (): Gets the name of the client

GetSession ([booleancreate]): Return and request the relevant session

getServerName (): Gets the name of the server

Getservletpath (): Gets the path of the script file requested by the client

Getserverport (): Gets the port number of the server

RemoveAttribute (stringname): Delete One of the properties in the request

85, we often encountered in the Web application development process output some kind of encoded characters, such as iso8859-1, how to output a certain encoding of the string?

For:

Publicstringtranslate (STRINGSTR) {

Stringtempstr= "";

try{

Tempstr=newstring (Str.getbytes ("iso-8859-1"), "GBK");

Tempstr=tempstr.trim ();

}

catch (Exceptione) {

System.err.println (E.getmessage ());

}

RETURNTEMPSTR;

}

86. What are the general implementations of the servlet implementation?

For:

Publicvoidinit (Servletconfigconfig)

Publicservletconfiggetservletconfig ()

Publicstringgetservletinfo ()

Publicvoidservice (Servletrequestrequest,servletresponseresponse)

Publicvoiddestroy ()

JDBC, JDO aspects

87. What is the role of Class.forName?

A: Calling this access returns an object of a class that specifies the class name as a string.

88. What is JDO?

A: JDO is a new specification for Java object Persistence, the abbreviation for Java data Object, and a standardized API for accessing objects in a data warehouse. JDO provides transparent object storage, so for developers, storing data objects does not require additional code (such as the use of the JDBC API). These tedious routines have shifted to the JDO product provider, freeing developers to focus time and energy on business logic. In addition, the JDO is flexible because it can run on any data underlying. JDBC is a more generic, relational database (RDBMS) JDO that provides storage capabilities to any underlying data, such as relational databases, files, XML, and Object Databases (Odbms), and so on, making application portability even stronger.

89. What is the working mechanism of the data connection pool?

A: The Java EE server starts up with a certain number of pooled connections and maintains not less than this number of pooled connections. When a client program needs to connect, the pool driver returns an unused pooled connection and memento it as busy. If there is currently no idle connection, the pool driver creates a new number of connections, and the number of new connections is determined by the configuration parameters. When a pooled connection call is completed, the pool driver memento the connection to idle, and other calls can use the connection.

90. What is JDO?

A: JDO is a new specification for Java object Persistence, the abbreviation for Javadataobject, and a standardized API for accessing objects in some kind of data warehouse. JDO provides transparent object storage, so for developers, storing data objects does not require additional code (such as the use of JDBCAPI). These tedious routines have shifted to the JDO product provider, freeing developers to focus time and energy on business logic. In addition, the JDO is flexible because it can run on any data underlying. JDBC is a more generic, relational database (RDBMS) JDO that provides storage capabilities to any underlying data, such as relational databases, files, XML, and Object Databases (Odbms), and so on, making application portability even stronger.

XML aspects

91. What are the parsing techniques of XML? What is the difference?

A: There are dom,sax,stax, etc.

DOM: When working with large files, its performance drops very badly. This problem is caused by the tree structure of the DOM, which takes up more memory, and the DOM has to load the entire document into memory before parsing the file, which is appropriate for random access to the XML sax: Dom,sax is an event-driven XML parsing method. It reads the XML file sequentially and does not need to load the entire file at once. When it encounters a file beginning with a document ending, or the beginning of a label with the end of the label, it triggers an event that the user processes the XML file by writing the processing code in its callback event, which is appropriate for sequential access to the XML

Stax:streaming ApI for XML (STAX)

92. What aspects of XML technology do you use in your project?

A: The use of data storage, information configuration two aspects. In the data exchange platform, the data source can not be assembled into an XML file, and then the XML file is compressed and packaged and encrypted over the network to the receiver, receive decryption and decompression, and then the corresponding information in the XML file for processing. In the software configuration, the use of XML can be easily carried out, the various configuration parameters of the software are stored in the XML file.

93. There are several forms of XML document definition. What are the essential differences between them? What are the different ways to parse an XML document?

A: The two forms Dtdschema,b: The essential difference: The schema itself is XML, can be parsed by the XML parser (this is also the fundamental purpose of developing schema from the DTD), C: There are dom,sax,stax and so on

DOM: When working with large files, its performance drops very badly. This problem is caused by the tree structure of the DOM, which takes up more memory, and the DOM must load the entire document into memory before parsing the file, which is appropriate for random access to the XML

SAX: Not present in Dom,sax is an event-driven XML parsing method. It reads the XML file sequentially and does not need to load the entire file at once. When it encounters a file beginning with a document ending, or the beginning of a label with the end of the label, it triggers an event that the user processes the XML file by writing the processing code in its callback event, which is appropriate for sequential access to the XML

Stax:streamingapiforxml (STAX)

Java pen question-thread programming aspects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.