java多线程实现猜数字_多线程猜数字游戏(3人),请问哪位大神会做啊,跪求

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import java.util.Random;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class Guess {

public int number = -1 ;

public volatile int number2;

public volatile int number3;

public Run2 r2 ;

public int count=0;

public Lock lock1 = new ReentrantLock();

public Object o = new Object();

public Object o2 = new Object();

public Run3 r3 ;

private static ExecutorService executor;

public static void main(String[] args){

ExecutorService e = Executors.newSingleThreadExecutor();

e.execute(new Guess().new Run1());

e.shutdown();

}

public class Run1 implements Runnable{

@Override

public void run() {

init();

Random r = new Random();

number = r.nextInt(100);

System.out.println("标准数字:" + number);

synchronized (o) {

o.notifyAll();

}

try {

TimeUnit.SECONDS.sleep(2);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

for(;;){

//lock1.lock();

count=0;

if(number==number2){

System.out.println("线程2正确");

break;

}

if(number>number2){

System.out.println("线程2小了" );

}

if(number

System.out.println("线程2大了");

}

if(number==number3){

System.out.println("线程3正确" );

break;

}

if(number>number3){

System.out.println("线程3小了");

}

if(number

System.out.println("线程3大了" );

}

//lock1.unlock();

synchronized (o) {

o.notifyAll();

}

synchronized (o2) {

try {

o2.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

System.out.println("over");

}

public void init(){

r2 = new Run2();

r3 = new Run3();

r2.setDaemon(true);

r3.setDaemon(true);

executor = Executors.newFixedThreadPool(2);

executor.execute(r2);

executor.execute(r3);

}

}

public class Run2 extends Thread{

/* (non-Javadoc)

* @see java.lang.Thread#run()

*/

@Override

public void run() {

synchronized (o) {

if(number==-1){

try {

o.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

while(true){

Random r = new Random();

//lock1.lock();

number2 = r.nextInt(100);

System.out.println("线程2:"+number2);

// lock1.unlock();

synchronized(o2){

if(count<1){

count++;

}else{

System.out.println("r2 notify" );

o2.notify();

}

System.out.println(count);

}

synchronized (o) {

try {

o.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

public class Run3 extends Thread{

@Override

public void run() {

synchronized (o) {

if(number==-1){

try {

o.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

while(true){

Random r = new Random();

//lock1.lock();

number3 = r.nextInt(100);

System.out.println("线程3:"+number3);

// lock1.unlock();

synchronized(o2){

if(count<1){

count++;

}else{

System.out.println("r3 notify" );

o2.notify();

}

System.out.println(count);

}

synchronized (o) {

try {

o.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值