cs117 oop chapter 8 - dr. paween · 2019. 4. 2. · •cs117 oop •chapter 8 java thread by dr....

24
CS117 OOP Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1

Upload: others

Post on 14-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

•CS117 OOP•Chapter 8Java Thread

By Dr. Paween Khoenkaw

Computer Science MJU

1

Page 2: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Task Execution Model

2

Single-Taskingท ำทละงำน

Multi-Taskingท ำพรอมกนหลำยงำน

Task คองาน หรอ หนวยยอยของโปรแกรมคอมพวเตอรโดยหนงโปรแกรมอาจจะแบงการท างานไดเปนหลาย Taskระบบปฏบตการ จะน า Task ไปท าการ Execute โดยมรปแบบหลกๆอย 2 แบบคอSingle-Tasking และ Multi-tasking

CPUT1T2T3

T1, T2, T3 CPUT1T2T3

T1T2T3

Page 3: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Multi-Tasking Execution Model

3

Multi-tasking มรปแบบการท างานหลกอย 2 แบบคอ1 Concurrent Programming

- ใช CPU เดยว แตแบงใหแตละโปรแกรมสลบกนใช Execute ทละ Task- Thread

2 Parallel Programming - ใช CPU หลายตว แยกกน Execute แตละ Task ไปพรอมๆกน- Core

Page 4: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

4

Task ทท ำงำนแบบ multi-tasking เรยกวำ Thread Thread เปนสวนหนงของ Process (คอมพวเตอรโปรแกรม)Thread หลำยตวอำจจะท ำงำนรวมกน เพอเปำหมำยเดยวกน

ตวอยำงเชน Process เกมสออนไลน จะประกอบไปดวยThread ตวท 1 ส ำหรบวำดภำพบนหนำจอThread ตวท 2 ส ำหรบตรวจจบกำรกดแปนพมพของผเลนThread ตวท 3 ส ำหรบรบ-สงขอมลผำนอนเตอรเนต

เกมสออนไลน GUI

Event-HandlingCommunication

Page 5: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

5

Java มกำรท ำงำนแบบ Thread โดยโปรแกรมทเลกทสดจะม 1 Threadเรยกวำ Main Thread

HelloWorld(Process)

main(Thread)

public class HelloWorld {public static void main(String[] args) {

System.out.println("Hello World");}

}

Page 6: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

6

ขอดของกำรใช Thread บน Java1 เขยนโปรแกรมงำย

- 1 thread ตอ 1 task2 โปรแกรมท ำงำนไดด

- thread จะท ำงำนเมอตองกำรใชงำนเทำนน- ไมตองรอใหงำนอนท ำงำนจนเสรจกอน

3 หลำย thread สำมำรถใชทรพยำกรรวมกนได- ใชตวแปรเดยวกนได

4 ใชหนวยประมวลผลแบบหลำย core ไดอยำงมประสทธภำพ- แตละ thread สำมำรถท ำงำนพรอมกนไดโดยไมตองสลบกนใช CPU

Page 7: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

7

ขอเสยของกำรใช Thread บน Java1 กำรสรำง thread ใหมจะใชทรพยำกรเพม

- ใชหนวยควำมจ ำเพม- ใชเวลำกำรท ำงำนเพมในกำรสรำง thread ใหม

2 กำรสอสำรระหวำง thread ท ำไดยำก- ตองตรวจสอบขอมลในตวแปรวำถกตองหรอไม

3 หำกมหลำย thread อำจเกด deadlock ขนได

Page 8: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

8

Deadlock

ลำง

ซก

ปนหมำด

ลำง ซก ปน ลำง ปน ลำง ปน

1 Thread

Page 9: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

9

Deadlock

ลำง

ซก

ปนหมำด

ลำง ซก ปน ลำง ปน ลำง ปน

รอ ลำง ซก รอ ปน ลำง รอ ปน ลำง ปน

2 Threads ( same task)

Page 10: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

10

Deadlock

ลำง

ซก

ปนหมำด

ลำง

รอ

ซก

3 Threads ( different task)

Resource

