A good memory is better than a bad pen. 75-multithreading-resource competition and examples under parallel access

Source: Internet
Author: User
Tags repetition

In the real business common, a lot of time we need to access some common resources, such as a serial number, such as a certain file. If multiple threads access this sequence or file together, and we do not do enough processing, it is very easy to create a variety of problems such as dirty data or data loss.
This scenario is particularly common and writes a simple example. Lest their team in the actual development, make this small error.
Of course, this kind of error knows, it is quite simple to deal with it beforehand, but if you want to make a mistake and find the hole in a lot of code, it's pretty deadly.

Scenario of the sample

We need to get a serial number for our business. The following is an example of a singleton pattern that obtains a serial number, but when a get () is called, it may produce two identical serial numbers:
When the code (1) and (2) both try to call get () to get a unique sequence. When the code (1) finishes executing code (a) and is about to execute code (b), it is interrupted and begins executing code (2). Once the code (2) finishes executing (a) and the Code (1) has not executed the code (b), then the Code (1) and the Code (2) will get the same value.

Java source code for resource competition under simple multi-threaded parallel access
 PackageCom.thread;/** * Simple multi-threaded to get serial number, theoretically can appear serial number repetition, but to observe, may need to experiment and careful observation * tip: We have 30 cycles here, if there is no 30 in the printed result, then there may be a repetition * @author Fan Fangming * * Public  class easygetseq extends Thread {    Private Static intNumber =0;Private StaticEasygetseq seq =NewEasygetseq (); Public Easygetseq(String name) {Super(name); }Private Easygetseq() {    } PublicEasygetseqgetinstance() {returnSeq } Public  int Get() {number++;//(a)        returnNumber//(b)} Public void Run(){ for(inti =0; I <Ten; i + +) {intA = getinstance (). get ();//(1)System.out.println (Thread.CurrentThread (). GetName () +":"+ a);//Analog access Pause            Try{Thread.Sleep (Ten); }Catch(Exception e)            {E.printstacktrace (); }        }       } Public Static void Main(string[] args) {//Simple analog multithreading callEasygetseq SepA =NewEasygetseq ("A");        Sepa.start (); Easygetseq SEPB =NewEasygetseq ("---B");        Sepb.start (); Easygetseq SepC =NewEasygetseq ("======c");    Sepc.start (); }}

Run Results
A:1
======c:3
-b:2
-b:5
======c:4
A:4
======c:7
-b:6
A:6
-b:9
A:8
======c:10
======c:12
-b:13
A:11
======c:14
A:16
-b:15
-b:18
======c:19
A:17
======c:20
-b:22
A:21
-b:23
======c:25
A:24
-b:26
A:27
======c:26

The trick of observation, because we run the number of times is 3*10, so the final data should be 30, but there is no 30 and 29, then there must be some number of repetitions, such as
-b:26
A:27
======c:26

A good memory is better than a bad pen. 75-multithreading-resource competition and examples under parallel access

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.