Understanding multithreading from scratch-2.14 resolving dirty reads caused by synchronized synchronized (NewObject ())

Source: Internet
Author: User

In this chapter we discuss the method of resolving dirty reads caused by synchronized synchronized (NewObject ()).

1. Causes of the above dirty reads: Multi-threaded multiple methods to access and modify a domain at the same time


2. Solution Ideas

Along the above reasons, I propose two methods that are not methods

(1) Only single-threaded access to and modification of an attribute domain so that the order of execution (that is, the steps that require synchronization of all accesses and modifications) is guaranteed (disadvantage: poor performance, advantages: Strong data consistency)

(2) In the case of multi-threaded multiple methods, only access to multiple domains, not be able to access and modify a single domain (the fundamental purpose is to separate operations, but in the production environment, it is really difficult to divide a domain into 2 to operate, so the author is more inclined to the first method)


3. Code Listing

(1) The first line of thought

Package com.ray.deepintothread.ch02.topic_15;/** * * @author Raylee * */public class Solutionofdirtyread {public static V OID Main (string[] args) throws Interruptedexception {MyService2 MyService = new MyService2 (); Threadthree threadthree = new Threadthree (MyService); Thread thread = new Thread (threadthree); Thread.Start (); Threadfour threadfour = new Threadfour (MyService); Thread thread2 = new Thread (threadfour); Thread2.start ();}} Class Threadthree implements Runnable {private MyService2 myservice;public threadthree (MyService2 myservice) { This.myservice = MyService;} @Overridepublic void Run () {try {myservice.update ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadfour implements Runnable {private MyService2 myservice;public threadfour (MyService2 myservice) { This.myservice = MyService;} @Overridepublic void Run () {try {myservice.update ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class MyService2 {private Integer id = 0;public void UpdateA () throws InterruPtedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). GetName () + " "+ id++); Thread.Sleep (50);}} public void Updateb () throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (T Hread.currentthread (). GetName () + "" + id++); Thread.Sleep (100);}} Public synchronized void Update () throws Interruptedexception {UpdateA (); Updateb ();}}

Output:

Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
Thread-0 8
Thread-0 9
Thread-1 10
Thread-1 11
Thread-1 12
Thread-1 13
Thread-1 14
Thread-1 15
Thread-1 16
Thread-1 17
Thread-1 18
Thread-1 19


We combine UpdateA and Updateb as a method, then do method-level synchronization, or synchronize two method groups to synthesize method blocks so that the output can be synchronous without dirty reads


(2) The second way of thinking

Package com.ray.deepintothread.ch02.topic_15;/** * * @author Raylee * */public class SolutionOfDirtyRead2 {public static void Main (string[] args) throws Interruptedexception {MyService myservice = new MyService (); Threadone Threadone = new Threadone (MyService); Thread thread = new Thread (threadone); Thread.Start (); Threadtwo threadtwo = new Threadtwo (MyService); Thread thread2 = new Thread (threadtwo); Thread2.start ();}} Class Threadone implements Runnable {private MyService myservice;public threadone (MyService myservice) {This.myservice = MyService;} @Overridepublic void Run () {try {Myservice.updatea ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadtwo implements Runnable {private MyService myservice;public threadtwo (MyService myservice) {This.myservice = MyService;} @Overridepublic void Run () {try {myservice.updateb ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class MyService {private Integer id1 = 0;private integer id2 = 0;public void UpdateA () throws Interruptedexception {synchronized (this) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). GetName () + "ID1:" + id1++); Thread.Sleep (50);}} public void Updateb () throws Interruptedexception {synchronized (this) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). GetName () + "ID2:" + id2++); Thread.Sleep (100);}}}

Output:

Thread-0 id1:0
Thread-0 id1:1
Thread-0 Id1:2
Thread-0 Id1:3
Thread-0 Id1:4
Thread-1 id2:0
Thread-1 id2:1
Thread-1 Id2:2
Thread-1 Id2:3
Thread-1 Id2:4


Although the result of the output is synchronous, but it is also mentioned above, a domain (usually a number of operations or a certain state of operation) is difficult to separate. Of course, if it can be separated, this is the optimal solution, because both the synchronization and ensure the strong consistency of the data.


Summary: This chapter focuses on the solution of dirty reads caused by synchronized synchronized (NewObject ()).


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.14 resolving dirty reads caused by synchronized synchronized (NewObject ())

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.