Solved

thread synchronization

Posted on 2014-02-07
6
323 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 36

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

724 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