编写多线程程序,模拟多个人通过一个山洞。这个山洞每次只能通过一个人,每个人通过山洞的时间为2秒(sleep)。随机生成10个人,都要通过此山洞,用随机值对应的字符串表示人名,打印输出每次通过山洞的人名。提示:利用线程同步机制,过山洞用一条输出语句表示,该输出语句打印输出当前过山洞的人名,每个人过山洞对应一个线程,哪个线程执行这条输出语句,就表示哪个人过山洞。
package week15;
public class Test01 {
public static void main(String[] args) {
//实例化山洞
Shandong shandong = new Shandong();
//创建10个线程
Thread thread1 = new Thread(shandong, "1");
Thread thread2 = new Thread(shandong, "2");
Thread thread3 = new Thread(shandong, "3");
Thread thread4 = new Thread(shandong, "4");
Thread thread5 = new Thread(shandong, "5");
Thread thread6 = new Thread(shandong, "6");
Thread thread7 = new Thread(shandong, "7");
Thread thread8 = new Thread(shandong, "8");
Thread thread9 = new Thread(shandong, "9");
Thread thread10 = new Thread(shandong, "10");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}
}
//山洞
class Shandong extends Thread{
@Override
public void run() {
//上锁
synchronized(this){
System.out.println(Thread.currentThread().getName()+"正在通过,请等待5s");
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.用两个线程玩猜数字游戏,第一个线程负责随机给出1~100之间的一个整数,第二个线程负责猜出这个数。要求每当第二个线程给出自己的猜测后,第一个线程都会提示“猜小了”、“猜大了”或“猜对了”。猜数之前,要求第二个线程要等待第一个线程设置好要猜测的数。第一个线程设置好猜测数之后,两个线程还要相互等待,其原则是:第二个线程给出自己的猜测后,等待第一个线程给出的提示;第一个线程给出提示后,等待给第二个线程给出猜测,如此进行,直到第二个线程给出正确的猜测后,两个线程进入死亡状态。
package week15;
import java.util.Random;
public class Test02 {
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;//猜的数字
}