Writer's problem first (Java)

Source: Internet
Author: User

/*
* Created on 2005-
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/

/**
* @ Author mirangelo
*
* Todo to change the template for this generated type comment go
* 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) (nap_time * Math. Random ());
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 endreading (){
-- Readercount;
If (readercount = 0 ){
Dbreading = false;
}
Policyall ();
System. Out. println ("One reader is done reading. Count =" + readercount );
Return readercount;
}
 
Public synchronized void startwriting (){
++ 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 );

Policyall ();
}
 
 
 
}

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

/**
* @ Author mirangelo
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class reader extends thread {

/**
*
*/
Private Database server;
Private int readernum;
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
* Window-preferences-Java-code style-code templates
*/

/**
* @ Author mirangelo
*
* Todo to change the template for this generated type comment go
* 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
* Window-preferences-Java-code style-code templates
*/

/**
* @ Author mirangelo
*
* Todo to change the template for this generated type comment go
* 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 ();

}
}

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.