This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes using synchronized keyword in method, showing no effect. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "using synchronized keyword in method, showing no effect." Watch "using synchronized keyword in method, showing no effect." New topic
Author

using synchronized keyword in method, showing no effect.

Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
Hi All,

I written code like this,



My problem is, number is not properly incrementing and some numbers are not showing, some numbers are repeating. Like,

Here i as global static var. Its showing Output: (Its random Output)

0
0
3
5
5

How do I solve this.

Thanks:
Ramakrishna K.C
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

You haven't shown us how you're calling it - that's going to be relevant.

(Also, I notice i isn't private, so there's nothing stopping other code updating it directly and skipping the synchronization, though obviously I can't see if you're actually doing that).
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
Hi,

Thanks for the reply matthew. I'm calling like this,



Thanks:
Ramakrishna K.C
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
That "i" variable, I dint use anywhere else, also I'm incrementing that inside the method itself only. But I'm not getting where is the problem is.

Thanks:
Ramakrishna K.C
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I'm not sure, because if I run that code I just get 1, 2, 3, 4, 5, which I think is what you're expecting.

One possibility is that it's to do with each thread caching the value of i. When one thread updates a value, it's not guaranteed that all other threads see the update immediately. This is a complicated area and I may be getting the details wrong, but if that was the case then adding the keyword volatile to the declaration of i might work. But I still don't see how zero would ever be output, so maybe it's something else.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
This is the snapshot of my code.


[Thumbnail for Capture.PNG]

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

OK, that explains why it can output 0 (the order of the increment and output lines are switched). Try declaring the variable as volatile, see if that changes anything.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
After adding volatile.


[Thumbnail for Capture1.PNG]

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

I suspect The synchronized keyword is not working as you expect. I think you are expecting the synchronized keyword to prevent more than one thread from entering the method, right? And that's not happening.

The reason is that putting synchronized on a method is like doing this


Since you have 5 instances of runnable and when 5 threads get into threadProcess method, each of them have a different This, they don't block on the synchronized keyword.

One thing you can try to do is either have only one runnable given to 5 threads. Or wrap I in an object that is shared between run bales, and synchronize on that object

Volatile will work too, sort of. You will get to 5 if you use volatile, but you won't get the nice order that you are expecting.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

(Slaps head).

Of course. Thanks Jayesh. Since it's updating a static variable, I'd got it into my head that it was a static method. But it isn't. If it was it would be synchronized on the entire class, and I think it would do what is intended. But as it stands it's synchronizing on the individual objects, and since the different threads are accessing different objects it has no effect.
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
****One thing you can try to do is either have only one runnable given to 5 threads****

I tried this thing, code is


calling from



then it is showing input like this...

Thread[t1,5,main]
Thread[t2,5,main]
Thread[t3,5,main]
Thread[t4,5,main]
Thread[t5,5,main]
Current Counter is: 0, updated by: t4
t4 going to sleep
Current Counter is: 1, updated by: t4
t4 going to sleep
Current Counter is: 2, updated by: t4
t4 going to sleep
Current Counter is: 3, updated by: t4
t4 going to sleep
Current Counter is: 4, updated by: t4
t4 going to sleep

Exiting child thread t4
[t4] finished
Exiting child thread t4
[t4] finished
Exiting child thread t4
[t4] finished
Exiting child thread t4
Exiting child thread t4
[t4] finished
[t4] finished




All Threads names are Same. i.e t4. I'm not getting this.

Thanks:
Ramakrishna K.C
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

You store the variable t as an instance variable. You only have one instance, so you only have one variable. There are 5 threads but you only hold a reference to one. Get rid of the t variable and use Thread.currentThread().getName().


Steve
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 245
instead of t.getName() I've to use this Thread.currentThread().getName() ??
then, my code is

.

Yup. Correct. This is working. As Matthew Brown told (Slaps head).

Thanks Its working.


Thanks:
Ramakrishna K.C
 
 
subject: using synchronized keyword in method, showing no effect.
 
Similar Threads
HashMap to synchronized Map is not working
asking about threads as a begginer
Threads confusing
Doubt in threads !!
Does program terminate when main thread terminates ?