两个线程模拟猜数字

1.一个线程随机生成数字,以及判断是否才对

2.另一个线程接受用户的收入数字

package www.whf.test2;

import java.util.Scanner;

public class Test2 {
    static Thread t1 = null;

    public static void main(String[] args) {

        t1 = new Thread(() -> {
            int i = 1;
            int num0;
            while (true) {
                System.out.println("第" + (i++) + "轮");
                num0 = ((int) ((Math.random() * 10)));
                System.out.println("请猜");

                while (true) {
                    synchronized (t1) {
                        try {
                            t1.wait();
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                    }

                    // System.out.println("数字"+num0);
                    if (num0 == num.getnum()) {
                        System.out.println("猜中了");
                        break;
                    } else if (num0 > num.getnum()) {
                        System.out.println("猜小了");
                    } else {
                        System.out.println("猜大了");

                    }

                }
            }

        });
        t1.start();

        new Thread(() -> {
            Scanner sc = new Scanner(System.in);
            while (true) {
                num.setnum(sc.nextInt());
                System.out.println("我猜是" + num.getnum());
                synchronized (t1) {
                    t1.notifyAll();
                }

            }
        }).start();
    }

    static class num {
        static int num;
        static boolean flag = true;

        public static synchronized void setnum(int num) {
            Test2.num.num = num;
        }

        public static synchronized int getnum() {
            return num;
        }

    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用Java语言实现的代码: ```java import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class GuessNumberGame { private volatile boolean isOver = false; // 双方是否都对了 private volatile int target; // 要数字 private final Lock lock = new ReentrantLock(); // 共享锁 private final Condition guessCondition = lock.newCondition(); // 测条件 private final Condition hintCondition = lock.newCondition(); // 提示条件 // 第一个线程,负责设置要数字 private final Runnable setter = () -> { Random random = new Random(); lock.lock(); try { target = random.nextInt(100) + 1; // 随机生成1~100之间的整数 System.out.println("要数字已经设置好了,开始吧!"); guessCondition.signalAll(); // 唤醒所有等待测的线程 while (!isOver) { // 如果双方都还没有对 hintCondition.await(); // 等待线程给出测 guessCondition.signal(); // 唤醒等待测的线程 } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }; // 第二个线程,负责测 private final Runnable guesser = () -> { lock.lock(); try { while (target == 0) { // 如果还没有设置要数字,等待 guessCondition.await(); } while (!isOver) { // 如果双方都还没有对 int guess = new Random().nextInt(100) + 1; // 随机一个1~100之间的数 System.out.println("线测了:" + guess); if (guess == target) { // 对了 isOver = true; System.out.println("线对了!"); hintCondition.signal(); // 唤醒等待提示的线程 } else if (guess < target) { // 小了 System.out.println("线小了!"); hintCondition.signal(); // 唤醒等待提示的线程 guessCondition.await(); // 等待下次测 } else { // 大了 System.out.println("线大了!"); hintCondition.signal(); // 唤醒等待提示的线程 guessCondition.await(); // 等待下次测 } } guessCondition.signal(); // 唤醒可能还在等待的线程 } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }; public void play() throws InterruptedException { Thread thread1 = new Thread(setter); Thread thread2 = new Thread(guesser); Thread thread3 = new Thread(guesser); thread1.start(); thread2.start(); thread3.start(); thread1.join(); thread2.join(); thread3.join(); } public static void main(String[] args) throws InterruptedException { GuessNumberGame game = new GuessNumberGame(); game.play(); } } ``` 这个程序中使用了一个共享锁和两个条件变量来实现线程之间的等待和唤醒。其中,第一个线程用来设置要数字,第二个和第三个线程用来测,直到其中一个线对为止,整个程序才会结束。在setter线程中,我们先生成一个1~100之间的随机整数作为要数字,然后唤醒等待测的线程。接着我们进入一个循环,在循环中,如果双方都还没有对,我们就等待线程给出测,然后唤醒等待测的线程。在guesser线程中,我们首先等待setter线程设置好要数字,然后进入一个循环,如果双方都还没有对,我们就随机一个1~100之间的数,并根据测结果给出相应的提示,然后等待下一个测。如果对了,我们就将isOver标记为true,唤醒等待提示的线程,整个程序就结束了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值