20165202 2017-2018-2 "Java Programming" 8th Week study Summary

Source: Internet
Author: User
Tags thread class

20165202 2017-2018-2 "Java Programming" 8th Week study summary Summary of learning contents of textbookCh12
    • Processes and Threads
      • A thread is a smaller unit than a process, and a process can produce multiple threads during its execution
    • Threads in Java
      • Main thread

        Each Java application has a default main thread. When the JVM loads code and discovers the Main method, it starts a thread called the main thread, which is responsible for executing the main method.
      • The state and life cycle of a thread

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

        The newly created thread typically undergoes the following four states in its full life cycle:

        1. NEW: When an object of the thread class or its subclasses is declared and created, the new thread object is in the new state.

        2. Run: The thread must call the Start () method (the method inherited from the parent class) to notify the JVM, so the JVM will know that another new thread is waiting to be switched. Once it comes to the CPU resource, it can start its own life cycle independently of the main thread that created it.

        3. Interruption: There are 4 reasons for the interruption:

        The JVM switches the CPU resources from the current thread to other threads, leaving the thread out of the CPU's right to use the interrupt state.

        While the thread is using CPU resources, the sleep (int millsecond) method is executed to bring the current thread into hibernation.

        The wait () method was executed during thread usage of CPU resources.

        During a thread's use of CPU resources, an operation is performed into a blocking state.

        4. Death: A thread in the dead state does not have the ability to continue running, freeing the memory allocated to the thread object

    • Thread class and the creation of threads
      • In the Java language, you create a thread object with the thread class or subclass, and when you write a subclass, you need to override the run () method.
      • Using the Thread class: Thread (Runnable target)

  Example12-3、4

  • Common Methods of threading
    • 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 of 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, which are methods that the system automatically calls and that the user program must not refer to.
    • Sleep (int millsecond): A high-priority thread can call the sleep method in its run () method to allow itself to abandon 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 of the threads 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.
  • Thread synchronization
    • Several threads need to use a method at the same time and need to be decorated with synchronized. When a thread uses the Synchronized method, other threads must wait with the wait () method if they want to use it until the thread finishes using the method
    • While other threads do not need to wait while using the synchronization method, the Notifyall () method is used to notify all waiting threads to end the wait while the method is exhausted.
  • Reconciling threads for synchronization
    • The wait () method interrupts the execution of the method, causes the thread to wait, temporarily yields the CPU, and allows other threads to use the synchronization method.
    • The Notifyall () method notifies all waiting threads that are waiting by using this synchronization method. The thread that was interrupted will continue to execute this synchronization method from the point where it was just interrupted, and follow the principle of "first break first".
    • The Notify () method simply notifies a waiting thread of the end of a wait.
    You cannot use the wait (), notify (), Notifyall () method in a non-synchronous method.
  • Thread Federation
    • While a thread A is occupying CPU resources, it is possible for other threads to call join () and this thread union, such as:
      B.join ();
      Called a during the run Union B.
    • If thread A is federating a B thread during the time it occupies CPU resources, the A thread will immediately break execution until it has finished executing the thread B of its union, and a thread then re-queued for CPU resources to resume execution.
    • If a ready-to-federate B-thread has ended, then B.join () will have no effect.
  • GUI thread
    • When a Java program contains a graphical user interface (GUI), the Java Virtual machine runs the application and automatically starts more threads
    • Awt-eventquecue thread is responsible for handling GUI events
    • Awt-windows thread is responsible for drawing a form or component to the desktop
  • Timer thread
    • Timer thread

      Construction method: Timer(int a,object b) , A is in milliseconds, B is the monitor of the timer

      Ring only once: Call Setreapeats (Boolean B) method, B evaluates to False

      To start the timer:start()

      Stop timer:stop()

      Restart Timer:restart()
  • Daemon Threads
    • A thread can call the void Setdaemon (Boolean on) method to set itself as a daemon thread.
    • You must set whether you want to daemon the thread before running.
Problems in code debugging and the resolution process
    • Question 1: Last week's MySQL hasn't been installed.

    • Problem 1 Solution: Refer to Lou Teacher's blog Xmapp installation to this step can not find the corresponding entrance

You should 127.0.0.1 select the red box from the insidephpMyAdmin

Code Hosting

last week's summary of the wrong quiz

1. Interface rowset inherits the interface __, you can call the method __ in rowset to populate its data area.
①statement②resultset③update④populate

A. ①③

B. ①④

C. ②③

D. ②④

Correct answer: C

Parsing: The populate method can only be used to write data

2. The following data manipulation languages are

A. Insert

B. Update

C. Create

D. Select

Correct answer: A B

My answer: A B D

Analysis: I do not understand why there is no D, according to the Baidu Encyclopedia on the definition of data manipulation language, select should also belong to the operating language ah?

Learning progress bar
lines of code (new/cumulative) Blog Volume (Add/accumulate) Learning Time (new/cumulative)
Goal 5000 rows 30 Articles 400 hours
First week 24/24 1/4 15/15
Second week 423/423 2/6 14/29
Third week 702/1109 1/7 16/45
Week Four 918/2027 2/9 16/61
Week Five 1208/3235 2/11 15/76
Week Six 1137/4372 2/13 14/90
Seventh Week 549/4921 1/14 10/100
Eighth Week 1025/5946 3/17 15/115

20165202 2017-2018-2 "Java Programming" 8th Week study Summary

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.