Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method Synchronization

 
sr shashidhar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi frndz,

Consider we have DEMO class in which two methods(Both are Static Synchronized) say method1() and method2().
Two user defined Threads AThread extends Thread class and BThread implements Runnable interface is running in the program.

1) Suppose if Athread is calling method1() with object a1(DEMO a1=new DEMO();) and Bthread is also calling method2() with same object a1. What will be the flow of execution? i mean its either Concurrent or Sequential...?

2) Suppose if Athread is calling method1() with class name DEMO and Bthread is also calling method2() with class name DEMO. What will be the flow of execution? i mean its either Concurrent or Sequential...?


thanks in advance
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you say something like:



it simply means



So, since lock is acquired over 'class', and this is only one per class, only one thread can execute the method - no matter which object is being used to invoke this method.

Thus, answer to your question - the execution will be sequential in both scenarios.

HIH.
 
sr shashidhar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Anayonkar Shivalkar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic