THREAD Subject Session Tahun Versi : : : : T0934 / Multimedia Programming Foundation 3 2009 1/0 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 Bina Nusantara Course Outlines • • • • • Thread Why Multithreading? Create and Start a Thread Sleeping Thread Runnable vs. Thread Class 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 are running at the same time Bina Nusantara 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 Multithread Animation In Winamp, we could see process of AVS, Music, Equalizer, and Read Events have their own threads to run Bina Nusantara 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 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 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 Create Thread 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. Bina Nusantara Create Thread Bina Nusantara Create Thread • A simple program to use multi threads. • The result may be like this. Bina Nusantara Sleeping Thread • We could make a delay with Thread.sleep(). • Example in use: clock application. • Example: delay 5 second (5000 ms) 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) Bina Nusantara SampleTimer Bina Nusantara SampleTimer • A simple timer to demonstrate derived class from JFrame and implementing Runnable. • Full code is downloadable in Additional Material (SimpleTimer.java) 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.syscon.com/node/35929 • Thread. Wikipedia. 2009. http://en.wikipedia.org/wiki/Thread_(computer_science) Bina Nusantara