ProcessThread

รอ

รอ

รอ

รอ

รอ

รอ

รอ

รอ

รอ

Page 11: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

11

Deadlockลำง

ซก

ปนหมำด

ลำง รอ รอ รอ ....

รอ รอ รอ รอ ....

ซก รอ รอ รอ ....

2 Threads ( different task)

ระบบปฏบตกำร ปองกนกำรเกด deadlock ดวยวธกำร Preemption (บงคบ)Preemption คอกำรยด Resource กลบมำให process อนใชกอน แลวคอยคน Resource นนกลบไปให Process เดมใชตอจนกวำจะเสรจงำนไมใชทก platform จะม Preemptionดงนนหำกเรำตองใช resource ทใชพรอมกนไมได เรำตองไมใช resource นนนำนเกนไป เพอปองกนไมใหเกด deadlockวธนเรยกวำ Voluntarily (เสยสละ)

Page 12: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

12

หลำย class บน Java เปน multi-thread อยแลว โดยเรำไมตองโปรแกรมเพม

1 Thread

Page 13: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

13

หลำย class บน Java เปน multi-thread อยแลว โดยเรำไมตองโปรแกรมเพม

ม 2 Thread

Page 14: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

14

งำนประเภทไหนทตองสรำงเปน thread- งำนทม blocking loop (ตองกำรคำงบำงอยำงไวบนจอ)- งำนทตองมกำรรออะไรบำงอยำง (รอให user กดปม)- งำนทสำมำรถท ำไดลวงหนำ โดยไมตองรอใหงำนกอนหนำนนเสรจกอน- Task มล ำดบควำมส ำคญไมเทำกน

ตองคอยตรวจสอบวำมกำร click หรอไมBlocking loop

Page 15: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

15

เรำสำมำรถสรำง class ของเรำเองใหเปน thread ได1 ) ใช Thread Class2) ใช Runnable Interface

Thread Runnable

- คลำส- สบทอดคลำสไมได- ท ำงำนเสรจแลวจะถกท ำลำย- มขนำดใหญ- ใชงำนไดทนท- ควบคมกำรท ำงำนได- เหมำะกบงำนทมควำมซบซอน

- อนเตอรเฟส- สบทอดคลำสได- น ำกลบมำใชใหมได- มขนำดเลก- ท ำงำนเองไมได ตองใชคลำส

thread ชวยท ำงำน- ควบคมไมได ท ำไดแคสงใหเรม

ท ำงำน- เหมำะกบงำนงำยๆ

Page 16: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread

16

Thread class และ Runnable interface จะมวฎจกรชวตทคลาย Thread class เมอถกสราง instance (new) จะมการเรยกใช constructor และ thread จะเรมท างาน เมอถกใช method start()เมอเรมท างาน method run() จะถกเรยกใช Runnable interface จะไมม constructor จะเรมท างาน method run() จะถกเรยกใช

Page 17: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Hello Thread

17

