Java Learning Note Thread synchronization

Source: Internet
Author: User

After implementing the Runnable interface, you can invoke the same piece of code resources through multiple start methods, which also leads to the problem that resources may not be synchronized.

The workaround for resource synchronization is to allow only one thread to manipulate the object or method during the same time period, and other threads to wait until the end of the thread's access to continue execution and lock it.

Keyword synchronized: Indicates that only one thread can execute a code block or method declared by synchronized, and other methods cannot lock the object during execution. There are synchronous code blocks and synchronous methods, both of which function the same way.

Only one synchronous method can access the object, and the other method cannot access the object if it is synchronized, but the non-synchronous method has access to the object (silly smacking)

Synchronizing code blocks
 Public classTest { Public Static voidMain (string[] args) {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1")); Thread T2=NewThread (thread, "T2"));          T1.start ();    T2.start (); }}classMyThreadImplementsRunnable {Static intnum = 5;  Public voidrun () {//synchronized (this) {//indicates that only one thread can execute this block of code and lock the code block below                if(num > 0) {                     for(inti = 1;i < 6;i++) {num--; Try{Thread.Sleep (20);//acts as a magnifying effect}Catch(interruptedexception e) {System.out.println ("Thread hibernation Interrupted"); } System.out.println (Thread.CurrentThread (). GetName ()+ "Take away the remaining" +num); }                }        //}            }}

After using the synchronization code block:

The synchronous method further explores a

 Public classTest { Public Static voidMain (string[] args)throwsException {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1"));          T1.start (); Thread.Sleep (10);        THREAD.M2 (); }}classMyThreadImplementsRunnable {intnum = 1;  Public synchronized voidRun () {//only means that the lock method cannot be executed by another thread at the same time and cannot be locked by another thread.num = 1000; Try{Thread.Sleep (5000);            SYSTEM.OUT.PRINTLN (num); }Catch(interruptedexception e) {System.out.println ("Interrupted in hibernation"); }    }        voidm2 () {num= 2000;        SYSTEM.OUT.PRINTLN (num); }}

Results

Discussion two

 Public classTest { Public Static voidMain (string[] args)throwsException {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1"));          T1.start (); Thread.Sleep (10);        THREAD.M2 (); }}classMyThreadImplementsRunnable {intnum = 1;  Public synchronized voidRun () {//only means that the lock method cannot be executed by another thread at the same time and cannot be locked by another thread.num = 1000; Try{Thread.Sleep (5000);            SYSTEM.OUT.PRINTLN (num); }Catch(interruptedexception e) {System.out.println ("Interrupted in hibernation"); }    }        synchronized voidM2 () {//While other threads are running while the Run method is locked, the second lock on the object cannot benum = 2000;        SYSTEM.OUT.PRINTLN (num); }}

Results

Java Learning Note Thread synchronization

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.