ThreadUnsafe
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
======================================
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
and just make sure the keyword "synchronized" is entirely written in lowercase and not with a capital S 

yogesh sood
Ranch Hand
Posts: 108
posted 23 years ago
Thanx Val i will keep it in mind....
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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....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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Yogesh, Val and Anthony
Great Help!
Great Help!
Shila Sharma
Greenhorn
Posts: 10
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yes. But alternatively, you could also use a synchronized block
Shila Sharma
Greenhorn
Posts: 10
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Anthony
But what will make increment() in the above code as thread-safe?
But what will make increment() in the above code as thread-safe?
Anthony Villanueva
Ranch Hand
Posts: 1055
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Anthony !
It makes more sense to me now.
Regards
shila
It makes more sense to me now.
Regards
shila
| Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











