เมื่อ Stop แล้วมันไม่จบ

เคยไหม เปิดหน้าเว็บเพื่อทำธุรกรรมบางอย่างแล้วรอนานมากจนกด Stop แล้ว Refresh เพื่อเริ่มใหม่?

สำหรับคนทั่วไปอาจจะคิดว่าเมื่อ Stop แล้วทุกอย่างจะจบพร้อมเริ่มใหม่เมื่อเรากด Refresh แต่จริง ๆ แล้วมันไม่ได้เป็นเช่นนั้นเลย

ความจริงที่เกิดขึ้นคือการ Stop ของเราเป็นแค่การหยุดรับผลลัพธ์ (HTTP Response ที่ถูกส่งกลับมา) แต่กระบวนการสร้างผลลัพธ์นั้นก็ยังคงดำเนินต่อไป

ตัวอย่างง่าย ๆ ลองเขียน Java Servlet หนึ่งตัวโดย Code ต่อไปนี้

try {
    this.getServletContext().log("Begin: " + Calendar.getInstance().toString());
    Thread.sleep(20 * 1000);
    // ... (Write something out to screen)
    throw new Exception();
} (Exception e) {
    this.getServletContext().log("End: " + Calendar.getInstance().toString());
}

จากตัวอย่าง Code ด้านบน จะเห็นว่ามีการหน่วงเวลาไว้ 20 วินาทีก่อนจะแสดงผลใด ๆ ออกทางหน้าจอ แต่หากเรากด Stop ไปที่ช่วงวินาทีที่ 15 แล้วอ่าน Log ของ Tomcat หลังจากนั้นซักพักใหญ่ เราจะพบทั้ง Begin และ End ใน Log นั่นจึงเป็นการพิสูจน์ได้ว่า เมื่อเกิดการ Request ขึ้นมา กระบวนการในการจัดการ Request จะถูกดำเนินการจนแล้วเสร็จโดยไม่สนว่าผู้รับ (ในกรณีนี้คือ Browser) จะอยู่รอรับผลหรือไม่

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <tt> <sup> <sub> <img> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can link nodes to other nodes using the following syntax:
    [node:node_id,title="val2"]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.