用两个线程玩猜数字游戏,第一个线程负责随机给出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
运行结果:
我生成了数字:9
question:0
你猜的是:100
猜大了1
question:1
question:1
我猜:56
question:0
你猜的是:56
猜大了1
question:1
我猜:55
question:0
你猜的是:55
猜大了1
question:1
我猜:53
question:0
你猜的是:53
猜大了1
question:1
我猜:51
question:0
你猜的是:51
猜大了1
question:1
我猜:47
question:0
你猜的是:47
猜大了1
question:1
我猜:12
question:0
你猜的是:12
猜大了1
question:1
我猜:5
question:0
你猜的是:5
猜小了-1
question:-1
我猜:5
question:0
你猜的是:5
猜小了-1
question:-1
我猜:9
question:0
你猜的是:9
猜对了,就是2
我特么终于猜对了~