Second-kill multithreading the 14th reader-writer problem following read-write lock Srwlock (cont.)

Source: Internet
Author: User

The Java package implements a read-write lock operation:

Package Com.multithread.readwritelock;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executor;import Java.util.concurrent.executors;import Java.util.concurrent.locks.Lock; Import Java.util.concurrent.locks.readwritelock;import Java.util.concurrent.locks.reentrantreadwritelock;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.IOException;import Com.multithread.main.exampleinterface;import Com.multithread.readwrite.WriteThread; Public classReadwritelockexample extends Exampleinterface { PublicReadwritelock Mreadwritelock =NewReentrantreadwritelock (false);  PublicFile Mfile =NULL;  Public intMcAsh =10000;  PublicCountdownlatch Mlatchdown =NewCountdownlatch (6); PrivateBoolean Mstopedflag =false; @Override Public voidStartdemo () {//TODO auto-generated Method StubMfile =NewFile ("H:\\project\\sst\\123.txt"); Try{mfile.createnewfile (); Executor Mececutor= Executors.newfixedthreadpool (2+4); Mececutor.execute (NewReader ("Reader1")); Mececutor.execute (NewReader ("Reader2")); Mececutor.execute (NewReader ("Reader3")); Mececutor.execute (NewReader ("Reader4")); Mececutor.execute (NewWriter ("Writer1", -)); Mececutor.execute (NewWriter ("Writer2", - the)); Mlatchdown.await(); System. out. println ("[Startdemo]"+"Demo Down"); } Catch(IOException |interruptedexception e) {            //TODO auto-generated Catch blockE.printstacktrace (); }    }     Public classReader extends Thread { PublicString name =NULL; Boolean flag=true; Private intindex =0;  PublicReader (String name) { This. Name =name; } @Override Public voidrun () { while(flag) {Try{Mreadwritelock.readlock ().Lock(); System. out. println ("[Reader]"+ name +"Start"); Thread.Sleep ((Long) (Math.random () * -)); if(!Mstopedflag) {System. out. println ("[Reader]"+ name +"get McAsh Now:"+McAsh); } Else{flag=false; } thread.sleep ((Long) (Math.random () * -)); System. out. println ("[Reader]"+ name +" Down");                    Mreadwritelock.readlock (). Unlock (); Thread.Sleep ((Long) (Math.random () * +)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();        }} mlatchdown.countdown (); }    }     Public classWriter extends Thread { PublicString name =NULL; Boolean flag=true; Private intindex =0; Private intCash =0;  PublicWriter (String name,intAddcash) {             This. Name =name;  This. Cash =Addcash; } @Override Public voidrun () {System. out. println ("[Writer]"+ name +"Start");  while(flag) {Try{Mreadwritelock.writelock ().Lock(); System. out. println ("[Writer]"+ name +"Start"); Thread.Sleep ((Long) (Math.random () * -)); intOldcash =McAsh; if(McAsh <=0) {flag=false; Mstopedflag=true; } Else{McAsh+=cash; System. out. println ("[Writer]"+name+"operator cash old:"+ Oldcash +"To :"+McAsh); } thread.sleep ((Long) (Math.random () * -)); System. out. println ("[Writer]"+ name +" Down");                    Mreadwritelock.writelock (). Unlock (); Thread.Sleep ((Long) (Math.random () * +)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();        }} mlatchdown.countdown (); }    }}

The features of the read-write lock are:

1. The writer is mutually exclusive with the reader

2. Each writer is mutually exclusive

3. The reader can simultaneously

Second-kill multithreading the 14th reader-writer problem following read-write lock Srwlock (cont.)

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.