Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

java synchronized block

example 1
-------------------------------------------------------------------------------------
inc1(String name) {
synchronized(this) {
c1++ --------------------T1 can enter , this cannot be modified else where
}
}


inc2(String name) {
synchronized(this) { --------------------T2 (thread 2 cannot go inside and change)
c2++
}
}

example 2
-------------------------------------------------------------------------------------

inc1() {
synchronized(lock1) { ---------------lock1 cannot be modified else where
c1++;-------------------------------T1 can enter
}
}

inc2() {
synchronized(lock2) { ---------------lock2 cannot be modified else where
c2++; ------------------- T2 also can enter and change
}
}

No comments:

Post a Comment

Search 24 Bytes