aspose file tools
The moose likes Threads and Synchronization and the fly likes Are these two methods equivalent? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Are these two methods equivalent?" Watch "Are these two methods equivalent?" New topic
Author

Are these two methods equivalent?

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1365

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.


[List of FAQs] | [Android FAQ] | [My Blog] | [Samuh Varta]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
[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

Joined: Oct 12, 2008
Posts: 1365

So, calling a static synchronized method from two threads is equivalent to calling that function twice, right?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
Yes. Well, assuming you use the same arguments in each case.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3188
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()


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1365

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are these two methods equivalent?
 
Similar Threads
Threads
synchronized blocks for static and non-static methods
how to make a thread sleep
Using wait() and notify()
Threading synchronization