Java object-oriented programming--14th chapter multithreaded programming

Source: Internet
Author: User

1. There are two types of multitasking: process-based and thread-based.

2, the process refers to a "self-containment" of the running program, directly managed by the operating system, directly run, has its own address space, each process will consume memory once opened.

3. A thread is a single sequential control flow within a process. A process has multiple threads. Multiple threads share the memory space of a process.

4, the process-based feature is to allow the computer to run two or more programs at the same time.

5. Threads are the smallest processing unit in a multi-tasking environment that is thread-based.

6. Process-based costs are less: each process requires the operating system to allocate a separate memory space, and all threads in the consent process are working in the consent memory space, which can share the same block of memory and system resources.

7, the main thread: Main method of a run, the main thread to open.

8. Main thread Features: Start first, end, generate other sub-threads, recycle resources

9. Two ways to create a thread: Declare a subclass of the thread class and overwrite the run () method, declare a class for the Runnable interface, and implement the Run () method.

10, in the thread class, call the start () method and the difference between the run () method: If the direct call to the run () method, is a normal method call, not on the main thread basis, the opening of the child thread is single-threaded, call the Start () method, the main thread on the basis of the child thread, is multithreaded.

11, the thread has eight kinds of states:

NEW: A new way to produce a thread object.

Ready: Start starts running the run () method.

Run: Executes the Run () method.

Death: The Thread Run () method has finished executing.

Wait: The Wait () method in object.

Sleep: Call the Thread Sleep () method.

Suspend: Yield () method, the line Cheng to assign the CPU control right.

Blocking: Waits for IO event input.

12, Thread priority: When two or more threads, while in the ready state, waiting for the CPU to execute, the priority of the high thread is executed. Priority is divided into 1-10 levels, and the higher the number, the higher the priority level.

13. Thread synchronization (thread-safe) when two or more processes concurrently manipulate the same resource, only one thread is allowed to access at the same time, and other threads wait.

14, the keyword synchronized synchronization, can be used as a modifier of the method, indicating that when a thread is accessing the resource, other processes can only wait. When the thread ends access to the resource, other threads are allowed access.

15, the result of synchronization: Data security, low efficiency.

16, there are two ways of synchronization: Synchronous method, synchronization block.

Synchronization method: Public synchronized void Test () {}

Sync block: public void run () {

Synchronized (OB) {}

}

17. The difference between the wait () and sleep () methods:

    • The wait method is defined in the object class, and the sleep method is defined in thread class threads;
    • The wait method can only be placed in a synchronous method or a synchronization block, indicating that the current thread is waiting on the resource, and the sleep method may be placed anywhere, indicating that the current thread is dormant;
    • The Wait method releases the object lock, and the Sleep method does not release the object lock;
    • After the wait method is used, the thread will need to notify wake up to continue execution, and sleep will automatically resume execution after sleep is completed.

18, between the threads through Wait (), notify (), Notifyall () method communication.

19, two threads have a dead loop when they cycle on two synchronization objects.

Java object-oriented programming--14th chapter multithreaded programming

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.