Redian新闻
>
synchronization for counters
avatar
synchronization for counters# Java - 爪哇娇娃
gm
1
昨晚梦到一个人,他的身体卷了起来,就像卷起的一套衣服一样。然后,他在墙里面滚
动。后来,我听人说那个人被捂住嘴勒死了然后被剥了皮。我吓了一跳,才意识到我看
到那个滚动的衣服卷就是那个被勒死的人鬼魂,也可能是他的穿着衣服的皮……后来貌
似醒了。感觉这个梦很怪异。
avatar
P*P
2
我需要maintain两个thread safe incremental counter在不同的method里.
这个counter各自计各自的,所以可以同时分别update.但是每个counter自己是synchron
ized.
象下面这样子.
private int c1;
private int c2
public class method1 {
synchronized(this) {
c1++;
}
}
public class method2 {
synchronized(this) {
c2++;
}
}
我有两个问题
一是c1,c2都是private的,是不是本身就已经是thread-safe了,需要synchronized吗?
二是象上面的这段code,由于c1,c2都用的是this的lock,他们可以在同一时间update吗,
还是一个一个来
先谢谢了.
avatar
l*n
3
寒 这么恐怖的梦 真的假的?

【在 gm 的大作中提到】
: 昨晚梦到一个人,他的身体卷了起来,就像卷起的一套衣服一样。然后,他在墙里面滚
: 动。后来,我听人说那个人被捂住嘴勒死了然后被剥了皮。我吓了一跳,才意识到我看
: 到那个滚动的衣服卷就是那个被勒死的人鬼魂,也可能是他的穿着衣服的皮……后来貌
: 似醒了。感觉这个梦很怪异。

avatar
g*g
4

