Learn from the beginning multithreading-2.8 Mitigating the pitfalls of synchronous methods-synchronizing code blocks

Source: Internet
Author: User

In this chapter we discuss the pitfalls of mitigating synchronization methods-synchronizing code blocks.

1. Thinking: Synchronize the method, reduce the granularity of synchronization, synchronization to the code block


2. According to the example in the previous section, we modified the code

(1) The first method, the synchronization tag to move to the updated column, in general, most of the updates need to synchronize the data

Package com.ray.deepintothread.ch02.topic_9;/** * Learn more about multithreading-2.8 Mitigating the pitfalls of synchronization methods-synchronizing code blocks <br> * * @author Raylee * */public CLA SS Reliefthreatofsynch {public static void main (string[] args) throws Interruptedexception {MyService MyService = new Myse Rvice (); Threadone Threadone = new Threadone (MyService); Thread thread = new Thread (threadone); Thread.Start (); Threadtwo threadtwo = new Threadtwo (MyService); Thread thread2 = new Thread (threadtwo); Thread2.start (); Thread.Sleep (10000); SYSTEM.OUT.PRINTLN ("Application Use Time:" + (Mytimeutil.end_time-mytimeutil.start_time));}} Class Threadone implements Runnable {private MyService myservice;public threadone (MyService myservice) {This.myservice = MyService;} @Overridepublic void Run () {Myservice.service ();}} Class Threadtwo implements Runnable {private MyService myservice;public threadtwo (MyService myservice) {This.myservice = MyService;} @Overridepublic void Run () {Myservice.service ();}} Class MyService {private void Querydatafromserver () {try {Thread.sleEP (1000);} catch (Interruptedexception e) {e.printstacktrace ();}}  /** * Increase synchronization mechanism */private synchronized void Updatedatafromserver () {try {Thread.Sleep (+)} catch (Interruptedexception e) {E.printstacktrace ();}} private void Retrundatafromserver () {try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ()}} public void Service () {Long startTime = System.currenttimemillis (); if (mytimeutil.start_time = = 0) {Mytimeutil.start_ Time = StartTime;} Querydatafromserver (); Updatedatafromserver (); Retrundatafromserver (); Long endTime = System.currenttimemillis (); if ( EndTime > Mytimeutil.end_time) {mytimeutil.end_time = EndTime;} System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "User time:" + (Endtime-starttime));}} Class Mytimeutil {public static long start_time = 0;public static Long end_time = 0;}

Output:

Thread name:thread-0 User time:3000
Thread name:thread-1 User time:4000
Application Use time:4000


(2) The second method, call the method directly when the synchronization flag is added

Package com.ray.deepintothread.ch02.topic_9;/** * Learn more about multithreading-2.8 Mitigating the pitfalls of synchronization methods-synchronizing code blocks <br> * * @author Raylee * */public CLA SS ReliefThreatOfSynch2 {public static void main (string[] args) throws Interruptedexception {MyService2 MyService = new My Service2 (); Threadthree threadthree = new Threadthree (MyService); Thread thread = new Thread (threadthree); Thread.Start (); Threadfour threadfour = new Threadfour (MyService); Thread thread2 = new Thread (threadfour); Thread2.start (); Thread.Sleep (10000); SYSTEM.OUT.PRINTLN ("Application Use Time:" + (Mytimeutil2.end_time-mytimeutil2.start_time));}} Class Threadthree implements Runnable {private MyService2 myservice;public threadthree (MyService2 myservice) { This.myservice = MyService;} @Overridepublic void Run () {Myservice.service ();}} Class Threadfour implements Runnable {private MyService2 myservice;public threadfour (MyService2 myservice) { This.myservice = MyService;} @Overridepublic void Run () {Myservice.service ();}} Class MyService2 {private void QuerydatafRomserver () {try {thread.sleep ()} catch (Interruptedexception e) {e.printstacktrace ()}} private void Updatedatafromserver () {try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ()}} private void Retrundatafromserver () {try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ()}} public void Service () {Long startTime = System.currenttimemillis (); if (mytimeutil2.start_time = = 0) {Mytimeutil2.start_ Time = StartTime;} Querydatafromserver (); synchronized (this) {Updatedatafromserver ();} Retrundatafromserver (); Long endTime = System.currenttimemillis (); if (EndTime > Mytimeutil2.end_time) { Mytimeutil2.end_time = EndTime;} System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "User time:" + (Endtime-starttime));}} Class MyTimeUtil2 {public static long start_time = 0;public static Long end_time = 0;}

Output:

Thread name:thread-0 User time:3002
Thread name:thread-1 User time:4002
Application Use time:4002


The result is similar to the one above, which is basically considered the same.


3. Thinking

Although the above method can alleviate the problem of synchronization method, but has not been fundamentally resolved, but also temporarily impossible to solve, so in addition to the above method, we also have:

(1) Reduce business complexity, from the business perspective to optimize (first out of the technology perspective)

(2) Enhanced hardware settings (out of the software perspective)

(3) Increase the network (jump out of the software angle)

(4) Optimized code


Summary: This chapter discusses the pitfalls of using synchronous code blocks to mitigate synchronization methods.


This chapter is here, thank you.

------------------------------------------------------------------------------------

My github:https://github.com/raylee2015/deepintothread.


Catalog: http://blog.csdn.net/raylee2007/article/details/51204573


Understanding multithreading from scratch-2.8 Mitigating the pitfalls of synchronization methods-synchronizing code blocks

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.