20165223 Java Learning Summary for the eighth week of Java programming

Source: Internet
Author: User
Tags switches thread class cpu usage

Summary of the contents of textbook Learning 12th Chapter-java Multithreading mechanism Essentials
    • Threads in Java
    • Thread class and the creation of threads
    • Common Methods of threading
    • Thread synchronization
    • Reconciling threads for synchronization
    • Thread Federation
    • GUI thread
    • Timer thread
Problems in teaching materials learning and the solution process 1. Processes and Threads
    • Program: Static code, application execution Blueprint
    • Process: A dynamic execution of a program
    • Threads: A process that is smaller than a process can produce multiple threads, a thread's terminal and recovery can save system overhead, and a multithreaded process is more practical.
2. Main thread
    • That is, the main thread, the JVM loading code, the thread that was started after the main method was found
    • When the main method returns, the JVM ends the Java application (the virtual machine exits automatically)
    • The JVM waits until all threads in the Java program end before ending the Java application
3. The State and life cycle of a thread

The Java language uses objects of the thread class and its subclasses to represent threads

    • Four different states
      • new : An object of the thread class or its subclasses is declared and created, and the corresponding memory space and other resources are already available
      • Run :
        • When the JVM switches the CPU usage to the thread, the thread can start its own lifecycle independently from the main thread that created it
        • After a thread is created that only occupies memory resources, the Start () method (the method inherited from the parent class) must be called to inform the JVM that the new process is queued
        • The program must override the run () method of the parent class in the subclass, and do not let the thread call the start () method before it ends the run () method, or the illegalthreadstateexception exception will occur
      • interrupts : Four types of interrupt causes:
        • The JVM switches the CPU resources from the current thread to other threads, leaving the thread out of the CPU's right of use in a broken state
        • During thread usage of CPU resources, the sleep (int millsecond) method is executed to put the current thread into hibernation
        • Executes the wait () method, which causes the current thread to enter a wait state, which must be notified by other threads calling the Notify () method, so that it goes back into the thread queue waiting for CPU resources to be queued
        • Performing an operation into a blocking state, such as performing a read/write operation, causes blocking. Threads are re-queued to wait for CPU resources only when the cause of the blocking is eliminated
      • death : The thread ends up with the ability to continue running and frees the memory allocated to the thread object. Two causes of death:
        • Run () method ends after normal execution
        • The run () method is terminated in advance with a mandatory
4. Thread scheduling and prioritization
    • The priority level of the thread is not explicitly set, and the priority defaults to 5, which isThread.NORM_PRIORITY
    • Thread priority is setPriority(int grade) adjusted by method, requires an int parameter, and the parameter range is within 1~10, that is Thread.MIN_PRIORITY , in the and Thread.MAX_PRIORITY , if no longer within this range throws an IllegalArgumenException exception
5. Thread class and creation of threads
    • Thread class
      • Construction Method:Thread(Runnable target)
      • Parameter: The parameter of the constructor method is an interface of type runnable
      • Example: To pass an instance of an Runnable interface class to a parameter
6. Thread Common methods
    • start (): The thread calls the method to start the thread and queue it from the new state into the ready queue, and once it comes to the CPU resource, it can start its own life cycle independently from the thread that created it
    • run (): The Run () method of the thread class is the same as the function and function of the run () method in the Runnable interface, and is used to define the operations that are performed after the thread object is dispatched and is a method that the user program must not refer to when the system is called automatically
    • sleep (int millsecond): A high-priority thread can call the sleep method in its run () method to make itself discard CPU resources and hibernate for a period of time
    • isAlive (): When the thread is in the "new" state, the thread calls the IsAlive () method to return false. The thread calls the IsAlive () method to return True before the run () method ends, that is, before the death state is entered.
    • CurrentThread (): This method is a class method in the thread class and can be called with the class name, which returns the thread that is currently using the CPU resource
    • interrupt (): A thread that occupies a CPU resource can let the dormant thread invoke the interrupt () method to "wake up" itself, that is, the thread that caused the sleep interruptedexception exception, thus ending hibernation, Re-queued for CPU resources
7. Coordinating Threads for synchronization
    • Thread synchronization: That is, a number of threads need to use a synchronized decorated method. That is, several threads in a program need to use a method, and this method is decorated with synchronized
    • Synchronization methods: wait() , notify() , notifyAll() . Are final methods of the object class, inherited by all classes, and not allowed to be overridden
    • cannot be used in a non-synchronous method wait() , notify()notifyAll()
Problems in code debugging and the thinking and comprehension of solving process

This chapter explores another major feature of Java-Threading. After learning, I learned that mastering multi-threaded programming technology can make full use of CPU resources, in the practical problems of the application of more extensive and more convenient.

Code Hosting

See Code Cloud: Code Cloud Operations

Learning progress Bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative) Important Growth
Goal 3000 rows 30 Articles 400 hours
Eighth Week 1324/200 2/11 20/20 Depth
Resources
    1. Instructional Videos
    2. Intellj Idea Easy Tutorial

20165223 Java Learning Summary for the eighth week of Java programming

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.