The concept of multiple threads and locks

Source: Internet
Author: User

Priority of Threads

The priority value is 1-10 (the higher the value, the higher the priority).

Public final int getpriority (); Gets the value of the thread priority.

Public final void setpriority (int newpriority), and modify the priority of the thread.

Note: High priority does not necessarily mean that the thread must run first, it can only represent the likelihood that the thread will run first.

Common methods for controlling thread cycles

Wait () Frees the CPU's execution power and releases the lock.

Notify () back to the state before the wait.

Yied () lets the thread temporarily pause. (Let the thread release the resource)

Join () to force the thread to join the execution.

Setdaemon (TRUE) sets the thread to be a background thread (the background thread is bound to end together when the current thread ends).

Note: The end thread principle is to let the Run method end, so just control the run process.

Why to Thread Sync

Sharing code and data between threads can save system overhead and improve efficiency. However, it can also cause data access conflicts. How to implement an organic interaction between threads, and make sure that the shared resource can only be accessed by one thread at a time, that is, thread synchronization.

Data that is shared between multiple threads is called a critical resource.

Mutual exclusion Lock

Each object corresponds to a mutex token, guaranteeing that only one thread at a time can access the object.

The keyword synchronized of a mutex can be written on a method (an object that invokes the method on behalf of the lock), and can be enclosed outside the statement to be locked.

Benefits: Resolves thread-safe issues

Disadvantages: Reduced operating efficiency (to determine the lock, and can not share information);

Deadlock:

Two-thread a,b uses the same object s (s) as a shared resource, and thread A creates the conditions after B runs in execution. In this premise a starts running, enters the synchronized block, the object S is locked, then thread a waits for the end of B to run into the blocking state, then B starts to run, but because cannot access the object s, thread B also enters the blocking state, waits for s to be unlocked by thread A. The final result: two threads are waiting for each other to run.

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.