public class HelloThread {public static void main(String[] args) {Thread task=new Thread(){@Overridepublic void run() {while(true){System.out.println(Calendar.getInstance().getTime());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};task.start();}}

รไดอยำงไรวำ Thread ไหน คอ code อะไร ?

ตอบ: ไมร เดำลวนๆ

Page 18: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Hello Thread

18

public class HelloThread {public static void main(String[] args) {Thread task=new Thread(“ClockThread”){@Overridepublic void run() {while(true){System.out.println(Calendar.getInstance().getTime());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};task.start();}}

ท ำไมตองตงชอ Thread ?

ตอบ: เพอใหเชคไดวำ thread ไหนยดท ำงำนไปแลว หรอยงท ำงำนอย

Page 19: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Thread and Swing

19

public class SwingThreadClock {public static void main(String[] args) {JFrame F=new JFrame("Hello Clock");final JLabel l=new JLabel("Hello");F.setBounds(1, 1, 400, 200);F.add(l);F.show();l.setFont(new Font("Serif", Font.BOLD, 30));Thread clock=new Thread("ClockThread"){@Overridepublic void run() {while(true){l.setText(Calendar.getInstance().getTime().toString());try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}};clock.start();

}

}

Page 20: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Thread and Swing

20

public class SwingThreadClock {public static void main(String[] args) {JFrame F=new JFrame("Hello Clock");JPanel P =new JPanel();F.add(P);final JLabel l=new JLabel("Hello");JButton b1=new JButton("Start");JButton b2=new JButton("Stop");F.setBounds(1, 1, 400, 200);P.add(l);P.add(b1);P.add(b2);F.show();l.setFont(new Font("Serif", Font.BOLD, 30));Thread clock=new Thread("ClockThread"){@Overridepublic void run() {while(true){l.setText(Calendar.getInstance().getTime().toString());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};

b1.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.start();}});

b2.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.stop();}}); }}

Page 21: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Thread and Swing

21

public class SwingThreadClock {public static void main(String[] args) {JFrame F=new JFrame("Hello Clock");JPanel P =new JPanel();F.add(P);final JLabel l=new JLabel("Hello");JButton b1=new JButton("Start");JButton b2=new JButton("Stop");F.setBounds(1, 1, 400, 200);P.add(l);P.add(b1);P.add(b2);F.show();l.setFont(new Font("Serif", Font.BOLD, 30));final Thread clock=new Thread("ClockThread"){@Overridepublic void run() {while(true){l.setText(Calendar.getInstance().getTime().toString());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};clock.start();b1.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.resume();}});

b2.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.suspend();}}); }}

Page 22: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Runnable

22

public class HelloRunnable {public static void main(String[] args) {Runnable task=new Runnable(){@Overridepublic void run() {while(true){System.out.println(Thread.currentThread().getName()+","+ Calendar.getInstance().getTime());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread t1=new Thread(task,"Thread1");t1.start();}}

Page 23: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Runnable

23

public class HelloRunnable {public static void main(String[] args) {Runnable task=new Runnable(){@Overridepublic void run() {while(true){System.out.println(Thread.currentThread().getName()+","+ Calendar.getInstance().getTime());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread t1=new Thread(task,"Thread1");Thread t2=new Thread(task,"Thread2");Thread t3=new Thread(task,"Thread3");t1.start();t2.start();t3.start();}}

Thread ไหนจะเรมท ำงำนกอน ?

ตอบ: ไมร ตวไหนโหลดเสรจกอนจะถกรนกอน ขนอยกบระบบปฏบตกำรแลวถำ Thread3 จะท ำงำนได กตอเมอ Thread2 เรมท ำงำนแลวจะท ำอยำงไร ?

ตอบ: ใหเขยนโปรแกรมเชคใน Thread3 วำ Thread2 เรมท ำงำนหรอยง ถำยงกใหรอ

Page 24: CS117 OOP Chapter 8 - Dr. Paween · 2019. 4. 2. · •CS117 OOP •Chapter 8 Java Thread By Dr. Paween Khoenkaw Computer Science MJU 1. Task Execution Model 2 Single-Tasking ท

Thread: Runnable and Swing

24

public class SwingRunnableClock {public static void main(String[] args) {JFrame F=new JFrame("Hello Clock");JPanel P =new JPanel();F.add(P);final JLabel l=new JLabel("Hello");JButton b1=new JButton("Start");JButton b2=new JButton("Stop");F.setBounds(1, 1, 400, 200);P.add(l);P.add(b1);P.add(b2);F.show();l.setFont(new Font("Serif", Font.BOLD, 30));Runnable task=new Runnable(){@Overridepublic void run() {while(true){l.setText(Calendar.getInstance().getTime().toString());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread clock=new Thread(task,"ClockThread");clock.start();b1.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.resume();}});

b2.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent arg0) {clock.suspend();}}); }}

ถงแมวำ Runnable interface จะตองใช Thread class ชวยในกำรท ำงำนแตจ ำนวน Thread ทเกดขนในระบบ กไมไดแตกตำงกน

ตองกำรรน Task ละ 1 Thread ควรใช Thread Classตองกำรรน Task ละหลำย Thread ควรใช Runnable Interface