• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ThreadUnsafe

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I am new to this forum. Recently I came across a problem about ThreadUnsafe. Can anybody help to correct this. Here is the code:

public class ThreadUnsafe {
private static int commonCounter=0;
public int getCount() {return commonCounter;}
public void addCount(int val) { commonCounter+=val;}
public void subtractCount(int val)
{ commonCounter-=val; }
}
Here what change will make the class defined above thread-safe?
Regards
shila
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the synchronized modifier for your methods. For more information on threads, synchronicity, object locking please see the Sun thread tutorial.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change code as
======================================
public class ThreadUnsafe
{
private static int commonCounter=0;
Synchronized public int getCount()
{return commonCounter;}
Synchronized public void addCount(int val) { commonCounter+=val;}
Synchronized public void subtractCount(int val)
{ commonCounter-=val; }
}
===============================================
This make sure that if u share a object of class among more than 1 thread at a time only one operation can be performed by any thread. e.g if thread 1 is subtracting at the same time other thread cannot perform any of above listed function.
b'coz
"At a time only one thread can access synchronized method(s) of an object"

By making all function above ur assure that all operation are atomic and treated as a Unit.
hope it will help to some extent

Any Correction from Movers and Shakers of Javaranch
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and just make sure the keyword "synchronized" is entirely written in lowercase and not with a capital S
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
and just make sure the keyword "synchronized" is entirely written in lowercase and not with a capital S


Thanx Val i will keep it in mind....
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Yogesh, Val and Anthony
Great Help!
 
Shila Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Yogesh, Val ,Anthony and Akash
So similary in the following piece of code if I want to make method increment() as thread-safe, we should declare increment() as synchronized.
class Counter {
int count = 0;
int increment() {
int n=count;
count = n + 1;
return n;
}
}
Regards
Shila
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. But alternatively, you could also use a synchronized block
 
Shila Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anthony
But what will make increment() in the above code as thread-safe?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The synchronized block makes it thread safe. Only one thread at a time can execute the code inside the block since it holds the object lock of the object referred to after the synchronized keyword (in the example above, it is the Counter object referred to by c).
Of course, in the example above, you can always invoke the increment() method outside a synchronized block in which case all bets are off.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a sample code:

If you run it, all 4 threads will try to access the Counter object in a sequential manner. You can make it "thread-unsafe" by simply commenting out the synchronized(this) line.
 
Shila Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anthony !
It makes more sense to me now.
Regards
shila
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic