java线程上级作业

卖东西

题目要求:

3个平台卖1000件商品,把他们卖东西的过程模拟出来

分析:

需要注意的是线程安全问题,加一个synchronized ,但是这个synchronized 加在哪里呢?注意是不能加在run()方法的前面的,因为他们每个都有自己的run方法,如果加在run方法的前面,就会一直显示的是一个商家去卖东西,我们要把synchronized加在buy()方法的前面

结论:

synchronized关键字放在run()方法前面是不正确的。这是因为synchronized是用来控制对共享资源的访问的,run()方法是线程的执行体,不涉及对共享资源的访问,因此在该方法上加锁并没有意义。

代码:

public class Large {
    public static void main(String[] args) {
        Sale sale = new Sale();

        new Thread(sale,"平台1").start();
        new Thread(sale,"平台2").start();
        new Thread(sale,"平台3").start();

    }
}

class Sale implements Runnable {
    private int nums = 1000;
    boolean flag = true;

    @Override
    public  void run() {
        while(flag) {
           buy();
        }
    }
    public void stop() {
        flag = false;
    }
    public synchronized void buy() {
        if(nums <= 0) {
            stop();
            return;
        }
        //解决线程安全问题
        System.out.println(Thread.currentThread().getName() + "销售了第: " + (1000 - --nums) + "件商品");
    }
}

龟兔赛跑:

题目要求,去模拟乌龟和兔子进行比赛跑步,当兔子跑到600米的时候需要休息

分析:

这个速度的模拟可以让sleep去模拟,这个是案例也是对sleep的复习

代码:

public class TheTortoiseAndTheHare {
    public static void main(String[] args) {
        Race race = new Race();//他们两个共用一个赛道
        new Thread(race, "兔子").start();
        new Thread(race, "乌龟").start();

    }
}
class Race implements Runnable {
    private static String winner;
    @Override
    public void run() {
        for(int i =  0; !gameOver(i);  ) {
            if(Thread.currentThread().getName().equals("兔子")) {//兔子
                if(i == 600) {//兔子跑到600米 需要休息
                    try {
                        Thread.sleep(1200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                else {
                    i += 12;//间接的模拟速度
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            } else {//乌龟
                i += 10;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            boolean flag = gameOver(i);
           // System.out.println("flag = " + flag);
            if(flag) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "-->跑了" + i + "米");
        }
    }
    private  boolean gameOver(int steps) {
        if(winner != null) {
            return true;
        }
        if(steps >= 1000) {
            winner = Thread.currentThread().getName();
            System.out.println("胜利者是: " + winner);
            return true;
        }
        return false;
    }
}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FindYou.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值