本文共 7821 字,大约阅读时间需要 26 分钟。
什么是程序DeBug?
Debug,是程序开发人员必会的一项调试程序的技能。
企业中程序开发和程序调试的比例为1:1.5,可以说如果你不会调试程序,你就没有办法从事编程工作。
Debug能帮助我们做什么?
Debug对于程序学习者的意义
/** * @Author: TEAM-AG * @Description: 调试基本介绍 * @Date: Created in 14:15 2020-05-28 * @Modified By: */public class Corgi_debug1 { public static void main(String[] args) { int num = 1; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); num ++; System.out.println("num = " + num); System.out.println("end..."); }}
步入调试
步过调试按钮(F7)
作用
步入,一行一行地往下走,如果这一行上有方法,则进入方法内部。
一般用于进入自定义方法内,不会进入官方类库的方法。
import org.jetbrains.annotations.Contract;/** * @Author: TEAM-AG * @Description: 步过和步入调试的使用 * @Date: Created in 15:27 2020-05-28 * @Modified By: */public class Corgi_debug2 { public static void main(String[] args) { int a1 = add(1, 2); System.out.println(a1); int a2 = add(2, 3); System.out.println(a1 + a2); int result = a1 + a2; System.out.println(result); } @Contract(pure = true) private static int add(int a, int b){ int result = a+b; return result; }}
import java.util.ArrayList;import java.util.Iterator;/** * @Author: TEAM-AG * @Description: 强制步入和步出&并发修改异常 * @Date: Created in 16:13 2020-05-28 * @Modified By: */public class Corgi_debug3 { public static void main(String[] args) { System.out.println("begin..."); operatorList(); System.out.println("end..."); } private static void operatorList() { ArrayListlist1 = new ArrayList<>(); list1.add("Corgi"); list1.add("CC"); list1.add("Jim"); list1.add("JACK"); //获取集合迭代器 Iterator iterator1 = list1.iterator(); while (iterator1.hasNext()) { String name = iterator1.next(); if(name.equals("Jim")) { //改变集合元素个数 list1.add("JimAdd"); } } }}
/** * @Author: TEAM-AG * @Description: 计算表达式 * @Date: Created in 16:53 2020-05-28 * @Modified By: */public class Corgi_debug4 { public static void main(String[] args) { User user = new User("Corgi", "123456","超级管理员"); login(user); } private static void login(User user) { String userType = user.getUserType(); switch (userType) { case "超级管理员": System.out.println("超级管理员业务逻辑"); break; case "董事长": System.out.println("董事长业务逻辑"); break; case "经理": System.out.println("经理业务逻辑"); break; case "员工": System.out.println("进入员工业务逻辑"); break; default: System.out.println("角色不存在"); break; } }}class User{ private String userName; private String pwd; private String userType; public User(String userName, String pwd, String userType) { this.userName = userName; this.pwd = pwd; this.userType = userType; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; }}
作用
通过设置断点条件,在满足条件时,才停在断点处,否则直接运行。
/** * @Author: TEAM-AG * @Description: 条件断点 * @Date: Created in 17:10 2020-05-28 * @Modified By: */public class Corgi_debug5 { public static void main(String[] args) { for (int i = 1; i < 1000; i++) { execute(i); } } private static void execute(int num) { int rs = ((num + 3) * 5 + 9) / num; System.out.println(rs); }}
/** * @Author: TEAM-AG * @Description: 多线程调试-案例1 * @Date: Created in 21:04 2020-05-28 * @Modified By: */public class Corgi_debug6 { public static void main(String[] args) { Thread thread1 = new Thread(new MyRunnable(), "曹操"); Thread thread2 = new Thread(new MyRunnable(), "刘备"); Thread thread3 = new Thread(new MyRunnable(), "孙权"); thread1.start(); thread2.start(); thread3.start(); }}class MyRunnable implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "____进入"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "____离开"); }}
package Milk;/** * @Author: TEAM-AG * @Description:多线程调试-案例2 * @Date: Created in 10:58 2020-05-29 * @Modified By: */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(); }}
package Milk;/** * @Author: TEAM-AG * @Description: BOX * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Box { private int milk; private boolean state = false; public void put(int milk){ System.out.println("put begin"); synchronized (this){ if (state){ try{ System.out.println("put wait"); wait(); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("put wait end"); } this.milk = milk; System.out.println("送奶工将第" + this.milk + "瓶牛奶放入奶箱"); this.state = true; notifyAll(); } System.out.println("put end"); } public void get() { System.out.println("get begin"); synchronized (this) { if (!state) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("用户拿到第" + this.milk + "瓶牛奶"); this.state = false; notifyAll(); } System.out.println("get end"); }}
package Milk;/** * @Author: TEAM-AG * @Description: Producer * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Producer implements Runnable { private Box box; public Producer(Box box) { this.box = box; } @Override public void run() { for (int i=1; i<=5; i++){ box.put(i); } }}
package Milk;/** * @Author: TEAM-AG * @Description: Consumer * @Date: Created in 10:58 2020-05-29 * @Modified By: */public class Consumer implements Runnable { private Box box; public Consumer(Box box) { this.box = box; } @Override public void run() { for (int i=1; i<=5 ;i++){ box.get(); } }}
更多信息
📍IDEA_Debug
https://www.bilibili.com/video/BV1LJ41187hu?p=1
转载地址:http://nnzdf.baihongyu.com/