jira confluence做网站,网站建设公司天成,网站建立的意义,国内响应式布局网站c语言中如何让诊断代码右移开发人员面临的更困难的任务之一是查找和诊断生产中运行缓慢的代码。 首先#xff0c;您如何监控生产代码而不放慢速度#xff1f; 当然#xff0c;您无法通过分析器运行生产代码。 即使您有计时代码的机制#xff0c;那么如何诊断问题呢#x… c语言中如何让诊断代码右移 开发人员面临的更困难的任务之一是查找和诊断生产中运行缓慢的代码。 首先您如何监控生产代码而不放慢速度 当然您无法通过分析器运行生产代码。 即使您有计时代码的机制那么如何诊断问题呢 如果您无法在开发环境中重现问题则尤其如此。 理想情况下您希望在生产中发生问题时得到通知并向其提供足够的信息以便有合理的机会修复或至少诊断问题。 这是我的同事彼得·劳瑞 Peter Lawrey建议的一种机制您可以用来精确地做到这一点。 完整的代码清单可在此处找到。 您要做的是创建一个Monitor类如下所示 public class Monitor implements Runnable{private final Thread thread;private final AtomicLong startTime new AtomicLong(Long.MAX_VALUE);private final int thresholdMS;public Monitor(Thread thread, int thresholdMS){this.thread thread;this.thresholdMS thresholdMS;}public void reset(){startTime.set(System.currentTimeMillis());}Overridepublic void run(){while(thread.isAlive()){long timeTaken System.currentTimeMillis()-startTime.get();if(timeTaken thresholdMS){System.out.println(timeTaken -------------------------);Stream.of(thread.getStackTrace()).forEach(System.out::println);}try {Thread.sleep(thresholdMS/2);} catch (InterruptedException e) {break;}}}
} 如果线程无法在阈值时间内重置则此类将转储正在运行的线程的堆栈跟踪。 这是一些示例程序演示如何调用Monitor。 Monitor monitor new Monitor(Thread.currentThread(), 8);
Thread thread new Thread(monitor, MonitorThread);
thread.setDaemon(true);
thread.start();while(true) {monitor.reset();double x0;for (int i 0; i 10_000; i) {x Math.sqrt(i);Logger.getLogger(getClass().getName()).fine(x x);}
} Monitor观察到了这一“关键”代码。 如果在8毫秒内未重置监视器它将转储代码的堆栈跟踪。 如果您有一个Monitor监视您的关键代码段则可以确保它们在给定的约束范围内执行。 如果代码确实违反了约束则可以通过检查堆栈跟踪来很好地了解问题所在。 您还可以使用它来查看关键代码在其运行期间未执行多少次。 如果没有专用的备用CPU进行监视则可以更改等待时间。 另外您可能想更改等待策略以允许GC暂停这会影响所有线程。 您可能想通过使用System.nanoTime()来细化计时而不是以毫秒为单位。 翻译自: https://www.javacodegeeks.com/2015/02/detect-diagnose-slow-code-production.htmlc语言中如何让诊断代码右移