Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ThreadUnsafe

 
Shila Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • 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.
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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....
 
Akash Kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Yogesh, Val and Anthony
Great Help!
 
Shila Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Anthony !
It makes more sense to me now.
Regards
shila
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic