Thread, Runnable, Callable ต่างกันอย่างไร ?

บทความนี้เจ้าของบทความจะมาให้ความหมายของเจ้า Thread ตามที่เข้าใจเอง และยกตัวอย่างการเขียน Thread ในภาษา JAVA

Thread
1. thread เป็น Class บรรจุ method ต่างๆที่ทำงานเกี่ยวกับ thread
2. ทำ class ให้เป็น thread ด้วยการ extends
3. class ที่เป็น thread จะ run ได้ด้วยการเรียกใช้ method start() และไปทำงานที่ method run()
4. thread

Runnable
1. runnable เป็น interface บรรจุแค่ abstract method run()
2. ทำ class ให้เป็น thread ด้วยการ implements
3. runnable ไม่สามารถเรียกใช้ thread ได้โดยตรงต้องทำการ new instance thread
4. class ที่เป็น thread จะ run ได้ด้วยการเรียกใช้ method start() และไปทำงานที่ method run()

Callable เหมือนกับ runnable ทุกๆอย่างเพิ่มเติมมาในส่วนของการ return

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response