14. www.SunilOS.com 14
Set and get priority
Thread t = new Thread(“Ram”);
o t.setPriority(9);
o int i = t.getPriority();
Default priority is 5
Priority constants are defined in Thread class:
o Thread.MIN_PRIORITY=1
o Thread.MAX_PRIORITY=10
o Thread.NORM_PRIORITY=5
19. Thread creation
A java program executed as thread inside JVM
A thread can create another thread.
Threads work concurrently.
If one thread dies, other will remain active in
memory
www.SunilOS.com 19
20. www.SunilOS.com 20
Thread Lifecycle - States
2: start()
finish run()
1: new
Born Blocked
Runnable
Dead
1. I/O available
2. notify()
3. wake up()
1. block on I/O
2. wait()
3. sleep()
21. www.SunilOS.com 21
Thread Methods
start()
o Starts the thread and put into runnable queue
run()
o Performs operation
Thread.sleep(int m)/sleep(int m,int n)
o Sleeps the thread for given time in milliseconds and nanoseconds
Thread.yield()
o Pauses currently executing thread and put at last in the runnable queue
22. www.SunilOS.com 22
Daemon Threads
Daemon threads are supporting and background threads
It provides services to normal threads
Garbage Collector is a Daemon thread.
t.setDaemon(true)
Daemon
Thread
Normal
Thread
23. Usage | multi user system
www.SunilOS.com 23
Web Application
Database
25. www.SunilOS.com 25
Race Condition
Threads of a process can share common memory area
When two threads simultaneously try to access and modify an
object, it is called race-condition
The result of a program is affected by the order in which threads
are allocated CPU time and executed
26. www.SunilOS.com 26
Deposit in the Account
Account
Balance = 1000
Balance = 2000
getBalance():1000
setBalance(2000)
setBalance(2000)
getBalance():1000
getBalance()
CPU
getBalance()
setBalance()
setBalance()
User1 User 2
Deposit
Deposit
28. www.SunilOS.com 28
Account
1. public class Account {
2. private int balance = 0;
3. public int getBalance() {
4. try {
5. Thread.sleep(200);
6. } catch (InterruptedException e) {}
7. return balance;
8. }
9. public void setBalance(int balance) {
10. try {
11. Thread.sleep(200);
12. } catch (InterruptedException e) {}
13. this.balance = balance;
14. }
15.
29. www.SunilOS.com 29
Account
1. public synchronized void deposit(String msg, int amt) {
2. int bal = getBalance();
3. bal = bal + amt;
4. setBalance(bal);
5. System.out.println(msg + " new balance " + bal);
6. }
7. }
30. www.SunilOS.com 30
RacingCondThread
public class RacingCondThread extends Thread {
public static Account data = new Account();
String name = null;
public RacingCondThread(String name) {
this.name = name;
}
public void run() {
for(int i=0;i<5;i++){
data.deposit(name, 1000);
}
}
32. Output
void deposit(..)
o Ram balance 1000
o Shyam balance 1000
o Ram balance 2000
o Shyam balance 2000
o Shyam balance 3000
o Ram balance 3000
o Shyam balance 4000
o Ram balance 4000
o Shyam balance 5000
o Ram balance 5000
synchronized void deposit(..)
o Ram balance 1000
o Ram balance 2000
o Ram balance 3000
o Ram balance 4000
o Ram balance 5000
o Shyam balance 6000
o Shyam balance 7000
o Shyam balance 8000
o Shyam balance 9000
o Shyam balance 10000
www.SunilOS.com 32
33. www.SunilOS.com 33
Synchronization types
Method
public synchronized void deposit(String message, int amount)
{….}
Block
public void deposit(String message, int amount) {
synchronized (this){
int bal = getBalance() + amount;
setBalance(bal);
}
System.out.println(message + " Now Bal is " + bal);
}
34. www.SunilOS.com 34
Monitors / Locks
It is a lock or token of an object
The thread has lock, can access object
Other threads will be in waiting queue
One object has two monitors or tokens
o One for instance methods, called Object Monitor
o Second for static methods, called Class Monitor
35. www.SunilOS.com 35
Monitor Keys
Static Sync Methods
x
z
y
Instance Sync Methods
a
c
b
Thread1
Thread2
x() Class
Monitor
y()
a() Object
Monitor
b()
CLASS
36. Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 36