java 线程作业2

来源:互联网 发布:nvsip软件下载 编辑:程序博客网 时间:2024/06/05 03:39

用两个线程玩猜数字游戏,第一个线程负责随机给出1~100之间的一个整数,第二个线程负责猜出这个数。要求每当第二个线程给出自己的猜测后,第一个线程都会提示“猜小了”、“猜大了”或“猜对了”。猜数之前,要求第二个线程要等待第一个线程设置好要猜测的数。第一个线程设置好猜测数之后,两个线程还要相互等待,其原则是:第二个线程给出自己的猜测后,等待第一个线程给出的提示;第一个线程给出提示后,等待给第二个线程给出猜测,如此进行,直到第二个线程给出正确的猜测后,两个线程进入死亡状态。


import java.util.Random;public class test2 {    static Object setnumber=new Object();    static Object guess=new Object();    public static void main(String[] args) {        Question question=new Question(setnumber,guess);        Answer answer=new Answer(setnumber,guess);        new Thread(question).start();        new Thread(answer).start();    }}class Question implements Runnable{    Object setnumber;    Object guess;    int number;    public Question(Object setnumber, Object guess) {        // TODO Auto-generated constructor stub        this.setnumber=setnumber;        this.guess=guess;    }    @Override    public void run() {        // TODO Auto-generated method stub        synchronized (setnumber) {            Random rand = new Random();            number=rand.nextInt(100);            State.initialize=1;            System.out.println("我生成了数字:"+number);        }//      synchronized()        while(true){            System.out.println("question:"+State.answed);            if(State.answed!=0){                try {                    Thread.sleep(100);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }else {                synchronized (guess) {                    System.out.println("你猜的是:"+State.answednumber);                    if(State.answednumber<number){                        State.answed=-1;                        System.out.println("猜小了"+State.answed);                    }if(State.answednumber>number){                        State.answed=1;                        System.out.println("猜大了"+State.answed);                    }if(State.answednumber==number){                        State.answed=2;                        System.out.println("猜对了,就是"+State.answed);                        break;                    }                    Thread.yield();                }            }        }    }}class Answer implements Runnable{    Object setnumber;    Object guess;    int min=0;    int max=100;    Random rand = new Random();    public Answer(Object setnumber, Object guess) {        // TODO Auto-generated constructor stub        this.setnumber=setnumber;        this.guess=guess;    }    @Override    public void run() {        // TODO Auto-generated method stub        while(State.initialize==0){//如果没有运行question方法就等待。            try {                Thread.sleep(100);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        synchronized (setnumber) {            while(true){                if(State.answed==0){                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }                else {                    synchronized (guess) {                        if(State.answed==1){                            max=State.answednumber;                            State.answednumber=rand.nextInt(max-min)+min;                            System.out.println("我猜:"+State.answednumber);                        }                        if(State.answed==-1){                            min=State.answednumber;                            State.answednumber=rand.nextInt(max-min)+min;                            System.out.println("我猜:"+State.answednumber);                        }                        if(State.answed==2) {                            System.out.println("我特么终于猜对了~");                            break;                        }                        State.answed=0;                        Thread.yield();                    }                }            }        }    }}class State{//内部static变量充当全局变量的作用来为线程间进行通讯    static int initialize=0;//是否被初始化。    static int answed=0;//用来表示是菜大了还是猜小了    static int answednumber=100;//猜的数字}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

运行结果:

我生成了数字:9question:0你猜的是:100猜大了1question:1question:1我猜:56question:0你猜的是:56猜大了1question:1我猜:55question:0你猜的是:55猜大了1question:1我猜:53question:0你猜的是:53猜大了1question:1我猜:51question:0你猜的是:51猜大了1question:1我猜:47question:0你猜的是:47猜大了1question:1我猜:12question:0你猜的是:12猜大了1question:1我猜:5question:0你猜的是:5猜小了-1question:-1我猜:5question:0你猜的是:5猜小了-1question:-1我猜:9question:0你猜的是:9猜对了,就是2我特么终于猜对了~


原创粉丝点击