synchron
Is this supposed to be
public void method1 { ?
you are not declaring method1 as a class, are you?
private variable needs to be synced as long as multiple threads may
be changing it. c1/c2 won't be changed at the same time.

【在 P*P 的大作中提到】
: 我需要maintain两个thread safe incremental counter在不同的method里.
: 这个counter各自计各自的,所以可以同时分别update.但是每个counter自己是synchron
: ized.
: 象下面这样子.
: private int c1;
: private int c2
: public class method1 {
: synchronized(this) {
: c1++;
: }

avatar
m*y
5
你应该有一个以前很亲近的朋友,近来遭遇了什么不好的事。。。

【在 gm 的大作中提到】
: 昨晚梦到一个人,他的身体卷了起来,就像卷起的一套衣服一样。然后,他在墙里面滚
: 动。后来,我听人说那个人被捂住嘴勒死了然后被剥了皮。我吓了一跳,才意识到我看
: 到那个滚动的衣服卷就是那个被勒死的人鬼魂,也可能是他的穿着衣服的皮……后来貌
: 似醒了。感觉这个梦很怪异。

avatar
P*P
6
谢谢回答
我把问题简化了
其实我需要十来个counter
在一个class的不同的method里.
如果我想要counter们可以同时update,只能每个counter用各自的
lock了吗?

【在 g*****g 的大作中提到】
:
: synchron
: Is this supposed to be
: public void method1 { ?
: you are not declaring method1 as a class, are you?
: private variable needs to be synced as long as multiple threads may
: be changing it. c1/c2 won't be changed at the same time.

avatar
y*6
7
你心里太烦躁了,没睡安稳才会做恶梦。
avatar
g*g
8
如果你有两个不相干的counter,这么做会使性能变差一点,
但不会影响结果。
你也可以选择在同一类内monitor不同object, 比如每个counter
一个string。

【在 P*P 的大作中提到】
: 谢谢回答
: 我把问题简化了
: 其实我需要十来个counter
: 在一个class的不同的method里.
: 如果我想要counter们可以同时update,只能每个counter用各自的
: lock了吗?

avatar
a*n
9
很恐怖的梦
avatar
P*P
10
你的意思是说每个counter定义一个string as lock吗
比如这样子
private string mutex1="";
private string mutex2="";
synchronized(mutex1){
c1++;
}
synchronized(mutex2){
c2++;
}
多谢

【在 g*****g 的大作中提到】
: 如果你有两个不相干的counter,这么做会使性能变差一点,
: 但不会影响结果。
: 你也可以选择在同一类内monitor不同object, 比如每个counter
: 一个string。

avatar
c*t
11
No! Both mutex will points to the same object. Be very
careful about string resource (and number autoboxing).
Use
private Object mutex1 = new Object ();
private Object mutex2 = new Object ();
instead.

【在 P*P 的大作中提到】
: 你的意思是说每个counter定义一个string as lock吗
: 比如这样子
: private string mutex1="";
: private string mutex2="";
: synchronized(mutex1){
: c1++;
: }
: synchronized(mutex2){
: c2++;
: }

avatar
P*P
12
多谢!
如果十多个counter,每个都定义一个object as lock,有没有什么弊端?

【在 c*****t 的大作中提到】
: No! Both mutex will points to the same object. Be very
: careful about string resource (and number autoboxing).
: Use
: private Object mutex1 = new Object ();
: private Object mutex2 = new Object ();
: instead.

avatar
g*g
13
use new String();

【在 P*P 的大作中提到】
: 你的意思是说每个counter定义一个string as lock吗
: 比如这样子
: private string mutex1="";
: private string mutex2="";
: synchronized(mutex1){
: c1++;
: }
: synchronized(mutex2){
: c2++;
: }

avatar
c*t
14
Well, that depends on how fast each counter gets hit. If
there is no congestion, no problem.

【在 P*P 的大作中提到】
: 多谢!
: 如果十多个counter,每个都定义一个object as lock,有没有什么弊端?

avatar
P*P
15
明白了,多谢

【在 g*****g 的大作中提到】
: use new String();
avatar
P*P
16
如果hit还挺频繁的咋办,还有别的方法提高performance吗

【在 c*****t 的大作中提到】
: Well, that depends on how fast each counter gets hit. If
: there is no congestion, no problem.

avatar
c*t
17
There is a no locking approach which depends on atomic read / write
of 32-bit integers. For 64 bit numbers, might require 64-jvm
to be safe (but I have no idea).
Here is the basic idea:
class Counter
{
private final int[] m_counter;
public Counter (int numThreads)
{
m_counter = new int[numThreads];
}
public void incCount (int threadNum)
{
++m_counter[threadNum];
}
public int getCount ()
{
int count = 0;
for (int i = 0; i < m_counter.length; ++i

【在 P*P 的大作中提到】
: 如果hit还挺频繁的咋办,还有别的方法提高performance吗
avatar
P*P
18
多谢了,我仔细琢磨琢磨

【在 c*****t 的大作中提到】
: There is a no locking approach which depends on atomic read / write
: of 32-bit integers. For 64 bit numbers, might require 64-jvm
: to be safe (but I have no idea).
: Here is the basic idea:
: class Counter
: {
: private final int[] m_counter;
: public Counter (int numThreads)
: {
: m_counter = new int[numThreads];

avatar
m*t
19

Right. If all OP wants is to update the counters in multi-threads, there is
no need to synchronize as long as they are ints. Longs would be a different
story.
I'm not sure either, but I would always synchronize updates on longs,
because the same class file could be run in either 32-bit or 64-bit systems.

【在 c*****t 的大作中提到】
: There is a no locking approach which depends on atomic read / write
: of 32-bit integers. For 64 bit numbers, might require 64-jvm
: to be safe (but I have no idea).
: Here is the basic idea:
: class Counter
: {
: private final int[] m_counter;
: public Counter (int numThreads)
: {
: m_counter = new int[numThreads];

avatar
P*P
20
意思是说如果counter是int的话,我根本不用管,不用在update counter的时候synchron
ize吗

is
different
systems.

【在 m******t 的大作中提到】
:
: Right. If all OP wants is to update the counters in multi-threads, there is
: no need to synchronize as long as they are ints. Longs would be a different
: story.
: I'm not sure either, but I would always synchronize updates on longs,
: because the same class file could be run in either 32-bit or 64-bit systems.

avatar
c*t
21
No. Only works in the code I posted. In my code, each mini counter
is only updated by a single thread. This is the critical part.

synchron

【在 P*P 的大作中提到】
: 意思是说如果counter是int的话,我根本不用管,不用在update counter的时候synchron
: ize吗
:
: is
: different
: systems.

avatar
m*t
22

I might be missing something, just don't see why that has to be.

【在 c*****t 的大作中提到】
: No. Only works in the code I posted. In my code, each mini counter
: is only updated by a single thread. This is the critical part.
:
: synchron

avatar
c*t
23
++counter is not an atomic action and updates by two different
threads would cause problems. My code separate the updates.
Both you and I know this, but I was worried that PHP would
could have misunderstandings.

【在 m******t 的大作中提到】
:
: I might be missing something, just don't see why that has to be.

avatar
m*t
24

Argh, I see what you mean now - misreading code is one of my vices these
days it seems. 8-)

【在 c*****t 的大作中提到】
: ++counter is not an atomic action and updates by two different
: threads would cause problems. My code separate the updates.
: Both you and I know this, but I was worried that PHP would
: could have misunderstandings.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。