Threads Condition
-
Upload
mohammad-adnan -
Category
Documents
-
view
215 -
download
0
Transcript of Threads Condition
-
7/30/2019 Threads Condition
1/10
Condition 1: Thread t1 and t2 accessing unsynchronized method resource1( ).
Without Thread.sleep() through an object of Resource class.
Answer: Concurrency. That mean even though the looping is of the resource1() is
not completed, chance are there of execution same resource1() by anotherthread in between. Also there is no lock over the object res.
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Condition 2: Thread t1 and t2 accessing unsynchronized resource1() andresource2() respectively.
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Condition 3: Thread t1 and t2 accessing unsynchronized resource1(),resource
and resource2() respectively.
Concurrency : Since unsynchronized, there is no lock over the object and the cpu
can hand over control to another thread when the thread in running phase is
moving out to ready to run phase or sleep phase or wait phase.
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1s
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
-
7/30/2019 Threads Condition
2/10
Condition 3: Thread t1 and t2 accessing unsynchronized resource1(), resource2()
and resource1(),resource2() respectively.
Concurrency . Similar to condition 3.
Thread1 of class Ais inside resourceb
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class Bis inside resourceb
Thread2 of class Bis inside resourceb
Thread2 of class Bis inside resourceb
Thread1 of class Ais inside resourceb
Thread1 of class Ais inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Condition 4: Thread T1 and T2 accessing synchronized method resource1()
defined inside run method of class A.
One by One. The resource object through which the CPU or its run method access
the synchronized resource method, lock shall be applied on the resource object
due to which only one thread can access at a time and the other thread wont be
able to access due to the lock at the resource object. Even during the thread sleep
time, the resource object will be locked for the synchronized resource() until the
thread process is completed .
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
-
7/30/2019 Threads Condition
3/10
Condition 5: Thread t1 and t2 are accessing synchronized resource () and non-
synchronized resource () respectively using their object.
Concurrency. A lock will be applied on the object when synchronized resource ()
is being accessed by t1. However, since the resource () in t2 is unsynchronized, itcan access it through the same object due to its visible nature.
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Condition 6: Thread t1 and t2 are accessing the synchronized resource,unsynchronized resource () and synchronized resource (), unsynchronized
resource () resource respectively through an Object resource.
Concurrency. For better result or analysis, Use sleep method (). Here the object is
locked for when synchronized resource 1 is being accessed by t1. However the
unsynchronized method can be accessed by another t2 since its run method
includes both the resources () ( same with t1) but cannot interrupt when t1 or t2
is accessing the synchronized resource since the object applies over itself to avoid
access interruption.
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
-
7/30/2019 Threads Condition
4/10
Condition 7: Threads t1 and t2 are accessing synchronized resource1 () and
synchronized resource2 () respectively through an object res.
One by One. Since the t1 run method contains the synchronized resource1 () and
t2 contains the synchronized resource2 () and when t1 is accessing resource1 (),lock is applied to both the synchronized resource1 () and synchronized resource2
() due to which the thread t2 wont be able to access its resource2 ().
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class Bis inside resourceb
Thread2 of class Bis inside resourceb
Thread2 of class Bis inside resourceb
Condition 8: Threads t1 and t2 are accessing their synchronized resource1 (),
resource2 () and synchronized resource1 (), resource2 () respectively through a
common Object res.
One by One. When t1 access resource1 (), t2 wont be able to access both
synchronized resource1 () and resource () due to lock applied over the Object
res.
Thread1 of class A is inside resource1Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
-
7/30/2019 Threads Condition
5/10
Condition 9: Thread t1 and t2 having respective Resource class object res1 and
res2 are trying to access unsynchronized resource1 ().
Concurrency. When t1 access resource1 () through its object res1 then t2 also
can access the same unsynchronized resource1 () .However t2 wont be able toaccess it from res since its object reference is res2 (vice versa for t1).
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Condition 10: Thread t1 and t2 having respective Resource class object res1 and
res2 are trying to access unsynchronized resource1 (),resource2 () and
unsynchronized resource1 (),resource2 () respectively.
Concurrency. When t1 is accessing any of the resource1 () or resource2 (), t2 at
the same time can access resource1 () or resource2 () based on CPU scheduler
algorithm.
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class Ais inside resourceb
Thread2 of class Bis inside resourceb
Thread2 of class Bis inside resourceb
Thread1 of class Ais inside resourceb
Thread1 of class Ais inside resourceb
Thread2 of class Bis inside resource
-
7/30/2019 Threads Condition
6/10
Condition 11: Thread t1 and t2 accessing synchronized resource1 () through their
object res and res2.
Concurrency : When t1 is accessing synchronized resource1 (), t2 would be able
to access the same synchronized resource1 () through its object res2 .t2 gets theopportunity to access the resource1 () when t1 is moving to ready to run state or
sleep state or wait state.
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Condition 12:Thread t1 and t2 access synchronized resource1 () and
unsynchronized resource2() respectively through their respective object res
and res2.
Concurrency. When t1 is accessing synchronized resource1 () through its res, t2
would be able to access only resource 2 concurrently since t2 run method does
not contain the resource1 () reference.
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
-
7/30/2019 Threads Condition
7/10
-
7/30/2019 Threads Condition
8/10
Condition 14: Threads t1 and t2 are accessing synchronized resource1 (),
synchronized resource2 () and synchronized resource1 (), synchronized resource2
() respectively through their respective object reference res and res2.
Concurrency. When t1 is accessing synchronized resource1 () then its object getslocked and t2 can be able to access any of the resource1() or resource2 () through
its object res2.
When resource1 () is before resource2 () for t1 in run method.
When resource1 () is before resource2 () for t2 in run method.
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resourceb
Thread2 of class B is inside resourceb
Thread1 of class A is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread1 of class A is inside resourceb
When resource2 () is before resource1 () for t1 in run method.
When resource1 () is before resource2 () for t2 in run method.
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
-
7/30/2019 Threads Condition
9/10
Condition 15: Thread t1 and t2 accessing static synchronized resource1 (), static
synchronized resource2 () and static synchronized resource1 (), static
synchronized resource2 () respectively using their respective object reference
res and res2.
One by One . When t1 is accessing static synchronized resource1 (), t2 wont be
able to access either of the static sync resource1 () and resource () because static
method memory is allocated only once and copy of it cannot be made i.e it is part
of the class but can never be part of the instance.
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread1 of class A is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Thread2 of class B is inside resourceb
Condition 16: Thread t1 and t2 accessing both static synchronized resource1 ()
and static resource2 () respectively through their respective object reference res
and res1.
Concurrency. When t1 is accessing static synchronized resource1 () then t2 would
be able to access the static resource2 () since it is unsynchronized. Also it can
access the static synchronized resource 1() when t1 is moving to ready to run
state or in sleep or in wait state.
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread1 of class A is inside resourceb
Thread2 of class B is inside resource1
Thread2 of class B is inside resource1
-
7/30/2019 Threads Condition
10/10
Thread2 of class B is inside resource1
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread2 of class B is inside resourceb
Thread1 of class A is inside resource1
Thread1 of class A is inside resource1