Threads in Java: (Deitel & Deitel)
Threads in Java: (Deitel & Deitel)
Threads in Java
(Deitel & Deitel)
OOutline
1- Introduction
1- Class Thread: An Overview of the Thread Methods
1- Thread States: Life Cycle of a Thread
1- Thread Priorities and Thread Scheduling
1- Thread Synchronization
1- Daemon Threads
1- Runnable Interface
1- Thread Groups
Introduction
• Threads of execution
– Each thread is a portion of a program that can execute
concurrently with other threads (multithreading)
• C and C++ are single-threaded
• Gives Java powerful capabilities not found in C and C++
– Example: downloading a video clip
• Instead of having to download the entire clip then play it:
• Download a portion, play that portion, download the next
portion, play that portion... (streaming)
• Ensure that it is done smoothly
– Other example applications of multi-threading?
• Portability
– Differences between platforms (e.g., Solaris, Windows, …)
• On Solaris (Linux?)
– A thread runs to completion or until a higher priority thread
becomes ready
– Preemption occurs (processor is given to the higher-priority
thread)
• On Win32 (Windows 9x, NT, XP)
– Threads are timesliced
• Thread given quantum of time to execute
• Processor then switched to any threads of equal priority
– Preemption occurs with higher and equal priority threads
• Java scheduler
– Keeps highest-priority thread running at all times
– If timeslicing available, ensure equal priority threads execute
in round-robin fashion
– New high priority threads could postpone execution of lower
priority threads
• Indefinite postponement (starvation)
• Priority methods
– setPriority( int priorityNumber )
– getPriority
– yield - thread yields processor to threads of equal priority
• Useful for non-timesliced systems, where threads run to
completion
Prentice Hall, Inc. All rights reserved.
11
Thread Synchronization
• Monitors
– Object with synchronized methods
• Any object can be a monitor
– Methods declared synchronized
• public synchronized int myMethod( int x )
• Only one thread can execute a synchronized method at
a time
– Obtaining the lock and locking an object
• If multiple synchronized methods, only one may be active
– Java also has synchronized blocks of code
Thread Synchronization
Daemon Threads
• Daemon threads
– Threads that run for benefit of other threads
• E.g., garbage collector
– Run in background
• Use processor time that would otherwise go to waste
– Unlike normal threads, do not prevent a program from
terminating - when only daemon threads remain, program exits
– Must designate a thread as daemon before start called:
void setDaemon( true );
– Method boolean isDaemon()
• Returns true if thread is a daemon thread
Runnable Interface
Synchonized blocks
Program Output
Thread Groups
• Thread groups
– Why might it be useful to organize threads into groups?
– May want to interrupt all threads in a group
– Thread group can be parent to a child thread group
• Class ThreadGroup
– Constructors
ThreadGroup( threadGroupName )
ThreadGroup( parentThreadGroup, name )
• Creates child ThreadGroup named name
• Use constructors
– Thread( threadGroup, threadName )
– Thread( threadGroup, runnableObject )
• Invokes run method of runnableObject when thread
executes
– Thread( threadGroup, runnableObject,
threadName )
• As above, but Thread named threadName
ThreadGroup methods
• ThreadGroup Methods
– See API for more details
– activeCount
• Number of active threads in a group and all child groups
– enumerate
• Two versions copy active threads into an array of references
• Two versions copy active threads in a child group into an array
of references
– getMaxPriority
• Returns maximum priority of a ThreadGroup
• setMaxPriority
– getName, getParent