Java learning notes

xiaoxiao2021-03-06  72

Java multi-threaded

First, thread basic concept

Convert 1 program into multiple independently run subtasities. Each subtraction is called a thread.

"Process" refers to a "self-contained" running program. Have your own address space. A process can accommodate multiple simultaneous execution threads.

In fact, one of the most important uses of multi-thread builds 1 "reaction sensitive" user interface.

Second, the use of threads

1. Create a thread

The easiest way is to inherit this class from the Thread class, including everything you need to create and run the thread.

Thread is the most important thing about the RUN method, inheritance, must be overloaded, making it in your own will.

2, several important methods of thread

(1) RUN method

Method for running when using the start method to activate the thread

(2) START method

When the thread is created, in the standby state, only the start method can be activated, so that it is running

(3) SetPriority method

Set the priority of threads, examples: Thet.SetPriority (5).

High priority thread is performed first.

Error Handing with Exceptions Java

First, exception mechanism

1, forced capture errors

2, easy to separate business logic code from exception control code.

Second, the basic violation

1. To distinguish between Exceptional Condition and "Ordinary Issues"

1) Exception conditions: There is no sufficient context to process problems.

2) Ordinary problem: there is enough context to handle problems

2, when you still have an exception, Java will do things:

1) In the heap, create an exception object with New (Exception Object)

2) The program stops working properly and is solved by the Exception Handler.

3, throwable object

For different error types, Different exception processing objects are required.

The wrong information is saved in the object of the Throwable object and the THROW exception object.

Third, the exception capture

1. If a method throw is an exception, it must assume that this exception will be captured (Catch).

2, Guarded Region

Possible exceptions Code Region

Try

{

Guarded Region

}

You can place the code to check the error in the TRY block and then capture possible errors.

3, exception controller (Exception Handlers)

Try

{

// Code That Might Generate Exceptions

}

Catch (Type ID1)

{

// Handle Exceptions of Type1

}

Catch (Type ID2)

{

// Hand Exceptions of Type2

}

// ETC

Description: Sometimes the parameters of Catch Clause are not used in the program, but the parameters must still be given.

Fourth, exception standard (specification)

1. If one of the classes, "throw" exception, then the violation type is required when the class method is defined.

Void f () THROWS TOOBIG, Toosmall, Divzero

{

}

2. If a method declares that the "throws" declaration is declared, the caller must capture an exception, which is forced by the Java compiler.

3, even, in the method, you don't actually don't "throw" exception, you can still use the "throws" keyword.

V. Some descriptions of abnormal treatment

1. You can use the throws keyword in a method declaration of an Abstract Class class.

2, if the method declares with the Throws parameters, the caller must capture it.

3, CATCH clause: Catch (anomaly class anomaly class variable) 4 When a method is overloaded, you can only define the exception type defined in the infrastructure.

This limitation is not suitable for the structure of the structure.

5, violation match

(1) After throwing an abnormality, the abnormal control system searches the most recent controller in the order of the original preparation.

(2) A derivative object can match the basic class of one controller

(3) If the basic class of the CatCH clause is before, it will compile an error.

转载请注明原文地址:https://www.9cbs.com/read-87640.html

New Post(0)