第十六周作业

题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

源代码:

Testmain.java文件

1 package CurrentTime;
2 
3 public class Testmain {
4     public static void main(String[] args) {
5          ThreadMethod tm=new ThreadMethod(); //创建ThreadMethod对象,继承父类Thread
6          tm.start();
7     }
8 }

ThreadMethod.java文件

 1 package CurrentTime;
 2 
 3 import java.util.Date;
 4 
 5 public class ThreadMethod extends Thread {
 6     public void run() {    //重写run()方法
 7         Date date=null;
 8         while(true){
 9             date=new Date();   //创建时间Date类对象
10             System.out.println(date);   //输出显示当前系统时间
11             try {
12                 Thread.sleep(1000); //正在运行的线程休眠1秒钟
13             } catch (InterruptedException e) {
14                 // TODO Auto-generated catch block
15                 e.printStackTrace();
16             }
17         }     
18     }
19 }

运行截图:

题目2:编写一个应用程序,利用Java多线程机制,实现猜数字游戏(随机数范围0~100之间的整数)。

源代码:

Test,java文件

1 package DigitalGame;
2 import DigitalGame.Threadmethod;
3 public class Test {
4     public static void main(String[] args) {
5         Threadmethod tm=new Threadmethod();  //创建使用Runnable接口Threadmethod类的对象
6         tm.thread1.start();  //让thread1线程进入等待序列
7         tm.thread2.start();   //让thread2线程进入等待序列
8     }
9 }

Threadmehod.java文件

 1 public class Threadmethod implements Runnable
 2 {
 3     Thread thread1,thread2;   //声明两个线程类对象
 4     int inputnum,rand,count;
 5     boolean bool1=false,bool2=false;
 6     public Threadmethod()     //构造方法
 7     {
 8         thread1=new Thread(this);
 9         thread2=new Thread(this);
10     }
11     public void run()  //重写run方法
12     {
13         while(true){
14             compare();  //执行同步方法compare()
15             if(count==3)
16                 return;
17         }
18     }
19     public synchronized void compare()  //同步方法compare()
20     {
21         if(Thread.currentThread()==thread1&&bool2==false){
22             rand=(int)(Math.random()*100)+1;   //随机生成1~100之内的数
23             System.out.println("$$$猜数字游戏开始啦$$$");
24             bool1=true;
25             bool2=true;    
26         }
27         if(Thread.currentThread()==thread1){
28             if(bool1==true)
29             {
30                 try {
31                     wait(); //中断线程thread1
32                 } catch (InterruptedException e) {
33                     // TODO Auto-generated catch block
34                     e.printStackTrace();
35                 }
36             }
37             if(inputnum<rand){   //作比较
38                 System.out.println("抱歉,您猜小了!");
39                 count=1;
40             }
41             else if(inputnum>rand){
42                 System.out.println("抱歉,您猜大了!");
43                 count=2;
44             }
45             else if(inputnum==rand){
46                 System.out.println("恭喜,您猜对了!");
47                 count=3;
48             }
49             bool1=true;
50             notifyAll();  //恢复因执行wait()方法中断的线程Thread1
51         }
52         if(Thread.currentThread()==thread2)
53         {
54               while(bool1==false)
55               {
56                    try {
57                         wait();  //中断线程thread2
58                     } catch (InterruptedException e) {}
59               }  
60               if(count<3)
61               {
62                   System.out.println("****************");
63                   System.out.println("请输入您猜测的数:(范围1~100)");
64                   Scanner reader=new Scanner(System.in);
65                   inputnum=reader.nextInt();   
66               }
67               bool1=false;
68         }
69         notifyAll();  
70     }
71 }

运行截图:

posted @ 2019-12-21 15:18  jie130  阅读(162)  评论(0编辑  收藏  举报