2. Thread model of Java
• All the class libraries are designed multithreading in
mind
• Java uses threads to enable entire system to be
asynchronous
• Once a thread has been started
• It can be suspended
• Suspended thread can be resumed
• It may be stopped
2
3. Thread model of Java contd..
• Thread features
• Creation of threads
• Context switch
• Thread priorities
• Synchronization
• Messaging
3
4. Creation of Threads
• Java provides two ways for creating threads by
• Extending Thread class
• Implementing Runnable interface
4
5. Context Switch
• Switch from one thread to other
• Two ways of context switch
• Thread can voluntarily relinquish control by
• Explicitly yielding
• Sleeping
• Blocking on pending I/O
• Context switch is done by selecting the highest priority thread
• Pre-emption
• Occurs when a high priority enters while executing a low
priority thread
5
6. Thread Priorities
• Thread priorities are simple integers
• Ranging from 1 to 10
• A relative measure
• Higher the priority – brighter the chances of getting
executed first
6
7. Synchronization
• As threads introduces asynchronous behavior in Java
programs
• Synchronization is needed when two or more
threads work with a shared resource
• ‘Monitor’ feature is used in Java to implement
Synchronization
• Monitors are used mutually exclusive
7
8. Synchronization contd..
• There is no special monitor class in Java
• Every object has its own implicit monitor
• Two ways to show synchronization
• synchronized methods
• synchronized statements
8
10. Summary
In this class ,you have learnt
• Thread basics
• Process
• Definition of thread
• Comparison of process and thread
• Thread model of Java
• Features of Java that implemented threads
10
11. Frequently Asked Questions
1. Compare a process and a thread
2. Which methods are useful for messaging in Java
3. Explain the thread model of Java
11
12. Quiz
1. Which one of the following is a thread ?
a) MS-Word
b) Windows Explorer
c) Spell checker in MS-Word
d) Notepad
12
13. Quiz Contd..
2. Which statement is true?
a) Thread does not depend on resources of any
process
b) Each Thread has its own heap memory
c) A Thread share heap with other threads
d) None
14. Home
Exception Handling
Graphical User Interface Programming (GUI)
Previous Java question papers with Answers for polytechnic
diploma (jntu)
OCT/NOV-2012 QUESTION PAPER
April / May 2012 c-09
October/ November-2011 c-09
April/ May 2011 c-09
APRIL/MAY-2011 (C-05)
Internal Lab question paper
External Lab question paper
BTECH JNTU LAB MANUAL
Lab practice
Core java interview questions