• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are these two methods equivalent?

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the following two methods equivalent?


Is calling a static method method from two threads equivalent to calling that function twice? Here is what I mean:

I read something about static methods using a class level lock (by default) and so only one method can execute them at a time(they are thread safe).Then, again someone suggested: there is nothing inherent in static methods that makes them thread safe.
Questions:
1. Did i misinterpret thread safety and the stuff about static methods?
2. Is my definition of thread safety distorted?
3. if you can share a link, that would throw some light on the above mentioned, i'd be obliged.

Thanks.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[MT]: I read something about static methods using a class level lock (by default) and so only one method can execute them at a time

That would be true for static synchronized methods. It's not true for static methods in general.

[MT]: (they are thread safe).

Well, in my opinion, "thread safe" is a horribly misused term anyway, and no one should assume that just because a method is synchronized it's also thread safe. Many people have written unsafe code with Vector, for example.

[MT]: Then, again someone suggested: there is nothing inherent in static methods that makes them thread safe.

This is true.

[MT]: Questions:
1. Did i misinterpret thread safety and the stuff about static methods?


Yes.

[MT]: 2. Is my definition of thread safety distorted?

Yes, but mostly in a minor, straightforward way, pointed out above.

[MT]: 3. if you can share a link, that would throw some light on the above mentioned, i'd be obliged.

Well, the correction in this thread was pretty minor, I think, and was probably present in the original sources you learned from - whatever those may be. For more detailed explanations I know are not free - try Java Concurrency in Partice, or Java Threads. Or a few chapters in Effective Java. Sorry I don't have a better free link offhand.
[ October 23, 2008: Message edited by: Mike Simmons ]
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, calling a static synchronized method from two threads is equivalent to calling that function twice, right?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Well, assuming you use the same arguments in each case.
 
arulk pillai
Author
Ranch Hand
Posts: 3402
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the second call has to wait for the first call to get out of the synchronized code.

Also, your second example does not make sense. As per your code, your aMethod gets called 4 times.


-- 2 times by your main thread via the following calls




once each by the two new threads you started via



Remember start() --> calls run() method --> aMethod()
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your second example does not make sense.


It was just to support my question about synchronized static methods, which Mike replied to. Thanks for reminding me of its implications

Thanks Arulk and Mike for your time and answers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!