Debug入门(IDEA)

1.按键介绍

断点标志位:在此处进行断点,程序调试时会在此处停下

调试debug:进行程序调试

步入:一步一步朝下进行(不会进入官方类库的方法)

步过:一步一步朝下进行(跳过方法)

步出:从方法内,回到调用该方法的地方(不想看该方法体的时候)。 

返回:回到最初的断点

恢复程序:执行程序直到遇到下一断点(该断点已解决,想看下一个断点)

强制步入:可以进入官方类库的方法(方便查看JDK源码)

回到执行点:回到当前执行位置

回退:返回当前方法调用处(重走)

移动:移动暂停到光标处

计算表达式: 当想修改对象值的时候又不想在代码里面修改,则启动计算表达式搜索想修改的对象,修改它的值方便下面的判断然后改错。不会修改源来的赋值,保证代码一致性。

2.条件断点

断点右键可以增加判断条件,当只有符合这个条件,此断点才生效

3.多线程断点

3.1理论

 执行时可以右键变量

 查看当前线程

 断点时 右键,点击线程 使默认

此时执行就只有一个线程,方便调试

想切换线程,点击

3.2实践

多线程调试(生产者消费者)

public class Consumer  implements Runnable{
    Box box;
    public Consumer(Box box) {
        this.box = box;}

    @Override
    public void run() {
        while (true){
            box.get();         断点============
        }}}
public class Producer implements Runnable{
    Box box;
    public Producer(Box box) {
        this.box = box;}

    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            box.put(i);     断点=============
        }}}
public class Box {
    private int milk;
    private boolean state = false;

    public void put(int milk){
        System.out.println("放入开始");
        synchronized (this){
            if(state){
                try{
                    System.out.println("放入等待");
                    wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("放入等待结束");
            }
            this.milk=milk;
            System.out.println("员工将第" + this.milk + "瓶牛奶放入");
            this.state = true;
            notifyAll();
        }
        System.out.println("放入结束");
    }
    public void get(){
        System.out.println("拿出开始");
        synchronized (this){
            if(!state){
                try{
                    wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            System.out.println("用户拿到第" + this.milk + "瓶牛奶");
            this.state = false;
            notifyAll();
        }
        System.out.println("获取结束");
    }
}
public class MainClass {
    public static void main(String[] args) {
        Box box = new Box();     断点=====================
        Producer producer = new Producer(box);
        Consumer consumer = new Consumer(box);
        
        Thread thread1 = new Thread(producer, "生产者");
        Thread thread2 = new Thread(consumer, "消费者");
        
        thread1.start();
        thread2.start();
    }}

俩个线程均start(),state(判断是否有牛奶)

先进入消费者

遇到wait()

消费者进入等待状态

放入后 state=true ,notifyAll() 不会直接释放🔒,走出synchronized后,唤醒消费者线程

消费者已经被唤醒

继续执行,直到,

因为生产者执行了notifyall()结束后,释放了🔒,不会进入,处于等待状态,等待唤醒

执行完get 第五瓶牛奶后结束

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值