Sixth job

Topic 1: write an application using Java multi-threading mechanism to achieve synchronization output time display.

 1 package shiliu;
 2 import java.util.Date;
 3 public class duoxiancheng {
 4     public static void main(String[] args) {
 5         ThreadTime time = new ThreadTime();
 6         time.start();        
 7     }
 8 }
 9  class ThreadTime extends Thread {
10     public void run() {
11         Date riqi = new Date();       
12         while (true) {
13             riqi = new Date();
14             System.out.println(riqi);
15             try {
16                 Thread.sleep(1000);
17             }catch (InterruptedException a) {
18                 a.getStackTrace();
19             }
20         }     
21     }
22 }

Topic 2: write an application using Java multi-threading mechanism to achieve guessing game (an integer between 0 to 100 range of random numbers)

. 1  Package shiliu;
 2  Import Classes in java.util *. ;
 . 3  public  class caishuzi {
 . 4      public  static  void main (String [] args) {
 . 5          System.out.println ( "guessing game now !!!" );
 . 6          the System .out.println ( "Please enter a number between 0-100:" );
 . 7          Scanner a = new new Scanner (the System.in);
 . 8          the Thread shu1 = new new the Thread ();
 . 9          the Thread T = new new the Thread (shu1) ;
 10          t.start ();        
 . 11          the try{
 12 is              the Thread.sleep (100 );
 13 is              the while ( to true ) {
 14                  int shu2 = a.nextInt ();
 15                  IF (shu1.getId ()> shu2) {
 16                      System.out.println ( "small number, please re-enter " );
 . 17                      BREAK ;
 18 is                  }
 . 19                  the else  IF (shu1.getId () == shu2) {
 20 is                      System.out.println ("! Congratulations ha answer it !!! " );
 21 is                  }
 22 is                  the else {
 23 is                     System.out.println ( "number is too large, please re-enter" );
 24                  }
 25              }
 26 is              
27              
28          } the catch (InterruptedException E) {
 29              e.printStackTrace ();
 30          }
 31 is  
32      }
 33 is  
34 is  }
 35  class the Num the implements {the Runnable
 36      int NUM;
 37 [      public  the synchronized  void RUN () {
 38 is          the Random = n- new new the Random ();
 39         num = n.nextInt (100 );
40      }
 41      public  int conceived () {
 42          return num;
43      }
 44      public  void substituted ( int num) {
 45          this .num = num;
46      }
 47 }

Guess you like

Origin www.cnblogs.com/wyd123/p/12076858.html