Start five threads at the same time, display count in the respective text box, and control the auto-increment of count with the button
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Applet .*;
Class ticker extends thread {
Private button T = new button ("toggle ");
Private textfield TF = new textfield (10 );
// Switch to control the count change
Private runflag = true;
Private int COUNT = 0;
Class stop implements actionlistener {
@ Override
Public void actionreceivmed (actionevent e ){
Runflag =! Runflag;
}
}
Public ticker (container c ){
T. addactionlistener (new stop ());
// Panel container
Panel P = new Panel ();
P. Add (t );
P. Add (TF );
C. Add (P );
}
@ Override
Public void run (){
While (true ){
Try (
Thread. currentthread (). Sleep (200 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
If (runflag)
TF. settext (integer. tostring (++ count ));
}
}
}
Public class counter extends applet {
Private button start = new button ("START ");
Private Boolean started = false;
Private int size = 0;
Private ticker [] ts;
@ Override
Public void Init (){
Start. addactionlistener (New Start ());
Add (start );
TS = new ticker [size];
For (INT I = 0; I <size; I ++ ){
TS [I] = new ticker (counter. This );
}
}
Class start implements actionlistener {
@ Override
Public void actionreceivmed (actionevent e ){
If (! Started ){
Started = true;
For (INT I = 0; I <size; I ++ ){
TS [I]. Start ();
}
}
}
}
Public static void main (string [] ARGs ){
Counter C = new counter ();
Frame frame = new frame ("program piece ");
Frame. addwindowlistener (
New windowadapter (){
@ Override
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}
);
Frame. setsize (300, C. Size * 50 );
Frame. Add (C, borderlayout. center );
C. INIT ();
C. Start ();
Frame. setvisible (true );
}
}
/** -- Note --**/
The above code is written in the text editor and may be omitted.