Advanced Java Programming Chapter 4 - Multhithreading
Advanced Java Programming Chapter 4 - Multhithreading
Multithreading
1
{ Chapter 4: Multithreading
Is the ability to have more than one program working at what seems like
at the same time. For example, you can print while editing or sending a
fax.
Process:
They assume that they can continue to work in a word processor, while other
applications download files, manage the print queue, and stream audio.
Even a single application is often expected to do more than one thing at a time.
Example 1: streaming audio application must simultaneously read the digital audio of
the network, decompress it, manage playback, and update its display.
Example 3: Web browser may need to download and display the contents of a
graphics file while rendering the rest of the associated Web page.
Each part can handle a different task at the same time that enables optimal
use of the available resources.
User thread:
User threads are high priority threads, They are designed to execute important
task in an application.
same purpose for the process running on the thread as method main() does for a full
application program.
program calls the start() method (inherited from class Thread), which
When the thread starts running, the run() method is invoked, and
completion.
Advanced Programming---Chapter Four---
17
Multithreading
Thursday, March 13, 2025
Example: Extending Thread
Class
Implementing the Runnable
18
interface
Three basic steps
Runnable interface
class ImplClass implements Runnable{
Multithreading
Thursday, March 13, 2025
Example: Implementing the
Runnable interface
Extending Thread Vs. 20
Implementing Runnable Interface
be executed in a thread.
The derived class cannot extend
any other base classes because the class can still extend other base
Java only allows single classes if necessary.
To summarize:
inheritance.
If the program needs a full control over the thread life cycle, extending
the Thread class is a good choice,
If the program needs more flexibility of extending other base classes,
implementing the Runnable interface would be preferable.
If none of these is present, either of them is fine to use.
Thread Methods 21
Method Description
public void start() Starts the thread in a separate path of execution, then invokes the run()
public void run() If this Thread object was instantiated using a separate Runnable target,
the run() method is invoked on that Runnable object.
public final void setName(String name) Changes the name of the Thread object. There is also a getName()
method for retrieving the name.
public final void setPriority(int priority) Sets the priority of this Thread object. The possible values are between
1 and 10.
public final void setDaemon(boolean A parameter of true denotes this Thread as a daemon thread.
on)
public final void join(long millisec) The current thread invokes this method on a second thread, causing the
current thread to block until the second thread terminates or the
specified number of milliseconds passes.
public void interrupt() Interrupts this thread, causing it to continue execution if it was blocked
for any reason.
public final boolean isAlive() Returns true if the thread is alive, which is any time after the thread has
been started but before it runs to completion.
Methods on currently running 22
thread
public static void yield() Causes the currently running thread to yield to any other threads of
ThreadA ThreadB
Suppose it two threads ThreadA to print number and ThreadB to print
characters are running,
every time a number is printed, the ThreadA task is yielded and ThreadB get
running.
So each number is followed by some characters.
Pausing a Thread: sleep() 25
The join method allows one thread to wait for the completion of another.
Multithreading
Thursday, March 13, 2025
suspend() and resume()
Suspend and resume:
Thread Priority
Every Java thread has a priority that helps the operating system determine the
28
order in which the threads are scheduled.
The Thread class has the int constants MIN_PRIORITY, NORM_PRIORITY, and
MAX_PRIORITY, representing 1, 5, and 10, respectively.
2. Time slicing: a task executes for a predefined slice of time and then reenters the pool of
ready tasks.
Synchronization: the action of make sure that only one thread can access the
resource at a given point in time.
Only one thread at a time may hold a lock on a monitor.
Locking is achieved by placing the keyword synchronized in front of the method definition or
block of code that does the updating.
To enter critical section, a thread needs to obtain the corresponding object’s lock.
Thread Synchronization 34
2. Synchronizing blocks
3. Static Synchronization
Example: Bank transaction
Consider a bank offering online access to its customers to 35
perform transactions on their accounts from anywhere.
Another thread invoking the same method of that object (respectively, class)
Synchronizing Blocks
37
A synchronized blocks enable us to synchronize part of the code
Wait(): calling thread give up the monitor and go to sleep until some other thread enters
the same monitor and calls notify().
NotifyAll(): wakes up all threads those called wait() and the one with highest priority will
Advanced Programming---Chapter Four---
39
Multithreading
Thursday, March 13, 2025
Example
Deadlock
40
A state of deadlock occurs when threads are waiting
Once Thread 1 acquires a lock on object1, Thread 2 has to wait for a lock
on object1.
Multithreading
Thursday, March 13, 2025
End of Chapter 4 }