由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - synchronization for counters
相关主题
synchronization 锁住了什么?问一个基础问题
发现 synchronized 的一个问题请教:performance issue
新手问个multi-threading关于synchronized和volatile的问题请教:怎么把synchronize的method改成用thread 但thread safe呢?
Talk a little more about How to lock a filejava的volatile
请教一个多线程lock机制的问题Java concurrency的疑惑,难道我理解错了? (转载)
怎么synchronize时间捏synchronized method does lock the object that passed into the method as a parameter?
HashMap cache传递一个object reference,如何防止更改object?
Apply lock on a class.问个面试题, 谢谢
相关话题的讨论汇总
话题: counter话题: counters话题: private话题: int
进入Java版参与讨论
1 (共1页)
P*P
发帖数: 36
1
我需要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吗,
还是一个一个来
先谢谢了.
g*****g
发帖数: 34805
2

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++;
: }

P*P
发帖数: 36
3
谢谢回答
我把问题简化了
其实我需要十来个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.

g*****g
发帖数: 34805
4
如果你有两个不相干的counter,这么做会使性能变差一点,
但不会影响结果。
你也可以选择在同一类内monitor不同object, 比如每个counter
一个string。

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

P*P
发帖数: 36
5
你的意思是说每个counter定义一个string as lock吗
比如这样子
private string mutex1="";
private string mutex2="";
synchronized(mutex1){
c1++;
}
synchronized(mutex2){
c2++;
}
多谢

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

c*****t
发帖数: 1879
6
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++;
: }

P*P
发帖数: 36
7
多谢!
如果十多个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.

g*****g
发帖数: 34805
8
use new String();

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

c*****t
发帖数: 1879
9
Well, that depends on how fast each counter gets hit. If
there is no congestion, no problem.

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

P*P
发帖数: 36
10
明白了,多谢

【在 g*****g 的大作中提到】
: use new String();
相关主题
怎么synchronize时间捏问一个基础问题
HashMap cache请教:performance issue
Apply lock on a class.请教:怎么把synchronize的method改成用thread 但thread safe呢?
进入Java版参与讨论
P*P
发帖数: 36
11
如果hit还挺频繁的咋办,还有别的方法提高performance吗

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

c*****t
发帖数: 1879
12
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吗
P*P
发帖数: 36
13
多谢了,我仔细琢磨琢磨

【在 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];

m******t
发帖数: 2416
14

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];

P*P
发帖数: 36
15
意思是说如果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.

c*****t
发帖数: 1879
16
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.

m******t
发帖数: 2416
17

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

c*****t
发帖数: 1879
18
++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.

m******t
发帖数: 2416
19

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.

1 (共1页)
进入Java版参与讨论
相关主题
问个面试题, 谢谢请教一个多线程lock机制的问题
一个关于generics的问题怎么synchronize时间捏
CC150 16.6答案是不是有问题? (转载)HashMap cache
Re: Question: Java SynchronizationApply lock on a class.
synchronization 锁住了什么?问一个基础问题
发现 synchronized 的一个问题请教:performance issue
新手问个multi-threading关于synchronized和volatile的问题请教:怎么把synchronize的method改成用thread 但thread safe呢?
Talk a little more about How to lock a filejava的volatile
相关话题的讨论汇总
话题: counter话题: counters话题: private话题: int