Java applet for simulating system process algorithms

Source: Internet
Author: User

Package PID;

Public class probean {

Private string PID;
Private int time;
Private int priority;
Public String getpid (){
Return PID;
}
Public void setpid (string PID ){
PID = PID;
}
Public int gettime (){
Return time;
}
Public void settime (INT time ){
This. Time = time;
}
Public int getpriority (){
Return priority;
}
Public void setpriority (INT priority ){
This. Priority = priority;
}
 
}

Package PID;

Import java. util. arraylist;
Import java. util. List;

Public class prolist {

List <probean> List = new arraylist <probean> ();

Public Boolean ISE (string PID ){
For (INT I = 0; I <list. Size (); I ++ ){
If (pid. Equals (list. Get (I). getpid ())){
Return true;
}
}
Return false;

}


}

Package PID;

Import java. util. List;
Import java. util. collections;

Public class protest {
Private Static pipeline Input = new pipeline (system. In );
Private Static prolist PL = new prolist ();
// Private Static probean Pb = new probean (); New only one global object

Public static void main (string [] ARGs ){
Newpro ();

}
Public static void newpro (){
System. Out. println ("Enter the number of processes :");
Int pnum = input. nextint ();
For (INT I = 0; I <pnum; I ++ ){
System. Out. println ("Enter the PID of the process :");
String pid = input. Next ();
Boolean isexist = pl. ISE (PID );
If (! Isexist ){
System. Out. println ("Enter the running time of the process (seconds ):");
Int time = input. nextint ();
System. Out. println ("Enter the priority of the process :");
Int priority = input. nextint ();
Probean Pb = new probean ();
PB. setpid (PID );
PB. settime (time );
PB. setpriority (priority );

List <probean> PL1 = pl. List;
Pl1.add (PB );
} Else {
System. Out. println ("the PID process already exists! ");
}

}

System. Out. println ("------------------------");
System. Out. println ("select algorithm :");
System. Out. println ("------------------------");
System. Out. println ("1. Advanced service algorithm ");
System. Out. println ("2. Priority Algorithm ");
Int num1 = input. nextint ();
Switch (num1 ){

Case 1:
List <probean> List = pl. List;
For (Int J = 0; j <list. Size (); j ++ ){
Probean Pb = list. Get (j );
String pname = Pb. getpid ();
Mythread T = new mythread (pname );
T. Start ();

Try {
T. Sleep (PB. gettime () * 1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
System. Out. println (PB. gettime () + "seconds later ends..." + "\ t ");
T. Stop ();
If (! T. isalive ()){
System. Out. println ("process ended ");
}
}
/* List <probean> List = pl. List;
Probean PB1 = list. Get (0 );
String pt1 = pb1.getpid ();
Mythread T1 = new mythread (pt1 );
T1.start ();
System. Out. println ();
Try {
T1.sleep (pb1.gettime () * 1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
System. Out. Print (pb1.gettime () + "seconds later ends..." + "\ t ");
T1.stop ();
System. Out. println ("process ended ");*/

/*
Probean PBS = list. Get (1 );
String pt2 = pb2.getpid ();
Mythread t2 = new mythread (pt2 );
T2.start ();
Try {
T2.sleep (pb2.gettime () * 1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}*/
Break;
Case 2:
Int temp = 0;
Int time = 0;
List <probean> list2 = pl. List;
For (INT I = 0; I <list2.size ()-1; I ++ ){
For (Int J = 0; j <list2.size ()-i-1; j ++ ){

If (list2.get (j). getpriority () <list2.get (J + 1). getpriority ()){

Temp = list2.get (J + 1). getpriority ();
String ptemp = list2.get (J + 1). getpid ();
Time = list2.get (J + 1). gettime ();

List2.get (J + 1). setpriority (list2.get (j). getpriority ());
List2.get (j). setpriority (temp );

List2.get (J + 1). setpid (list2.get (j). getpid ());
List2.get (j). setpid (ptemp );

List2.get (J + 1). settime (list2.get (j). gettime ());
List2.get (j). settime (time );
}
}
}


For (INT I = 0; I <list2.size (); I ++ ){
// Int pri = list2.get (I). getpriority ();

Probean Pb = list2.get (I );
String pname = Pb. getpid ();
Mythread T = new mythread (pname );
T. Start ();

Try {
T. Sleep (PB. gettime () * 1000 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
System. Out. println (PB. gettime () + "seconds later ends..." + "\ t ");
T. Stop ();
If (! T. isalive ()){
System. Out. println ("process ended ");
}
}

Break;
}
}
 
Static class mythread extends thread {
Boolean run = true;
Mythread (string s ){
Super (s );
}
Public void run (){

System. Out. Print ("PID:" + getname () + "\ t" + "running ...");
}

}
 
}

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.