Solved

thread synchronization

Posted on 2014-02-07
6
291 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
Comment Utility
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
Comment Utility
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
Comment Utility
Please advise
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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
Comment Utility
I'll try and get back to you soon with one.

Sure.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now