Threads Notes
Threads Notes
Pre-assessment Questions
1. Which package defines classes that implement GUI, such as buttons,
checkboxes, and comboboxes ?
a. java.applet
b. java.io
c. java.awt
d. java.net
2. Which class of the java.lang package provides methods for converting
characters from uppercase to lowercase?
a. System
b. Character
c. Object
d. Class
NIIT
Lesson 1B / Slide 1 of 25
Threads
NIIT
Lesson 1B / Slide 2 of 25
Threads
NIIT
1,
1,
2,
3,
2,
3,
3,
2,
3
2
1
1
Lesson 1B / Slide 3 of 25
Threads
Solutions to Pre-assessment
Questions
1.
2.
3.
4.
5.
NIIT
c.
b.
a.
a.
b.
java.awt
Character
floor()
getChars()
1, 3, 2
Lesson 1B / Slide 4 of 25
Threads
Objectives
NIIT
Lesson 1B / Slide 5 of 25
Threads
NIIT
Lesson 1B / Slide 6 of 25
Threads
NIIT
Lesson 1B / Slide 7 of 25
Threads
NIIT
Lesson 1B / Slide 8 of 25
Threads
NIIT
Lesson 1B / Slide 9 of 25
Threads
NIIT
Lesson 1B / Slide 10 of 25
Threads
Creating Threads
NIIT
You can use the following code to create a thread by implementing the
Runnable interface:
class NewThread implements Runnable
{
Thread t;
NewThread()
{
Packages and Streams
Lesson 1B / Slide 11 of 25
Threads
NIIT
Lesson 1B / Slide 12 of 25
Threads
NIIT
Lesson 1B / Slide 13 of 25
Threads
NIIT
Lesson 1B / Slide 14 of 25
Threads
NIIT
Lesson 1B / Slide 15 of 25
Threads
Lesson 1B / Slide 16 of 25
Threads
Lesson 1B / Slide 17 of 25
Threads
Lesson 1B / Slide 18 of 25
Threads
NIIT
When two threads need to share data, you must ensure that one thread does
not change the data used by the other thread.
Synchronizing Threads
Synchronization of threads ensures that if two or more threads need to
access a shared resource then that resource is used by only one thread
at a time.
Synchronization is based on the concept of monitor.
The monitor controls the way in which synchronized methods access an
object or class.
To enter an objects monitor, you need to call a synchronized method.
When a thread calls the wait() method, it temporarily releases the locks
that it holds. In addition, the thread stops running and is added to the
list of waiting threads for that object.
Packages and Streams
Lesson 1B / Slide 19 of 25
Threads
NIIT
Lesson 1B / Slide 20 of 25
Threads
NIIT
Lesson 1B / Slide 21 of 25
Threads
Deadlock Conditions
NIIT
Collaborate
Lesson 1C / Slide 22 of 20
Threads
NIIT
Collaborate
Lesson 1C / Slide 23 of 20
Threads
setDaemon() Method
NIIT
Collaborate
Lesson 1C / Slide 24 of 20
Threads
Garbage Collection
NIIT
Lesson 1B / Slide 25 of 25
Threads
Implementing Garbage Collection
You can explicitly run the garbage collector in Java to collect information
regarding how large the object heap is or to determine the number of objects
of a certain type that can be instantiated.
To obtain these values, you can use the totalMemory() and freeMemory()
methods.
The totalMemory() method returns the total memory in the JVM, and
The freeMemory() returns the amount of memory free in the JVM.
To run the garbage collector explicitly, you need to perform the following
steps:
Create an object of the Runtime class.
Runtime r = Runtime.getRuntime();
NIIT
Lesson 1B / Slide 26 of 25
Threads
Best Practices
The join() Method
NIIT
Collaborate
Lesson 1C / Slide 27 of 20
Threads
NIIT
When you have two tasks within a program that can execute independently,
make individual threads for them.
Collaborate
Lesson 1C / Slide 28 of 20
Threads
FAQs
NIIT
Collaborate
Lesson 1C / Slide 29 of 20
Threads
FAQs (Contd.)
How do you ensure that your threads share the processor of the computer
properly?
To ensure that all the threads share the memory of the processor properly,
call the yield() or sleep() method in the thread. The sleep() method is used
to keep the thread in the sleeping mode for a specified time. The yield()
method is used to allocate processor time to a low priority thread.
NIIT
Collaborate
Lesson 1C / Slide 30 of 20
Threads
Demonstration-Implementing
Threads in Java
Problem Statement
Solution
NIIT
Lesson 1B / Slide 31 of 25
Threads
Summary
In this lesson, you learned:
A thread is defined as the path of execution of a program. It is a sequence of
instructions that is executed to define a unique flow of control.
A program that creates two or more threads is called a multithreaded
program.
The two types of multitasking are:
Process-based
Thread-based
The various advantages of multithreading are:
Improved Performance
Minimized System Resources Usage
Simultaneous access to multiple applications
NIIT
Lesson 1B / Slide 32 of 25
Threads
Summary (Contd.)
NIIT
Lesson 1B / Slide 33 of 25
Threads
Summary(Contd.)
NIIT
Thread priorities are the integers in the range of 1 to 10 that specify the
priority of one thread with respect to the priority of another thread.
You can set the thread priority after it is created using the setPriority()
method declared in the Thread class.
Synchronization of threads ensures that if two or more threads need to
access a shared resource, the resource is used by only one thread at a
time.
Various threads can communicate with each other using the following
methods:
wait()
notify()
notifyAll()
Garbage collection is the feature of Java that helps to automatically
destroy the objects created and release their memory for future
reallocation.
Packages and Streams
Lesson 1B / Slide 34 of 25