weixin_43602396
weixin_43602396
2021-01-16 21:55
采纳率: 100%
浏览 89
已结题

为什么我的break不能结束我的while循环,从而让程序停下来啊??

//Runnable接口实现类MyRunnable

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable implements Runnable {
    public static int number = 100;
    public static int num1 = 0;
    public static int num2 = 0;
    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (number > 0) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String s = DoubleColorBallUtil.create();
                System.out.println("编号为:" + number + "的员工 从" + Thread.currentThread().getName() + " 入场! 拿到的双色球彩票号码是:" + s);
                number--;
            } else {
                System.out.println();
                System.out.println();
                System.out.println("从前门入场的员工总共:" + MyRunnable.num1 + "位员工。");
                System.out.println("从后入场的员工总共:" + MyRunnable.num2 + "位员工。");
                break;
            }
            if (Thread.currentThread().getName() == "前门") {
                num1++;
            } else if (Thread.currentThread().getName() == "后门") {
                num2++;
            }
            lock.unlock();
        }
    }
}
//工具类DoubleColorBallUtil
import java.util.Arrays;
import java.util.Random;

public class DoubleColorBallUtil{
    // 产生双色球的代码
    public static String create() {
        String[] red = {"01","02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16","17","18","19","20","21","22","23",
                "24","25","26","27","28","29","30","31","32","33"};
        //创建蓝球
        String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
        boolean[] used = new boolean[red.length];
        Random r = new Random();
        String[] all = new String[7];
        for(int i = 0;i<6;i++) {
            int idx;
            do {
                idx = r.nextInt(red.length);//0‐32
            } while (used[idx]);//如果使用了继续找下一个
            used[idx] = true;//标记使用了
            all[i] = red[idx];//取出一个未使用的红球
        }
        all[all.length-1] = blue[r.nextInt(blue.length)];
        Arrays.sort(all);
        return Arrays.toString(all);
    }
}

//测试类
public class work5 {
    public static void main(String[] args) {
        Runnable runnable = new MyRunnable();
        Thread t1 = new Thread(runnable, "前门");
        Thread t2 = new Thread(runnable, "后门");
        t1.start();
        t2.start();
    }
}
/*
我在学习多线程的时候,练习使用实现Runnable接口的方法来创建线程,以上代码解决的是:
某公司组织年会,会议入场时有两个入口,在入场时每位员工都能获取一张双色球彩票,假设公司有100个员工, 利用多线程模拟年会入场过程,并分别统计每个入口入场的人数,以及每个员工拿到的彩票的号码。

我想询问的是问什么我MyRunnable中run()方法的if(number>0)的else语句中的break不能把while循环结束,从而让程序停下来啊,我在控制台看到输出了前门和后面经过的人数,但是break好像没发挥作用啊,在idea的控制台我看到程序运行的方框那个还是红色的*/
  • 点赞
  • 收藏

1条回答 默认 最新

  • wwwjkarry
    断.桥 2021-01-16 23:47
    已采纳

    1.在while循环中把锁放在tryfinally中以便break后unlock一定运行。

    2.判断字符串是否相等应该用equals函数哦。

    3.如果只使用一个else的话,前门和后门两个进程最后都会输出一遍,所以可以只设置前门进程输出,后门进程就直接break就好了。

    这样,应该就能正确运行了吧!

    点赞 评论

相关推荐