Results 1 to 11 of 11
- 07-14-2014, 08:17 AM #1
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
Synchronized Blocks in Static Methods Confusion
Hi guys,
I was reading this: Java Synchronized Blocks
As I'm trying to get my head around the synchronized keyword.
My query is based on some simple example code below.
Query: If threadOne executes before threadTwo and invokes the "add" method which synchronizes on the class object will thead2 continue to be blocked from updating both the
instanceVariableInt and the class variable someOtherInt until the intrinsic lock on Test.class has been released? (threadOne has left the synchronized statement code)
Java Code:public class Test { private static int value = 10; private static int someOtherInt = 50; private int instanceVariableInt = 100; public int add(int digit){ synchronized(Test.class){ value =+ digit; } return value; } public void printName(){ System.out.println(Test.class.getName()); } public void doSomething(){ //Update instance variable instanceVariableInt += 50; //Update some other class variable someOtherInt = 70; } public static void main(String[] args) { final Test threadOne = new Test(); final Test threadTwo = new Test(); new Thread(new Runnable() { public void run() { threadOne.add(10); } }).start(); new Thread(new Runnable() { public void run() { threadTwo.printName(); threadTwo.doSomething(); } }).start(); } }
Thanks :)
- 07-14-2014, 06:41 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Synchronized Blocks in Static Methods Confusion
Other than main, you have no static methods. The way it is written, only the add method will block another method from continuing with that method until the object lock is released. The two variables you mentioned may be updated by other means by other threads at any time since they are not subject to any synchronization.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-15-2014, 04:08 AM #3
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
Re: Synchronized Blocks in Static Methods Confusion
Thanks for the quick response Jim.
So when synchronization is performed on the class itself, it does not prevent other class variables from being updated during the period when a thread has lock on the class?
I was reading this article and just had another related question to the synchronized statement.
Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
Synchronized Statements
Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:
Java Code:public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); }
Cheers,
- 07-15-2014, 04:50 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Synchronized Blocks in Static Methods Confusion
Correct (except that threads don't have locks on the class, only on methods and synchronized blocks). Synchronization only affects simultaneous access to fields within the synchronized block when using the same lock (monitor).
Locks only apply to synchronization. If an instance method is synchronized on this, then that instance may still access other methods as long as they are not synchronized or not synchronized on this.
Some of this may not be intuitive and my answers also depend on understanding what you are asking. I recommend you try some examples using two threads (perhaps main and one other). Use different or same locks on different methods or synchronized blocks. If the code does not do what you expect, then someone on this forum should be able to explain why. It is essential to try examples to ensure full understanding.
Regards,
JimLast edited by jim829; 07-15-2014 at 06:20 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-15-2014, 06:50 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Synchronized Blocks in Static Methods Confusion
Yay Paint Creativity time! Hi, I'm Gimby and I suck at Paint.
Lets illustrate a basic sketch of what you want to achieve and what you want to prevent when it comes to thread synchronization. This is a simple world in which there is a wall with a closed door and an open door; that wall is all that stands between the nasty outdoors and the object properties.
That door has only one key; each thread is allowed through one at a time and must pass the key on to the next thread waiting in line. The world is sane, each thread manipulates the object all privately and alone behind that wall. The other threads cannot interfere.
Until one day there is this evil thread that sees the open door and just decides to run through it without waiting for its turn to get the key. There is chaos, there are now two threads that can pull on the levers and push the buttons of the object at the same time. Before long, the object grinds to a halt with lots of black smoke. If only that evil thread had waited for its turn to get the key.
The end! (good god I hope this looks okay)"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 07-15-2014, 07:03 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Synchronized Blocks in Static Methods Confusion
A masterpiece!
Please do not ask for code as refusal often offends.
** This space for rent **
- 07-15-2014, 07:16 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Synchronized Blocks in Static Methods Confusion
Hi, my name is Thread! See Thread run. Run Thread run! Watch Thread muck things up.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-23-2014, 03:32 AM #8
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
Re: Synchronized Blocks in Static Methods Confusion
Hi Guys,
Thanks a heap for the help! I understand how it all works out now ( I think). Great diagram too!
Locks only apply to synchronization. If an instance method is synchronized on this, then that instance may still access other methods as long as they are not synchronized or not synchronized on this.
One last curious question if you have 2 synchronized methods, but one synchronizes on this and the other synchronizes on the Class itself, are there 2 intrinsic locks which will allow an instance of the class to access both methods simultaneously?
Cheers :)
- 07-23-2014, 03:58 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Synchronized Blocks in Static Methods Confusion
Yes!
One last curious question if you have 2 synchronized methods, but one synchronizes on this and the other synchronizes on the Class itself, are there 2 intrinsic locks which will allow an instance of the class to access both methods simultaneously?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-23-2014, 10:15 AM #10
- 07-23-2014, 10:23 AM #11
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Synchronized Blocks in Static Methods Confusion
Watch your terminology there, this is a synchronized method:
Java Code:public synchronized void thisMethodIsSyncedDude(){ }
Java Code:public void thisMethodIsNotSyncedDude(){ synchronize(lockObject){ // ... but this part is } }
Ask yourself this: are this and SomeClass.class the same object with the same door?"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
using synchronized with static method calls used in multiple threads?
By johnmerlino in forum New To JavaReplies: 3Last Post: 07-01-2014, 12:55 AM -
Static Blocks
By penguinCoder in forum New To JavaReplies: 6Last Post: 10-13-2012, 04:22 AM -
How to handle synchronized blocks in clustered environments?
By Arun_N in forum Threads and SynchronizationReplies: 1Last Post: 04-16-2012, 05:36 PM -
alternative to static initialization blocks
By paola in forum New To JavaReplies: 1Last Post: 12-06-2008, 06:51 PM -
How to synchronize blocks instead of entire methods
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 07:39 PM
Bookmarks