Explanation of all methods in Java multithreading technology

Source: Internet
Author: User
1. Run () and start ()
These two methods should be familiar with each other. Put the code to be processed in parallel in the run () method, and the start thread of the START () method will automatically call the run () method, this is defined by the Java memory mechanism. the run () method must have the public access permission, and the return value type is void.
2. Keyword synchronized
This keyword is used to protect shared data. The premise is to distinguish which data is shared. each object has a lock flag. When a thread accesses this object, the data modified by synchronized will be "locked" to prevent other threads from accessing this object. after the current thread accesses this part of data, the lock flag is released and other threads can access it.
Public threadtest implements runnable
{
Public synchronized void run (){
For (INT I = 0; I <10; I ++)
{
System. Out. println ("" + I );
}
}
Public static void main (string [] ARGs)
{
Runnable R1 = new threadtest ();
Runnable r2 = new threadtest ();
Thread T1 = new thread (R1 );
Thread t2 = new thread (R2 );
T1.start ();
T2.start ();
}
}
The I variable in the above section does not share data, that is, the synchronized keyword does not work here. because t1 and t2 threads are the threads of two objects (R1, R2. the data of different objects is different, so I variables of R1 and R2 are not shared data.
It takes effect only when the code is changed to the following: synchronized keyword.
Runnable r = new threadtest ();
Thread T1 = new thread (R );
Thread t2 = new thread (R );
T1.start ();
T2.start ();
3. Sleep ()
Pause the execution of the current thread (that is, the thread that calls this method) for a period of time, so that other threads can continue to execute, but it does not release the object lock. that is, if there is a synchronized synchronization block, other threads still access Shared data differently. note that this method must capture exceptions.
For example, if two threads run simultaneously (without synchronized), one thread has a priority of max_priority and the other is min_priority. If there is no sleep () method, only after the thread with a high priority is executed, A thread with a lower priority can be executed. However, when a thread with a higher priority is sleep (5000), the thread with a lower priority can be executed.
In short, sleep () can give low-priority threads a chance to execute. Of course, it can also give threads with the same priority and higher priority a chance to execute.
4. Join ()
The join () method enables the thread that calls this method to complete execution before this, that is, wait until the thread that calls this method finishes execution before continuing execution. Note that this method also needs to capture exceptions.
V. Yield ()
Similar to sleep (), the yield () method can only give threads with the same priority a chance to execute.
Vi. Wait () and notify (), policyall ()
These three methods are used to coordinate multiple threads to access Shared data. Therefore, these three methods must be used in the synchronized statement block. as mentioned above, synchronized is used to protect shared data and prevent other threads from accessing shared data. however, the process of the program is not flexible. How can other threads access Shared data before the current thread exits the synchronized data block? In this case, these three methods are used for flexible control.
The wait () method suspends execution of the current thread and releases the object lock mark so that other threads can enter the synchronized data block. The current thread is put into the object waiting pool. when the notify () method is called, an arbitrary thread will be removed from the object wait pool and placed in the lock sign wait pool, only
The lock sign waits for the thread in the pool to obtain the lock sign. If the lock sign waits for no thread in the pool, then notify () does not work.
Policyall () removes all threads waiting for that object from the object waiting in the pool and puts them in the lock sign waiting in the pool.
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.