File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ThreadUnsafe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ThreadUnsafe " Watch "ThreadUnsafe " New topic
Author

ThreadUnsafe

Shila Sharma
Greenhorn

Joined: Jul 18, 2002
Posts: 10
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Aug 31, 2000
Posts: 108
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


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
and just make sure the keyword "synchronized" is entirely written in lowercase and not with a capital S


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
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

Joined: Jul 17, 2002
Posts: 13
Thanks Yogesh, Val and Anthony
Great Help!
Shila Sharma
Greenhorn

Joined: Jul 18, 2002
Posts: 10
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

Joined: Mar 22, 2002
Posts: 1055
Yes. But alternatively, you could also use a synchronized block
Shila Sharma
Greenhorn

Joined: Jul 18, 2002
Posts: 10
Thanks Anthony
But what will make increment() in the above code as thread-safe?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jul 18, 2002
Posts: 10
Thanks Anthony !
It makes more sense to me now.
Regards
shila
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadUnsafe