Several methods of Thread Synchronization Control and thread synchronization

Source: Internet
Author: User

Several methods of Thread Synchronization Control and thread synchronization

When multiple threads call the same function, if we do not control the number lock mechanism, data is often disordered. When we usually control the synchronization of threads, common Methods:

1: Use synchronized to control access.

2: Use ReentrantLock to lock and unlock the method body at the end.

Next, I will use an instance to describe the locking method in detail. If the locking mechanism is removed, you will find that the data will be in disorder.

Import java. util. concurrent. locks. lock; import java. util. concurrent. locks. reentrantLock; public class TraditionalThreadSynchronized {public static void main (String [] args) {TraditionalThreadSynchronized tt = new TraditionalThreadSynchronized (); tt. init ();} public void init () {final Outeputer out = new Outeputer (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {try {Thread. sleep (100);} catch (InterruptedException e) {e. printStackTrace ();} out. output2 ("22222222222222222 ");}}}). start (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {try {Thread. sleep (100);} catch (InterruptedException e) {e. printStackTrace ();} out. output2 ("tcent1111111 ");}}}). start ();} static class Outeputer {Lock lock = new ReentrantLock (); public void output (String name) {synchronized (Outeputer. class) {int len = name. length (); for (int I = 0; I <len; I ++) {System. out. print (name. charAt (I);} System. out. println () ;}} public void output2 (String name) {// lock. lock (); // test int len = name here. length (); for (int I = 0; I <len; I ++) {System. out. print (name. charAt (I);} System. out. println (); // lock. unlock ();} public void output3 (String name) {int len = name. length (); for (int I = 0; I <len; I ++) {System. out. print (name. charAt (I);} System. out. println ();}}}


How can I synchronize threads?

Wait (): puts a thread in the waiting state and releases the lock of the object it holds.

Sleep (): It is a static method that calls a running thread to capture InterruptedException exceptions.

Y (): Wake up a thread in the waiting state. Note that when this method is called, it cannot actually wake up a thread in the waiting state, instead, the JVM determines which thread to wake up, not by priority.

Allnotity (): Wake up all threads in the wait state. Note that it is not a lock for all wake-up threads but a lock for them to compete.

Why thread synchronization and several methods of thread synchronization?

Threads may share resources with other threads, such as memory, files, and databases.
Conflicts may occur when multiple threads read and write the same shared resource at the same time. At this time, we need to introduce the thread "synchronization" mechanism, that is, there must be a first-in-one between all threads, and we cannot rush together for a group.
The real meaning of thread synchronization is the opposite to that of the word surface. The real meaning of thread synchronization is actually "Queuing": Several threads need to queue up to operate the shared resources one by one, rather than simultaneously.

Thread Synchronization Method
(1) wait (): puts a thread in the waiting state and releases the lock of the object it holds.
(2) sleep (): It is a static method to make a running thread sleep and call this method to capture
InterruptedException exception.
(3) y (): Wake up a thread in the waiting state. Note that this method cannot be called accurately.
To wake up a thread in the waiting state, the JVM determines which thread to wake up, not by priority.
(4) notityAll (): Wake up all threads in the waiting state. Note that it is not a lock for all wake-up threads for an object,
Instead, let them compete

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.