Java producer consumer issues.

Source: Internet
Author: User
Tags date now

 

Producer, consumer, fixed-length buffer, and thread can be interrupted externally

Import java. util. arrays;
Import java. util. date;
Import java. util. Collections list;
Import javax. Swing .*;

Public class producerconsumer {

Public static jframe inst;

Public static void main (string [] ARGs ){

Bufferlock buffer = new bufferlock ();
Controlcondition indexcontrol = new controlcondition ();

(New indextimedialogthread (indexcontrol). Start ();
(New producer (buffer, indexcontrol, "extract"). Start ();
(New consumer (buffer, indexcontrol, "Index"). Start ();
}

}

// This class is the warehouse, which is a synchronous resource for the producer and consumer to compete for control.
Class bufferlock {
Public Boolean isproduceend;

Public int bufferlength;

Shortlist <string> List = new shortlist <string> ();

Bufferlock (){
Bufferlength = 10;
}

// Place the production product in the buffer list
Public synchronized void push (string product ){
While (list. Size () = bufferlength) {// warehouse full
Try {
Wait (); // wait
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
// Warehouse dissatisfaction
Policyall ();
List. Add (product );
System. Out. println ("extracted:" + product );
System. Out. println (arrays. tostring (list. toarray ()));
}

// Consume the product and remove it from the buffer list
Public synchronized void POP (){
While (list. Size () = 0) {// The repository is empty.
Try {
System. Out. println ("no index resource, waiting for extraction ");
Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
Policyall ();
String Product = List. removefirst ();
System. Out. println ("indexed:" + product );
System. Out. println (arrays. tostring (list. toarray ()));
}
}

Class producer extends thread {
Bufferlock buffer = NULL;

String name;

Controlcondition indexcontrol;

Producer (bufferlock buffer, controlcondition indexcontrol, string name ){
This. Buffer = buffer;
This. indexcontrol = indexcontrol;
This. Name = Name;
}

Public void run (){

For (INT I = 0; I <200; I ++ ){
If (indexcontrol. isinterrupt = false) {// No external interruption
Buffer. Push ("file" + I );
If (I = 199) {// extraction ends
System. Out. println ("extraction ended ");
Indexcontrol. isextractend = true;
}
} Else {
System. Out. println ("extraction interrupt ");
Break;
}
}
}
}

Class consumer extends thread {
Bufferlock buffer = NULL;

Controlcondition indexcontrol;

String name;

Consumer (bufferlock buffer, controlcondition indexcontrol, string name ){
This. indexcontrol = indexcontrol;
This. Buffer = buffer;
This. Name = Name;
}

Public void run (){
While (true ){
If (indexcontrol. isinterrupt = false) {// No external interruption
If (indexcontrol. isextractend = true & buffer. List. Size () = 0)
{// The scan is complete and the buffer is null, which is the index end condition.
System. Out. println ("index ended ");
Indexcontrol. isindexend = true;
Break;
} Else
Buffer. Pop ();
} Else {
System. Out. println ("index interrupted ");
Break;
}
}
}
}

Class controlcondition {
Boolean isextractend = false;

Boolean isindexend = false;

Boolean isinterrupt = false;
}

/*
* Display the index consumption time dialog box thread
*/
Class indextimedialogthread extends thread {
Showdialog DLG;

Controlcondition indexcontrol;

Public indextimedialogthread (controlcondition indexcontrol ){
This. indexcontrol = indexcontrol;
DLG = new showdialog (null, indexcontrol, "Create Index", false ,"");
DLG. jbutok. setenabled (false );
DLG. setvisible (true );
}

Public void run (){
Date starttime = new date ();
While (true ){
Date Now = new date ();
DLG. jlabelshow. settext ("<HTML> <body> creating index... timing: <font color = Red>"
+ (Now. gettime ()-starttime. gettime ())
+ "</Font> millisecond </body> If (indexcontrol. isindexend = true) {// indexthread complete
Date endtime = new date ();
DLG. jlabelshow. settext ("<HTML> <body> index completed! Total time consumption <font color = Red>"
+ (Endtime. gettime ()-starttime. gettime ())
+ "</Font> millisecond." + "<br> View index logs in the index menu"
+ "</Body> DLG. jbutok. setenabled (true );
DLG. jbutstopindexthread. setenabled (false );
Break;
}

If (indexcontrol. isinterrupt = true) {// indexthread interrupt
Date endtime = new date ();
DLG. jlabelshow. settext ("<HTML> <body> index interrupted! Total time consumption <font color = Red>"
+ (Endtime. gettime ()-starttime. gettime ())
+ "</Font> millisecond." + "<br> View index logs in the index menu"
+ "</Body> DLG. jbutok. setenabled (true );
DLG. jbutstopindexthread. setenabled (false );
Break;
}
}
}
}

Related Article

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.