Java multi-threaded practice doctor

| - demand explanation

| - realization of ideas

Creating a thread class that implements special needs patients, the highest priority level, the main thread method named ordinary patients, as detailed code comments

 

| - Content Code

. 1  Package cn.doctor;
 2  
. 3  / ** 
. 4  * 9527 :: @auther
 . 5  * @Description: Dr
 . 6  * @program: shi_yong
 . 7  * @Create: 2019-08-05 20:12
 . 8   * / 
. 9  public  class Doctor {
 10      public  static  void main (String [] args) throws InterruptedException {
 . 11          // by parameter passing constructor, watching a particular patient 300 seconds, a total of 10 specific patient 
12 is          SeeDoctor SD = new new SeeDoctor (300, 10 );
 13 is          / / to thread class named special needs patients 
14         T = the Thread new new the Thread (SD, "special needs patient" );
 15          // main thread named ordinary patients 
16          Thread.currentThread () the setName ( "normal patients." );
 17          // start threads particular patient 
18 is          T .start ();
 . 19          // disposed particular patient is the highest priority level 
20 is          t.setPriority (10 );
 21 is          // start ordinary patients thread 
22 is          for ( int I = 0; I <50; I ++ ) {
 23 is              
24              the System .out.println ( "we are to the first" + (i + 1) + " position" + Thread.currentThread () getName () + " doctor." );
 25              IF (I ==. 9 ) {
 26 is                 try {
27                     t.join();
28                 } catch (InterruptedException e) {
29                     e.printStackTrace();
30                 }
31             }
32         }
33     }
34 }
Program entry
Thread class - Special Patient
. 1  Package cn.doctor;
 2  
. 3  / ** 
. 4  * 9527 :: @auther
 . 5  * @Description: Thread class - a doctor
 . 6  * @program: shi_yong
 . 7  * @Create: 2019-08-05 20:07
 . 8   * / 
. 9  public  class SeeDoctor the implements the Runnable {
 10  Private  int time;   // time it takes the doctor 
. 11  Private  int NUM;   // number of doctor 
12 is  
13 is      public SeeDoctor () {
 14      }
 15  
16      public SeeDoctor (int time, int num) {
17         this.time = time;
18         this.num = num;
19     }
20 
21     public int getTime() {
22         return time;
23     }
24 
25     public void setTime(int time) {
26         this.time = time;
27     }
28 
29     public int getNum() {
30         return num;
31     }
32 
33     public void setNum(int num) {
34         this.num = num;
35     }
36 
37     @Override
38     public void run() {
39         for (int i = 0; i < num; i++) {
40             System.out.println("正在给第"+(i+1)+"位"+ Thread.currentThread().getName()+"看病");
41             try {
42                 Thread.sleep(time);
43             } catch (InterruptedException e) {
44                 e.printStackTrace ();
45              }
 46          }
 47  
48  
49      }
 50 }

 

| - operating results

 

Guess you like

Origin www.cnblogs.com/twuxian/p/11305434.html