(1) Common Methods of threading (2) thread synchronization mechanism (3) common sense of network programming

Source: Internet
Author: User

1. Common methods of threading
static void sleep (long Millis)
-Used to put the currently executing thread into hibernation, the sleep parameter specifies the milliseconds (emphasis).
static void sleep (long millis, int nanos)
-For the sleep parameter specified milliseconds + nanoseconds, 1 seconds =1000 milliseconds 1 milliseconds =1000 microseconds 1 microseconds =1000 nanoseconds

int getpriority ()-Used to get the priority of the calling object and return.
void setpriority (int newpriority)-used to set the priority of the calling object.
-The higher the priority, the greater the chance of acquiring a CPU time slice, but does not guarantee that the thread will be executed first.

void Join ()-is used to wait for the thread that the calling object represents to terminate (focus).
void join (Long Millis)-Used to wait for the maximum time that the calling object represents a thread termination to specify milliseconds for the parameter
void join (long millis, int nanos)-Used to wait for the parameter to specify milliseconds plus nanoseconds.

Boolean Isdaemon ()-Used to determine whether the thread represented by the calling object is a daemon thread.
void Setdaemon (Boolean on)-used to set the calling object to represent the thread as the daemon thread.
-The invocation of the method must be preceded by a thread call to the start () method.
-When all non-daemon threads end, the daemon thread ends with it.

2. Thread synchronization mechanism (emphasis)
2.1 Basic Concepts
When multiple threads access the same shared resource at the same time, it can cause problems such as inconsistency or overwrite of data, which requires coordination and communication between threads, which is called thread synchronization mechanism.

2.2 Solutions
The result of the program shows that when two threads withdraw at the same time, the final account balance is unreasonable.
Cause: Line Cheng has not had time to write the withdrawal balance to the database, but thread two has started executing.
Solution: Change the concurrent operation of the thread to the serial operation of the thread.
Raises the problem: This approach causes the efficiency of thread execution to degrade, so it is not necessary to use it later in development.

2.3 Implementation Methods
In the Java language, you can use the Synchronized keyword to implement the synchronous lock/object lock mechanism to ensure the atomicity of thread execution, as follows:
(1) using the mechanism of the synchronous statement block to implement
Synchronized (reference to object) {
Write all code blocks that need to be locked;
}
(2) Use the Synchronized keyword to decorate the entire method, representing the method body that locks the entire method.
This method is equivalent to synchronized (this) {method body;}.

2.4 Realization principle (try to understand)
When multiple threads start to preempt the shared resource at the same time, if one of the threads grabs the shared resource, the other thread enters the blocking/waiting state until the thread executes all the locked code and automatically releases the object lock, while the waiting thread can preempt the shared resource, and the code that grabs the object lock executes the locked code. The thread that cannot be robbed continues to wait.

2.5 Deadlock (understanding)
Code executed by thread one:
public void Run () {

Synchronized (a) {Hold object lock A, wait for object lock b
Synchronized (b) {
code block;
}
}
}
Code executed by thread two:
public void Run () {

Synchronized (b) {Hold object lock B, wait for object lock a
Synchronized (a) {
code block;
}
}
}

Experience:
In future development, remember not to use the nested structure of the synchronous statement block!!!

2.6 Common methods in the object class
void Wait ()
-Used to get the current thread into a wait state until another thread calls the Notify ()/notifyall () method.
void wait (long timeout)
-Used to get the current thread into a wait state until another thread calls the Notify ()/notifyall () method,
Or the milliseconds specified by the parameter have passed.
void Notify ()-a thread used to wake up the wait state (random).
void Notifyall ()-All threads used to wake up the wait state.

(1) Common Methods of threading (2) thread synchronization mechanism (3) common sense of network programming

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.