thread subject:t0934 / multimedia programming foundation session:3 tahun:2009 versi:1/0

17
THREAD Subject : T0934 / Multimedia Programming Foundation Session : 3 Tahun : 2009 Versi : 1/0

Upload: oscar-greene

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

THREAD

Subject : T0934 / Multimedia Programming FoundationSession : 3Tahun : 2009Versi : 1/0

Page 2: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Bina Nusantara

Learning Outcomes

In the end of this session, students must be able to: – Recognize Threads– Understand the concept of

multithreading– Use Threads and Multithreads

in Java

Page 3: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Bina Nusantara

Course Outlines

• Thread• Why Multithreading?• Create and Start a Thread• Sleeping Thread• Runnable vs. Thread Class

Page 4: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Bina Nusantara

Thread

• A path of code execution through a program, and each thread has its own local variables, program counter, and lifetime

• Modern OS is able to do all in the same time– read the slides– opening the web browser– copy files to USB flash-disk

All in the same time• So, running multiple thread

means the programs arerunning at the same time

Page 5: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Thread

• Example:Java code execution starts with the main() method and proceed in a path until the statements are completed. This is one thread.Another thread is actually run by Java Virtual Machine: the garbage collector thread. It cleans up discarded objects and reclaim their memory.Therefore, even a simple Java Program “Hello World” actually use multithreaded environment.

Bina Nusantara

Page 6: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Multithread Animation

Bina Nusantara

In Winamp, we could see process of AVS, Music, Equalizer, and Read Events have their own threads to run

Page 7: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Why Multithreading

• If only one thread was available, a program can only do one thing a time

• It’s not fun if a word processor program cannot be used to open a second document while the first was printed

• Allows user to do more than one action at one time

• User doesn’t have to wait for the first process to finish to start another

Bina Nusantara

Page 8: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Why Multithreading

• Running more than one thread also carry new resources

• Many threads run at one time cost more memory resources than one thread at a time

• Doesn’t limit how many thread that program can make, but the costs of memory and processor resources should be considered

Bina Nusantara

Page 9: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Create Thread

• Two ways to use threads in Java:– 1. Extending the java.lang.Thread class– 2. Implementing java.lang.Runnable interface

• Both ways has its own uniqueness• The steps to spawn a new thread are:

– 1. Extends Thread class or implement Runnable interface– 2. Override the run() method– 3. Invoke the start() method

Bina Nusantara

Page 10: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Create Thread

Bina Nusantara

A simple program to use a single thread.In this program, the thread is running the run() method when start() is invoked.The result is printing the “Usage of thread” on the monitor.

Page 11: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Create Thread

Bina Nusantara

Page 12: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Create Thread

• A simple program to use multi threads.

• The result may be like this.

Bina Nusantara

Page 13: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Sleeping Thread

Bina Nusantara

• We could make a delay with Thread.sleep().

• Example in use: clock application.• Example: delay 5 second (5000 ms)

Page 14: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Bina Nusantara

Runnable vs. Thread Class

• Java can’t extends 2 classes at once (eq. extends from JFrame and Thread is impossible)

• Use Runnable interface to replace Thread class

• Other than extending (in class) implementing (in interface)

Page 15: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

SampleTimer

Bina Nusantara

Page 16: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

SampleTimer

• A simple timer to demonstrate derived class from JFrame and implementing Runnable.

• Full code is downloadable in Additional Material (SimpleTimer.java)

Bina Nusantara

Page 17: THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0

Bina Nusantara

References

• Introduction to Java Programming. 7ed. Liang. 2009. Chapter 15.

• Java Thread Programming. 1ed. Hyde, Paul. 1999. • Multi-Threading in Java. Tod Cunningham. 1998.

http://java.sys-con.com/node/35929• Thread. Wikipedia. 2009.

http://en.wikipedia.org/wiki/Thread_(computer_science)