Solved

thread synchronization

Posted on 2014-02-07
6
308 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
  • 4
  • 2
6 Comments
 
LVL 35

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 35

Accepted Solution

by:
mccarl earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
Tomcat: Unable to run tomcat service. 2 23
eclipse console opening separately 2 29
learn programming 8 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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