Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

thread synchronization

Posted on 2014-02-07
6
Medium Priority
?
345 Views
Last Modified: 2014-03-14
Hi,

I am working on below example

http://www.avajava.com/tutorials/lessons/how-do-i-use-a-synchronized-block-in-a-static-method.html


I have not understood what author is trying to demonstrate as below

It's possible to synchronize a static method. When this occurs, a lock is obtained for the class itself. This is demonstrated by the static hello() method in the SyncExample class below. When we create a synchronized block in a static method, we need to synchronize on an object, so what object should we synchronize on? We can synchronize on the Class object that represents the class that is being synchronized. This is demonstrated in the static goodbye() method of SyncExample. We synchronize on SyncExample.class.


I got below output which is not clear to me
hello
goodbye

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39843986
Yeah, that example is not that good because you would get the same output even if you remove the synchronize modifer/block.

The main point that it is trying to convey is that the two methods have the same functionality. Putting the synchronized keyword in the method declaration (like in hello() method) is essentially the same as using the synchronized block (like in goodbye() method) with this class as the argument, ie. SyncExample.class
0
 
LVL 7

Author Comment

by:gudii9
ID: 39850790
you would get the same output even if you remove the synchronize modifer/block.



you mean below synchronized block inside goodbye() method right?
public static void goodbye() {
            synchronized (SyncExample.class) {
                  System.out.println("goodbye");
            }
When you say removing  synchronize modifer/block means as below right comment those two lines?


public class SyncExample {

	public static void main(String[] args) {
		hello();
		goodbye();
	}

	public static synchronized void hello() {
		System.out.println("hello");
	}

	public static void goodbye() {
		//synchronized (SyncExample.class) {
			System.out.println("goodbye");
	//	}
	}

}

Open in new window

Can you please point me to better practical example on this concept to understand clearly.

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39870576
Please advise
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39872245
Yes, I meant the synchronized block inside the goodbye method, and yes you can do it the way you did. I also meant that you can take the synchronized keyword out of the definition for the hello method (line 8).

The point of my comment though was more around the fact that that program all runs within the one thread, there is no multithreading and so the synchronized make no difference.

As for an example that does make proper use of it, it is a bit hard for me to do one or find one right at the moment. I'll try and get back to you soon with one.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39880860
I'll try and get back to you soon with one.

Sure.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39880871
using the synchronized block (like in goodbye() method) with this class as the argument, ie. SyncExample.class

Can there be synchronized blocks without any argument(like class name)?
Is that is possible?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
A solution for Fortify Path Manipulation.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question