Java Multithreaded Learning notes--simple

Source: Internet
Author: User

Process: The process of executing a program (Task)-dynamic.

Holds resources (shared memory, shared files) and threads.

Thread: A thread is the smallest execution unit in the system, with multiple threads in the unified process and threads sharing the resources of the process.

Thread Interaction: mutual exclusion and synchronization.

Note: Multithreading is asynchronous, so do not take the order of the code in Eclipse as the order in which threads are executed, and the timing of the thread being invoked is random.

Java Support for threading:

Class Thread Interface Runnable Common Run method

Thread creation and startup:

Thread Common methods:

How to stop a thread:

There are three ways to terminate a running thread in Java:

1, use the exit flag, so that the thread exits normally, that is, when the Run method completes, the thread terminates.

2. Use the Stop method to forcibly terminate the thread, but this is not recommended because stop and suspend and resume are obsolete methods, and using them may produce unpredictable results.

3. Use the interrupt method to break the thread.

Threads that cannot be stopped:

Call the interrupt () method to stop the thread, and when the interrupt () method does not use the same effect as the For+break statement, stop the loop immediately. Calling the interrupt () method simply hits a stop mark in the current thread and does not really stop the thread.

To determine if a thread is a stopped state:

In the Java SDK, there are two methods available in the Thread.java class:

1, this.interrupted (): Test whether the front-line has been interrupted.

2, this.isinterrupted (): Test thread has been interrupted.

Difference: The current thread is the thread (static method) that runs the this.interrupted () method when the front-line is interrupted. This.interrupted (): Tests if the front thread is already in the interrupt state and has the ability to clear the status flag to false after execution. This.isinterrupted (): Tests whether the thread object is already in a broken state, but does not clear the status flag.

Anomaly method

Stop in the sleep

The violence stops (notice the Stop () and the Java.lang.ThreadDeath exception, releasing the bad consequences of the lock).

Use return to stop the thread.

Pause thread (suspend suspend thread with resume resume thread execution), disadvantage: exclusive, out of sync.

Yield method: Discard the current CPU resources, will give other tasks to occupy CPU execution time. But the time to give up is uncertain, it is possible to just give up, and immediately get the CPU time slice.

Thread Priority: Set the priority of the thread using the SetPriority () method, the priority of the thread is divided into 1-10 of these 10 levels, if less than 1 or greater than 10, then the JDK throws an exception throw new illegalargumentexception.

The inheritance attribute of thread precedence: a thread initiates a B thread, and the B thread has the same priority as a.

Precedence has rules: high-priority threads are always executed mostly first, but not high-priority threads.

Daemon Thread:

There are two kinds of threads in a Java thread, one is a user thread and the other is a daemon thread. The daemon thread is a special thread, and his features have the meaning of "companionship", and the daemon thread automatically destroys when there is no non-daemon thread in the process. A typical daemon thread is a garbage collection thread, and when there is no non-daemon thread in the process, the garbage collection thread will not have the necessary, automatic destruction. That is, any daemon is a "nanny" for all non-daemons in the entire JVM, as long as any non-daemon thread in the current JVM instance does not end, the daemon is working, and the Guardian thread friend with the JVM as soon as the last non-daemon thread ends. The role of Daemon is to facilitate the operation of other threads, the most typical application of the daemon is the GC (garbage collector).

Java Multithreaded Learning notes--simple

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.