Download presentation
Presentation is loading. Please wait.
Published byOscar Greene Modified over 9 years ago
1
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0
2
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
3
Bina Nusantara Course Outlines Thread Why Multithreading? Create and Start a Thread Sleeping Thread Runnable vs. Thread Class
4
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
5
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
6
Multithread Animation Bina Nusantara In Winamp, we could see process of AVS, Music, Equalizer, and Read Events have their own threads to run
7
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
8
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
9
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
10
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.
11
Create Thread Bina Nusantara
12
Create Thread A simple program to use multi threads. The result may be like this. Bina Nusantara
13
Sleeping Thread Bina Nusantara We could make a delay with Thread.sleep(). Example in use: clock application. Example: delay 5 second (5000 ms)
14
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)
15
SampleTimer Bina Nusantara
16
SampleTimer A simple timer to demonstrate derived class from JFrame and implementing Runnable. Full code is downloadable in Additional Material (SimpleTimer.java) Bina Nusantara
17
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/35929http://java.sys- con.com/node/35929 Thread. Wikipedia. 2009. http://en.wikipedia.org/wiki/Thread_(computer_science) http://en.wikipedia.org/wiki/Thread_(computer_science)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.