Synchronization and asynchronous of Java thread objects

Source: Internet
Author: User

one, synchronous and asynchronous in multi-threaded environment

Sync: A thread wants to request a resource, but this resource is being used by the B thread, because the synchronization mechanism exists, a thread cannot request it, and a thread can only wait.

 Packagecom.jalja.org.thread.demo01; Public classTHREAD02 { Public synchronized voidmethod1 () {System.out.println ("Method1:" +Thread.CurrentThread (). GetName ()); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); }    }    Public synchronized voidmethod2 () {System.out.println ("METHOD2:" +Thread.CurrentThread (). GetName ()); }     Public Static voidMain (string[] args) {FinalTHREAD02nd=NewThread02 (); Thread Thread1=NewThread (NewRunnable () { Public voidrun () {th.method1 (); }        },"Th1"); Thread thread2=NewThread (NewRunnable () { Public voidrun () {th.method2 (); }        },"Th2");        Thread1.start ();    Thread2.start (); }}

Observe the output: the method1:th1 method2:th2 output after 3 seconds, because Method2 () and method1 () are synchronous methods, and the thread thread1 is the same object th as the thread2 operation. Therefore, when Thread2 executes the METHOD2 () method, it needs to obtain a lock on the th object first.

Async: A thread wants to request a resource, but this resource is being used by the B thread, because there is no synchronization mechanism, and a thread is still requested, a thread does not have to wait.

 Packagecom.jalja.org.thread.demo01; Public classTHREAD02 { Public synchronized voidmethod1 () {System.out.println ("Method1:" +Thread.CurrentThread (). GetName ()); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); }    }     Public voidmethod2 () {System.out.println ("METHOD2:" +Thread.CurrentThread (). GetName ()); }     Public Static voidMain (string[] args) {FinalTHREAD02nd=NewThread02 (); Thread Thread1=NewThread (NewRunnable () { Public voidrun () {th.method1 (); }        },"Th1"); Thread thread2=NewThread (NewRunnable () { Public voidrun () {th.method2 (); }        },"Th2");        Thread1.start ();    Thread2.start (); }}

Observe the output: Method1:th1 and method2:th2 output simultaneously, this is because METHOD2 does not have synchronization control, so the thread thread2 do not have to get EXECUTE permission (object lock) when executing the METHOD2 () method.

Second, dirty reading of data

We must consider the integrity of the business when designing the business, or there will be data consistency issues.

 Packagecom.jalja.org.thread.demo01; Public classTHREAD03 {PrivateString name= "ZS"; PrivateString passworrd= "123"; Public synchronized voidsetValue (String name,string passWord) { This. name=name; Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }         This. passworrd=PassWord; System.out.println ("Set:name=" + This. Name + "passworrd=" + This. passworrd); }     Public voidGetValue () {System.out.println ("Get:name=" + This. Name + "passworrd=" + This. passworrd); }     Public Static voidMain (string[] args)throwsinterruptedexception {FinalTHREAD03rd=NewThread03 (); Thread Thread=NewThread (NewRunnable () { Public voidrun () {Th.setvalue ("LS", "456");        }        });        Thread.Start (); Thread.Sleep (100);    Th.getvalue (); }}

Results: Get:name=ls passworrd=123 set:name=ls passworrd=456 The result shows that the data get is obviously problematic, because the thread thread is executing the Get method when it is set. To avoid this situation, we have to make sure that when the thread is manipulating the data of the same object, other threads will also be manipulating the object's data. In this case we add the Synchronized keyword to the Get method.

Synchronization and asynchronous of Java thread objects

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.