JavaSE -- thread synchronization, javase thread

Source: Internet
Author: User

JavaSE -- thread synchronization, javase thread

Why is thread synchronization required?

Synchronization is to work in a coordinated pace and run in a predetermined order. For example, if you have finished speaking, I will not talk about it together. The word "same" should refer to collaboration, assistance, and mutual cooperation. For example, process and thread synchronization can be understood as A combination of process or thread A and thread B. When A executes to A certain extent, it depends on A result of B, so it stops and signals B to run; b. Execute the statement, and then send the result to A; A. Continue the operation. The so-called synchronization means that when a function call is sent, the call will not be returned until the result is obtained, and other threads cannot call this method. According to this definition, most functions are called synchronously (such as sin and isdigit ). But in general, when we talk about synchronization and Asynchronization, we are referring to the tasks that require the collaboration of other components or a certain amount of time. For example, the Window API function SendMessage. This function sends a message to a window. This function does not return a message before the recipient finishes processing the message. After the other party completes processing, the function returns the LRESULT value returned by the message processing function to the caller. Generally, creating a thread does not improve the execution efficiency of the program. Therefore, multiple threads must be created. However, when multiple threads run simultaneously, the thread function may be called to write data to the same memory address in multiple threads at the same time. Due to CPU time scheduling problems, writing data will be overwritten multiple times, so the thread must be synchronized. Thread Synchronization: When a thread is operating on the memory, other threads cannot operate on the memory address until the thread completes the operation, other threads can operate on the memory address, while other threads are waiting. Currently, there are many methods to implement thread synchronization, and the object in the critical section is one of them. In multi-thread programming, some sensitive data cannot be accessed by multiple threads at the same time. In this case, the synchronous access technology is used to ensure that data can be accessed by at most one thread at any time, to ensure data integrity. For example, if a bank account is consumed by two people, these two people cannot pay with the bank card at the same time, because the bank card balance is limited, you must confirm that the balance of the bank card is sufficient to pay for the next purchase after one consumption is completed. The balance in the account is equivalent to cpu resources, while the two consumption is equivalent to two threads. At this time, the two threads cannot use cpu resources at the same time. Thread synchronization operation method: synchronized program block synchronized Method: public synchronized run () {} That is, add the keyword synchronized before the run () method of the thread object, this method is atomic and cannot be executed concurrently by two threads. When an instance object of a thread executes this method, a synchronization lock will be applied to the current thread object, after executing the task, the lock is handed over to another thread that needs to access the resource.

 

When two threads use cpu resources at the same time, it may cause some trouble. At this time, a lock should be applied to the t1 thread, and a synchronization lock should be applied to the runnable object. Do not allow the t2 thread to run until the t1 thread completes its work.

The yield () method is executed but does not work.

Synchronized block:

 

The lock will be released by the thread holding the lock in the following cases:

The synchronization code block is executed;

An exception occurs during the execution of the synchronous code block, leading to thread termination;

The wait () method of the object to which the lock belongs is executed.

Note: neither the yield () method nor the sleep () method will release the lock.

 

 

 

 

 

 

 

 

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.