You are on page 1of 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 another thread in between. Also there is no lock over the object res.
Thread1 Thread1 Thread2 Thread2 Thread2 Thread1 of of of of of of class class class class class class A A B B B A is is is is is is inside inside inside inside inside inside resource1 resource1 resource1 resource1 resource1 resource1

Condition 2: Thread t1 and t2 accessing unsynchronized resource1() and resource2() respectively.


Thread1 Thread2 Thread2 Thread2 Thread1 Thread1 of of of of of of class class class class class class A B B B A A is is is is is is inside inside inside inside inside inside resource1 resourceb resourceb resourceb resource1 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 Thread2 Thread2 Thread2 Thread1 Thread1 Thread1 Thread1 Thread1 of of of of of of of of of class class class class class class class class class A B B B A A A A A is is is is is is is is is inside inside inside inside inside inside inside inside inside resourceb resource1s resource1 resource1 resourceb resourceb resource1 resource1 resource1

Condition 3: Thread t1 and t2 accessing unsynchronized resource1(), resource2() and resource1(),resource2() respectively. Concurrency . Similar to condition 3.
Thread1 Thread2 Thread2 Thread2 Thread2 Thread2 Thread2 Thread1 Thread1 Thread1 Thread1 Thread1 of of of of of of of of of of of of class class class class class class class class class class class class Ais inside resourceb B is inside resource1 B is inside resource1 B is inside resource1 Bis inside resourceb Bis inside resourceb Bis inside resourceb Ais inside resourceb Ais inside resourceb A is inside resource1 A is inside resource1 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 Thread1 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 Thread2 Thread2

of of of of of of of of of of

class class class class class class class class class class

A A A A A B B B B B

is is is is is is is is is is

inside inside inside inside inside inside inside inside inside inside

resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1

Condition 5: Thread t1 and t2 are accessing synchronized resource () and nonsynchronized 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, it can access it through the same object due to its visible nature.
Thread1 Thread2 Thread1 Thread2 Thread1 Thread1 Thread2 Thread1 of of of of of of of of class class class class class class class class A B A B A A B A is is is is is is is is inside inside inside inside inside inside inside inside resource1 resourceb resource1 resourceb resource1 resource1 resourceb 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 Thread1 Thread1 Thread1 Thread1 Thread1 Thread2 Thread1 Thread1 Thread2 Thread2 Thread2 Thread2 Thread2 Thread2 Thread2

of of of of of of of of of of of of of of of of

class class class class class class class class class class class class class class class class

A A A A A A B A A B B B B B B B

is is is is is is is is is is is is is is is is

inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside

resource1 resource1 resource1 resource1 resource1 resourceb resource1 resourceb resourceb resource1 resource1 resource1 resource1 resourceb resourceb resourceb

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 Thread1 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 of of of of of of of of class class class class class class class class A is inside resource1 A is inside resource1 A is inside resource1 A is inside resource1 A is inside resource1 Bis inside resourceb Bis inside resourceb 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 Thread1 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 Thread2 Thread2 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 of of of of of of of of of of of of of of of of class class class class class class class class class class class class class class class class A A A A A B B B B B A A A B B B is is is is is is is is is is is is is is is is inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resourceb resourceb resourceb resourceb resourceb resourceb

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 to access it from res since its object reference is res2 (vice versa for t1).

Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2

of of of of of of of of of of

class class class class class class class class class class

A B B A A B A B A B

is is is is is is is is is is

inside inside inside inside inside inside inside inside inside inside

resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 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 Thread2 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 of of of of of of of of of of of of of of of of class class class class class class class class class class class class class class class class A is inside resource1 B is inside resource1 B is inside resource1 A is inside resource1 B is inside resource1 A is inside resource1 B is inside resource1 A is inside resource1 B is inside resource1 A is inside resource1 Ais inside resourceb Bis inside resourceb Bis inside resourceb Ais inside resourceb Ais inside resourceb Bis inside resource

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 the opportunity to access the resource1 () when t1 is moving to ready to run state or sleep state or wait state.

Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread2 Thread1

of of of of of of of of of of

class class class class class class class class class class

A B B A A B B A B A

is is is is is is is is is is

inside inside inside inside inside inside inside inside inside inside

resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 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 Thread2 Thread2 Thread1 Thread1 Thread2 Thread1 Thread1

of of of of of of of of

class class class class class class class class

A B B A A B A A

is is is is is is is is

inside inside inside inside inside inside inside inside

resource1 resourceb resourceb resource1 resource1 resourceb resource1 resource1

Condition 13: :Thread t1 and t2 access synchronized resource1 (),unsynchronized resource2() and resource1 (),unsynchronized resource2() respectively through their respective object res and res2. Concurrency. When t1 is accessing synchronized resource1 (), t2 can be able to access either synchronized resource1 () or unsynchronized resource2 () through its object reference res2.
When resource1 () is before resource2 () for t1 in run method. When resource1 () is before resource2 () for t2 in run method.

Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1

of of of of of of of of of of of of of of of of

class class class class class class class class class class class class class class class class

A is inside resource1 B is inside resource1 B is inside resource1 A is inside resource1 A is inside resource1 B is inside resource1 B is inside resource1 A is inside resource1 B is inside resource1 A is inside resource1 Bis inside resourceb Ais inside resourceb Bis inside resourceb Ais inside resourceb Bis inside resourceb Ais inside resourceb

When resource2 () is before resource1 () for t1 in run method. When resource1 () is before resource2 () for t2 in run method.

Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2

of of of of of of of of of of of of of of of of

class class class class class class class class class class class class class class class class

Ais inside resourceb B is inside resource1 B is inside resource1 Ais inside resourceb Ais inside resourceb B is inside resource1 B is inside resource1 A is inside resource1 A is inside resource1 B is inside resource1 A is inside resource1 Bis inside resourceb A is inside resource1 Bis inside resourceb A is inside resource1 Bis inside resource

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 gets locked 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 Thread2 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread2 Thread1 Thread1 Thread2 Thread1 Thread2 Thread2 Thread1 of of of of of of of of of of of of of of of of class class class class class class class class class class class class class class class class A B B A A B B A B A A B A B B A is is is is is is is is is is is is is is is is inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resourceb resourceb resourceb resourceb resourceb resourceb

When resource2 () is before resource1 () for t1 in run method. When resource1 () is before resource2 () for t2 in run method. Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread2 Thread1 Thread2 Thread1 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 of of of of of of of of of of of of of of of of class class class class class class class class class class class class class class class class A B B A A B B A B A A B A B A B is is is is is is is is is is is is is is is is inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside resourceb resource1 resource1 resourceb resourceb resource1 resource1 resource1 resource1 resource1 resource1 resourceb resource1 resourceb resource1 resourceb

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 Thread1 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 Thread2 Thread2 Thread1 Thread1 Thread1 Thread2 Thread2 Thread2 of of of of of of of of of of of of of of of of class class class class class class class class class class class class class class class class A A A A A B B B B B A A A B B B is is is is is is is is is is is is is is is is inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside inside resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resource1 resourceb resourceb resourceb resourceb resourceb 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 Thread2 Thread1 Thread2 Thread1 Thread2 Thread2 of of of of of of of class class class class class class class A B A B A B B is is is is is is is inside inside inside inside inside inside inside resourceb resource1 resourceb resource1 resourceb resource1 resource1

Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread1 Thread1

of of of of of of of of of

class class class class class class class class class

B A B A B A B A A

is is is is is is is is is

inside inside inside inside inside inside inside inside inside

resource1 resource1 resourceb resource1 resourceb resource1 resourceb resource1 resource1

You might also like