Dark Horse programmer Java SE (3)

Source: Internet
Author: User
Tags thread class java se

----<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Immediately following the previous issue, continue to review the Java basics of knowledge points, mainly to review the content of the thread !

1. Upward transformation: Animal a = new Cat ();//auto type promotion, cat object promoted to animal type, but unique function cannot be accessed. Function: Restricts access to specific functions of an object.

Downward transformation: Jinzhao Animal A = new Cat (); Cat C = (cat) A; If you also want to use specific animal cat features, you can change the object down. The purpose of the downward transformation is to use the unique features of subclasses

Note: For transformation, all the while, the subclass object is doing the type change

2.instanceof: In order to more accurately identify the true type of an object, it can only be used to reference data type judgments, whereas instanceof is often used for robustness judgments before downward transformation.

3. Polymorphism 3 Features:

(1) member variables:

Compile-time: Refer to the class in which the reference variable belongs has a member variable called, has, compiled pass, no, compile failed. Runtime: Refer to the class in which the reference variable belongs, and run the member variable in the class to which it belongs. Simply put: Compile and run are all reference to the left of the equals sign (Fu f = new Zi ();)

(2) member function:

At compile time, refer to the reference variable in the class described in whether there is a function called, there is, compile through; No, the compilation fails. At run time, the reference is whether there are functions called in the class to which the object belongs. Simply put: Compile to look to the left and run to see the right.

(3) static function:

Compile, run to see whether the reference variable belongs to the class to call the static method, simply say: Compile run all look to the left. For static functions, objects are not required, as long as they are called with the class name

(Inner Class)

4. Internal class Access features: (1) An inner class can directly access members in an external class (including private members in an external Class) (2) An external class to access an inner class, an object of the inner class must be established in the outer class

When analyzing things, it is found that there is something in the description of the thing, and that the thing is still accessing the content of the described thing. In this case, the other things are defined as internal classes to describe

5. If a static member is fixed in an inner class, the inner class must also be static.

6. Why do internal classes have direct access to external class members?

That's because the inner class holds references to external classes. External class name. This

7. Inner classes can only access locally-modified local variables in local locations

8. Anonymous inner class: is the shorthand of the inner class (which is essentially an anonymous subclass object). Must have a premise: an inner class inherits or implements an external class or interface

The difference between 9.error and exception:

Error: The JVM throws much of the severity of the problem, generally can not be handled, the occurrence of this problem generally do not take the targeted treatment-directly modify the program, but the system underlying error problems.

Exception: Exception, is a general problem.

Thread

10. What is a program? What is a process? What is a thread?

Program: is the executable process on the device: Running program, is the program dynamic execution process (the program) thread: Process internal single sequence control flow concurrency: The operating system divides time into a lot of time fragments, evenly distributed to the running program, micro-walk stop, macro on the run, which The phenomenon is called concurrency.

11. Multithreading: Refers to the concurrent execution of multiple sub-program flows in the same process. When the JVM starts, multiple threads are started, at least two threads are started, one is the main thread that executes the main function, and the other is a garbage-collected thread

12. What is the purpose of creating a thread? is to create an execution path that executes the specified code and other code to run concurrently.

There are two ways to create threads in 13.Java: (1) One is to inherit the thread class, override the Run () method (2) to implement the Runnable interface, and thread through thread and the class that implements Runnable.

14. Benefits of implementing the Runnable Interface: (1) Separating the thread's tasks from the child classes of the thread, and encapsulating them individually, encapsulating the tasks into objects according to the object-oriented thinking. (2) Avoids the limitations of Java Single inheritance (note: Thread security is a concern when using multiple threads)

15. What are the causes of thread safety problems? (1) Multiple threads operating shared data (2) manipulating the thread code that shares the information has multiple lines

16. When a thread executes multiple code processes that share data, other threads participate in the operation and cause thread safety problems.

The solution is to encapsulate the thread code that has multiple operations sharing data, and when the thread executes the code, the other threads are not allowed to participate in the operation. The other threads are not allowed to participate in the operation until the code is executed by the front-end thread.

(1) In Java, this problem can be solved by synchronizing blocks of code.

Synchronous code block Format: Synchronized (object) {

Code blocks that need to be synchronized

}

Benefits of synchronization: Resolves thread security issues

The disadvantage of synchronization is that it reduces efficiency, because threads outside the thread will judge the synchronization lock.

Prerequisites for synchronization: Multiple threads must be in sync and use the same lock

(2) Synchronization function: The lock used by the synchronization function is fixed.

17. What is the difference between a sync function and a synchronized code block?

The lock used by the synchronization function is a fixed this. The lock used by the synchronization code block is any object (recommended)

18. The static synchronization function uses the following: The function belongs to the bytecode file object, can be obtained using the GetClass () method, or the current class name. class.

Dark Horse programmer Java SE (3)

Related Article

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.