20165205 2017-2018-2 "Java Programming" Eighth Week study summary

Source: Internet
Author: User
Tags thread class

20165205 2017-2018-2 "Java program design" Eighth Week study summary textbook study summary
  • Processes and Threads
    • A thread is not a process, but behaves like a process, and a thread is a smaller execution unit than a process.
    • Unlike processes, threading interrupts and restores can be more cost-efficient.
  • Threads in Java
  • A major feature of the Java language is the built-in support for multithreading.
  • thread state and life cycle:
    • NEW: Java language When an object that uses the thread class and its subclasses is declared and created, the newborn thread object is in the new state.
    • Run and block: When the ready state thread acquires the CPU execution slice, it goes into a running state, but during execution, the thread may get into a blocking state because of the following reasons
      • the CPU execution slice is exhausted and the JVM switches to another thread to execute
      • thread calls sleep ()
      • thread calls the blocking Io method, and the thread blocks until the method returns
      • thread tries to get synchronization monitor held by another thread
      • thread is waiting for a notification
      • program calls the thread's suspend () to suspend the thread. (Easy to deadlock, not recommended)
    • After the thread has run into a blocking state, and then can only continue to block or re-enter the ready state, the following situation causes the thread to re-enter the ready state from the blocking state, the
      • thread called Slee () has passed A blocking Io method called by the specified time
      • thread returns the
      • thread that successfully obtains the synchronization monitor
      • thread receives a notification from another thread
      • suspended (suspend) thread is called by the program again Res Ume method
    • death: The thread ends in a dead state, the thread ends in the following three ways,
      • run () or call () completes gracefully, the thread ends gracefully
      • thread throws a Uncaught exception or error
      • direct calling thread's Stop () method ends thread, easy deadlock
      • Note that once a child thread is started, its status and the main thread are the same, so once the main thread is finished, the child threads are not affected. The IsAlive () method that does not follow the end thread object is in ready, running, and when blocking returns True, when new, death returns false on a thread that has already died calling start () is not valid and throws an exception. The thread of death cannot be executed again as a thread. For a new thread, calling two times the start () method also throws an exception
  • Thread class and process creation
    • All Java threads must be instances of thread or its subclasses. Here's how:
      • Define the THEAD subclass and implement the Run () method, run () is the thread execution body
      • Create this subclass instance object, that is, the thread object was created
      • Call the Start () method of the thread object to start the thread
  • Common Methods for threading:

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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 and waiting for CPU resources to be queued again.
  • Synchronization of Threads
    When dealing with multithreading issues, we have to be aware of the problem when two or more threads are accessing the same variable at the same time, and a thread needs to modify the variable. We should deal with such problems.

Thread synchronization is a method that requires a synchronized modification for several threads. That is, several threads in a program need to use a method, and this method is decorated with synchronized.

Multiple threads calling the synchronized method must adhere to the synchronization mechanism: When a thread uses this method, other threads must wait until the thread finishes using the method. When using multithreading to solve many practical problems, some methods of modifying data may be modified with the keyword synchronized.

    • Thread Federation
      • The join method of a thread indicates that one thread waits for another thread to finish before executing. After the join method is called, the thread object is in a blocked state. Some people also call this approach a union thread, which means that the current thread and the thread on which the current thread is located are joined together as a thread. Which thread calls the method, then the thread executes the other thread before executing it.

        Textbook Learning Code debugging problems and the process of solving:
    • Question 1: What is the difference between a thread and a process?
    • Issue 1 Solution: The process is static and is a mirror
      A thread is a dynamic program that, when run, opens a thread and executes the method inside to open up more new threads and the process is a mechanism that the operating system uses to show the user how to run the program.
      So in real development, whether it's Java or any other language, there are only threads.

    • What is the difference between the problem 2:start () method and the Run () method?
    • Workaround for Issue 2: Only the Start () method is called to show the multi-threading feature, and the code in the Run () method of the different threads executes alternately. If you just call the run () method, then the code is executed synchronously, and you must wait for the code in the Run () method of one thread to execute after all of it has been executed, and the other thread can execute the code inside its run () method.

Code Hosting

Summary of the wrong question
    • The exception type for JDBC programming is divided into AC

      • A.sqlexception
      • B.sqlerror
      • C.sqlwarning
      • D.sqlfatal
      • E.sqltruncation
    • The type returned by executeupdate is int, meaning that it is affected by record a
      • A.true
      • B.false
    • The following are common database Acde
      • A.access
      • B.xammp
      • C.mysql
      • D.oracle
      • E.sql
Summarize

This week I learned about multi-threading in Java, and when I started learning I was wondering why I was using multithreading, and then I learned that after learning this chapter, I learned that using the multi-core CPU to prevent blocking, easy to model, and flexible use of multi-threading would be much easier.

20165205 2017-2018-2 "Java Programming" Eighth Week study summary

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.