Java advanced application

1. Exception handling

Exception handling is to throw exceptions for possible errors in the program. Either terminate the program, or proceed to avoid the error condition. The syntax that needs to be used is try (avoid). throws (throw possible conditions).

In actual development, exception handling is necessary. Because, for example, compiled exceptions, if there is no exception handling. The popular program will not be compiled, such as the following; //This is possible when compiling, Java is a rigorous language.

 basic concept:

Unusual architecture:

 Main exception types:

The way of java exception handling;

There are two methods: Method 1: try-catch-finally

              Method 2: throws + exception type

12. The use of try-catch

First introduce the first way to handle exceptions. This is the catch-and-throw model treatment. It is used in compiled and running exceptions, among which, it is more convenient to use running exceptions in this way. Because compiled exceptions must be written, it feels easier to code through throws.

basic concept:

 

Precautions:

The use of try-catch must pay attention to the inheritance relationship of exception classes. Subclass exceptions are placed above. You can view the inheritance relationship by pressing Ctrl+h.

Two methods that exist in the exception class: printStackTrace() and getMessage need to be kept in mind.

13. The use of finally

finally is used in conjunction with try-catch, and finally is an essential operation in database operations and data flow operations. It is a block of code that is guaranteed to be executed. The priority is higher than return. Such operations are used on data streams to close a data stream or disconnect a database connection. This prevents memory leaks. If there is no shutdown operation. It is likely that the gc mechanism will not recycle the database because there is still a connection to the database externally. Thus causing a memory leak. It has a high priority, see the picture below:

Note that the final result of this question is that 11 is related to the temporary space of the stack. 

basic concept:

Reason for use:

 The main reason is to prevent memory leaks, but finally is also used in other situations. For example, an exception also occurs in the catch code block. In order to allow subsequent code to execute. You must add finally. This also enhances code robustness.

Extension: Unit Testing

There are two ways to create unit tests, and the second way is the easiest in the case of networking.

The second way: 

 

Interview questions:

 Final style:

14. Exception handling method 2 throws

The second type of exception handling is throws. This way is pseudo-handling exceptions. Its essence is to throw an exception to the parent class or the main program class. This method is often used in large-scale development scenarios with multiple subcategories. But if it is a compilation exception, you must handle try-catch for the exception in the main class. For running exceptions, there is no need to handle the exceptions thrown.

basic concept:

The essence did not handle the exception.

 How to choose exception handling method:

Understanding "manually" and "automatically" throwing exceptions:

Manually throwing exceptions is necessary, and it is used for code problems that do not meet specific scenarios. For example: when the student's id<0 is entered, the scene requirements are not met, but the program will not go wrong. You need to manually throw an exception: id cannot be less than 0. This method is more convenient than if-else.

 

Note on throws:

How to customize exception class?

Examples of enumeration classes: (see object-oriented knowledge)

Two, multithreading

Multithreading is actually an execution process in a process. For example: qq message box, sending a message is a process. Multiple message boxes are multiple threads. The following is a process (red line box), and there are different threads in the process.

Programs, Processes, and Threads

  • Program : A program written in a language to accomplish a specific task 一组指令的集合. That is 一段静态的代码, static objects.

  • Process : An execution of a program, or an application running in memory . Such as: QQ in operation, Netease music player in operation.

    • Each process has an independent memory space . Running a program in the system is the process of a process from creation, operation to death. (life cycle)

    • Programs are static, processes are dynamic

    • As a process 操作系统调度和分配资源的最小单位(it is also the basic unit of the system to run programs), the system will allocate different memory areas for each process at runtime.

    • Most modern operating systems support multi-process and support running multiple programs at the same time. For example: Now we are using the editor and the screen recording software in class, and at the same time, we also open the drawing board, dos window and other software.

  • Thread (thread) : The process can be further refined into a thread, which is internal to the program一条执行路径 . A process has at least one thread.

    • If a process 并行executes multiple threads at the same time, it supports multithreading.

Need to understand the concepts of parallelism and concurrency:

Parallelism can be understood as multiple people doing things.

Concurrency is one person doing multiple things. And everything has to be interactive.

Preemptive scheduling:

Let 优先级高the thread to 较大的概率use the CPU first. If the threads have the same priority, one will be randomly selected (thread randomness), and Java uses preemptive scheduling.

Why do you need to learn multithreading?

Background: Taking a single-core CPU as an example, using only a single thread to complete multiple tasks one after another (calling multiple methods) is definitely shorter than using multiple threads to complete it. Why do we still need multi-threading?

Advantages of multithreaded programs:

  1. Improve application responsiveness. Makes more sense for graphical interfaces and enhances user experience.

  2. Improve CPU utilization of computer system

  3. Improve program structure. Divide a long and complex process into multiple threads and run independently, which is easy to understand and modify

Usage scenarios: FTP downloads, databases, distributed computing, browser web services, background tasks.

 The cost of data exchange between processes is very high, such as Taobao jumping to Alipay.

21. Thread creation method 1:

Steps 1 and 2 require additional creation of a subclass and an overridden method run(). The way of calling is as follows: Here is an example of multi-threading, using different threads respectively.

 

 Multithreaded example:

Precautions: 

 

 

Create an anonymous object through an anonymous subclass:

22. Thread creation method 2:

Implementation:

23. Compare the two creation methods:

Extended example: 

24. Thread common structure method

Constructor in thread:

Commonly used methods for threads:

 life cycle:

25. Thread safety issues

The thread safety problem is that when multiple threads access the same resource, they all read or write the resource, which is prone to security problems, for example, there are multiple threads running () at the same time. Then, before the first data reduction is over, the second process accesses the data. As a result, the final result will have the same name or even a negative number.

basic concept:

Thread synchronization mechanism (safety lock):

The principle of synchronization: 

 Sample scheme and notes:

sychronized is a method, and the parameters inside are objects, and they are the only objects.

The first is to perform synchronization lock problems in the method of implementing the interface:

It should be noted that the way of the interface is to share the same instance space, and this can represent the only instance object.

 The second method implements the synchronization lock problem in the way of inheritance:

It should be noted that inheritance is single inheritance, which means that calling three processes uses three inheritance spaces. Cause this will not be the only object. At this time, you need to use this class.class to represent the unique object.

 

 summary:


Because the efficiency is too low, I just do copy and paste work, and I temporarily gave up this method. Choose, start with other people's matching study notes. Review the entire knowledge point every time you study a chapter. Requirements: memory, serious attitude. efficiency.

Guess you like

Origin blog.csdn.net/qq_55928086/article/details/132211843