Get to know the threads and processes with a little slack

Source: Internet
Author: User

One: Process and thread Introduction

Process: A process is the execution of a program (task) that holds resources (shared memory, shared files), and threads. For example, ECLIPSE,QQ is a process after running.

Thread: A function of the process runtime, such as chatting with QQ typing, uploading photos are different threads.

Features of the thread: 1. A thread is the smallest execution unit in the System 2. There are multiple threads in the same process 3. Resources for thread-sharing processes

For example: If the process is likened to a class, each student in the class can be regarded as a thread, a broom in the classroom, and a rubbish bin (resource) are the shared resources of the students.

Two: Multiple Threads of interaction

Race condition: When multiple threads share access to the same data (in a memory region), each thread attempts to manipulate the data, causing the data to be corrupted, a phenomenon known as a race condition.

Ways to resolve race conditions: mutual exclusion and synchronization of threads

Mutex: Only one thread can operate on our critical data or critical sections at the same time.
Synchronization: A communication mechanism between threads, such as when a thread finishes something, and somehow tells other threads that I'm done.

Mutually exclusive solutions to threads:
First define a lock object private final Object lockobject = new Object ()

Then use the Synchronized keyword and lock.
Synchronized (lockobject) add a lock to our thread, Java syntax guarantees the same time, only one thread obtains our LockObject lock object
For example:
Synchronized (lockobject) {
Key code (code that requires a mutex) ....
}

Thread Synchronization Solution:
Wait (), the thread goes into waiting set
Notify (), wakes a thread in wait set (Waiting pool), which one, random
Motifyall (), wakes all the threads in the wait set (waiting pool), and the awakened thread can compete for resources.

Let's give you a minute to learn about thread mutex and synchronization

Goddess in Sunday want to date, Zhang San, John Doe, Harry, small east all want to date goddess, but goddess in order to prevent the cock silk fight, the same time can only with a boy date, the final goddess decided to date with small east, small east on the goddess added a lock (synchronized keyword Other dicks are out of the way, and that is mutual exclusion.
But one weekend the goddess was ill, the cock silk came to the goddess again, the goddess said I was ill to stay at home, not a date, you all wait for it (equivalent to call the Wait () method, the Cock silk are in wait set medium), Next weekend the goddess sent a message to the cock ("I'm sick, I can date today", which is equivalent to the Notifyall () method), and the cock thread happily comes out of the waiting pool (waitset), and it's all about the goddess, which is synchronization.

Three: Common methods in threading

Thread.yield (); The target thread yields the CPU, and the next time which thread executes is random
Thread.Join (); The target thread will be executed, not interrupted, and other threads will wait for
Thread.Sleep (1000) The target thread sleeps for some time, the parameter is millisecond, 1000 is 1 seconds

Four: Easy to use the wrong way in the thread

Stop () method This is a bad way to exit the thread, because, after the thread calls this method, the thread has not finished, it will be forced to stop, you think, if this happens in our database of a transaction to operate, what will happen?
The correct way to exit a thread is to set a flag bit keeprunning, when the thread executes when Keeprunning=true is Keeprunning=false, and the thread exits, so that the thread exits the thread after execution. This ensures thread security, which is the recommended way to exit threads.

Some people say that the interrupt () method can also stop the current thread, in fact the interrupt () method is not intended to stop the thread, interrupt () is to interrupt our thread, if we just call the interrupt () method thread will not stop, will continue to execute. If we have to use interrupt () to stop the thread execution, we need to know about the isinterrupted () method, return a Boolean value (but this disadvantage is that the operation can not be combined with sleep (), will be wrong, so generally do not use the situation please yourself). The implementation principle is similar to the above setting flag bit exit thread.

Crossing, do you have a clear understanding of the process and thread, next time we see ~ Thank you for watching

Get to know the threads and processes with a little slack

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.