The writer of the reader's writer is preferred (Java)

xiaoxiao2021-03-06  33

/ * * Created on 2005-1-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @Author michelangelo * * Todo to change Ty test go to * window - preferences - java - code style - code templates * / public class database {

/ ** * * / private static final int NAP_TIME = 5; private int readerCount; private int writerCount; private boolean dbReading; private boolean dbWriting; public Database () {super (); readerCount = 0; writerCount = 0; dbReading = false DBWriting = false; // Todo auto-generated constructor stub} public static void napping () {int sleeptime = (int); try {thread.sleep (SleepTime * 1000);} catch (EXCEPTION E) {E.PrintStackTrace ();}} public synchronized int startread () {while (WriterCount> 0) {Try {system.out.println ("Reader is Waiting"); Wait ();} catch (Exception e) {system.out.println (E.TOSTRING ()); E.PrintStackTrace ();}} readercount; if (readercount == 1) {dbreading = true;} Return ReaderCount;} public synchronized int endue ) {--readerCount; if (readerCount == 0) {dbReading = false;} notifyAll (); System.out.println ( "one reader is done reading Count =." readerCount); return readerCount;} public synchronized void Star TWRITING () { WriterCount; while (dbreading == true || dbwriting == true) {try {system.out.println ("Writer is Waiting"); Wait ();} catch (exception e) {system. Out.println (e.tostring ());}} dbwriting = true;} public synchronized void endwriting () {--writercount; dbwriting = false; system.out.println ("One Writer IS Done Writing. count =" Writercount); notifyall ();}} / * * Created on 2005-1-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author Michelangelo * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class Reader extends Thread {/ ** * * / private Database server PRIVATE INTRENUM; Public Reader (INT R, Database DB) {Super (); ReaderNum = R; Server = DB; // Todo Auto-generated constructor stub} public void run () {Int C; while (true) { System.out.Println ("Reader" Readernum "Is Sleeping"); Database.napping (); System.out.Println ("Reader" Readernum "Wants to read"); c = server.StartRead (); system .out.println ("Reader" Readernum "Is Reading. Count =" C); Database.napping (); C = Server.EndReading (); System.Out.println ("IT IS Reader Readernum " WHO HAS DONE Reading According to count = " c);}}

}

/ * * Created on 2005-1-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author Michelangelo * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class Writer extends Thread {private Database server; private int writerNum; / ** * * / public Writer (int W, Database DB) {super (); Writernum = W; server = db; // Todo auto-generated constructor stub} public void run () {while (true) {system.out .println ("Writer" Writernum "Is Sleeping"); Database.napping (); System.out.Println ("Writer" Writernum "Wants to WRITE"); Server.StartWriting (); System.out.Println ( "Writer" Writernum "IS Writing"); database.napping (); server.Endwriting (); system.out.println ("IT IS Writer" Writernum "WHO HAS DONE WRITING.");}}}

/ * * Created on 2005-1-9 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @Author michelangelo * * Todo to change the template for this generated type comment go to * window - preferences - java - code style - code templates * / public class databaseserver {

/ ** * * / public databaseserver () {super (); // Todo auto-generated constructor stub}

Public static void main (string [] args) {database db = new database (); reader r1 = new reader (1, db); reader r2 = new reader (2, db); Reader R3 = New Reader (3, DB) Reader R4 = New Reader (4, DB); Writer W1 = New Writer (1, DB); Writer W2 = New Writer (2, DB); R1.Start (); r2.start (); r3.Start (); W1.start (); r4.Start (); w2.start ();}}

转载请注明原文地址:https://www.9cbs.com/read-62558.html

New Post(0)