Redian新闻
>
Java concurrency的疑惑,难道我理解错了?
avatar
Java concurrency的疑惑,难道我理解错了?# Programming - 葵花宝典
L*s
1
说他们今年本来只打算找一个人,结果来了一堆牛人。还有个总统奖的。后来就招了三
个...
avatar
c*T
2
【 以下文字转载自 Working 讨论区 】
发信人: conTT (bulldozer), 信区: Working
标 题: 请教 H1B transfer
发信站: BBS 未名空间站 (Thu Sep 24 07:36:05 2009, 美东)
现在还在OPT期间,现在公司的H1B将在 10月1号生效。
问题:
1.如果现在立即跳槽,到10月一号前应该可以以OPT为新公司工作。但是10月1号后,如
果H1B的transfer还没有完成,是不是就必须停下来,直到H1B转到新的公司?
2.或者是不是只要在10月一号前辞职,就自动延续OPT,那么也就不必担心H1B
transfer的问题?
多谢解答。
avatar
z*l
3
父母刚通过签证,现准备机票事宜。可是真的很犹豫,不知道是该买单程,还是买半年
往返,还是1年往返。来回都是单程肯定能不划算,但最十拿九稳,半年往返看起来比
较合理,但按照9月份走3月份回,价格得将近1400美金,貌似也不便宜。一年往返看起
来最不牢靠,因为上次来就申请的延期,这次如果再申请,不知道是否还能被批准。到
底该怎么办啊,哎,都是钱给闹的。
avatar
d*g
4
小弟刚开始学,有一事不明。
如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized
还是会有问题。比如x,y两个线程同时读并且试图写一个变量v
Time 0
xyTime 1
x---(write v+1)--->v //v becomes 4 after write
y---(write v+1)--->v //failed, because v is locked by x
Time 2
y---(write v+1)--->v //v is still 4, because the value read by y is 3.
这样看来,虽然synchronized可以保护写不同时进行,但无非是推迟了y写入v的时间,
问题依然是存在的。
请问这个问题java有解决吗?
avatar
h*7
5
多招两个人很容易吗,不是每年的指标都是很早就定了的?
avatar
m*g
6
你到哪里要1400?
从网上买,上海/北京---NYC都不要那么多,我说的还是周末,直飞,半年往返。
到西海岸就更不用说了。
不要再找代理了。他们现在价格不低,尤其是从国内出发的。
avatar
z*e
7
这个学术上叫做读写锁,dirty read
你应该在数据库课上学过怎么搞transaction
java可以搞得跟db一样
在read的时候就lock,把read & write写入同一个method
然后synchronized这个method就好了,lock加在method所属的obj上
但是这还不是工业界的搞法
工业界的搞法是上vert.x, ejb, spring etc.
不要自己动手写concurrency
如果非要自己写,用java.util.concurrency
avatar
l*e
8
我刚定的1300多,直飞的,买个省心。现在机票不便宜。找代理吧。
avatar
g*g
9
Of course, AtomicInteger.

synchronized

【在 d*********g 的大作中提到】
: 小弟刚开始学,有一事不明。
: 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized
: 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v
: Time 0
: x: y: Time 1
: x---(write v+1)--->v //v becomes 4 after write
: y---(write v+1)--->v //failed, because v is locked by x
: Time 2

avatar
m*g
10
你从哪直飞要1300?
我刚买的,PVG-EWR, CO,双周末,半年往返,1201,- CITI信用卡5% = 1141.
另外送里程 13K。
网上直接买的,不需要代理,群发代理报价都在1200以上,大部分还需要银行转帐。不
受信用卡,更有些代理还牛皮哄哄。

【在 l*********e 的大作中提到】
: 我刚定的1300多,直飞的,买个省心。现在机票不便宜。找代理吧。
avatar
z*l
12
能给推荐个网站吗,据说从国内走就从国内买,可是我知道的像是expedia priceline
等都是美国网站啊,先行谢过

【在 m*****g 的大作中提到】
: 你到哪里要1400?
: 从网上买,上海/北京---NYC都不要那么多,我说的还是周末,直飞,半年往返。
: 到西海岸就更不用说了。
: 不要再找代理了。他们现在价格不低,尤其是从国内出发的。

avatar
w*z
13
synchronized 保证你写进去的value 是准确的。没有synchronized, 一个Java
statement 可以不是 atomic, 几个instructions完成, 中间可能会被插入其他thread
的instructions, 造成你要写的那块memory 被搞乱了。

synchronized

【在 d*********g 的大作中提到】
: 小弟刚开始学,有一事不明。
: 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized
: 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v
: Time 0
: x: y: Time 1
: x---(write v+1)--->v //v becomes 4 after write
: y---(write v+1)--->v //failed, because v is locked by x
: Time 2

avatar
m*i
14
在国内买比在美国买便宜
avatar
m*o
15
他这个情况直接用volatile就可以了,value change直接flush到main memory。

【在 g*****g 的大作中提到】
: Of course, AtomicInteger.
:
: synchronized

avatar
m*g
16
你查的为什么贵?
我刚再查 expedia, 10/15/2011 PVG---3/31/2012 ERW 牛娃可.所有税费合计 1201.20
刀.
双周末.周中还再便宜点.
我买的扣除信用卡折扣,合计人民币72XX元.还不算CO的13K点里程.

priceline

【在 z*****l 的大作中提到】
: 能给推荐个网站吗,据说从国内走就从国内买,可是我知道的像是expedia priceline
: 等都是美国网站啊,先行谢过

avatar
g*g
17
No, volatile cannot avoid race condition on add. It's fine on a boolean. And
you don't need to understand why.
At the end of the day, just use AtomicInteger and don't try to be super
smart. You'll never see the performance gain from here.

【在 m****o 的大作中提到】
: 他这个情况直接用volatile就可以了,value change直接flush到main memory。
avatar
m*g
18
Total price for this trip: $1,201.20
I have a coupon.
If you have a redemption code, enter it below or sign in to your account and
select the saved coupon you want to use. Only one coupon can be used per
booking.
Sign in to my account
I have a coupon.
I don't have a coupon.

Apply and reprice


Your flight could cost $1101 instead of $1201! Get up to $100 off when you'
re approved. See details >
1 Review the flight details
Sat 8-Oct-11
Shanghai (PVG)
Depart 3:45 pm
Terminal 2 to Newark (EWR)
Arrive 6:20 pm
Terminal C 7,373 mi
(11,866 km)
Duration: 14hr 35mn
CO
Flight: 86
----------------------------------------------------------------------------
----
3Economy/Coach Class , Boeing 777
----------------------------------------------------------------------------
----
Total distance: 7,373 mi (11,866 km)
Total duration: 14hr 35mn
Sat 31-Mar-12
Newark (EWR)
Depart 11:00 am
Terminal C to Shanghai (PVG)
Arrive 1:40 pm +1 day
Terminal 2 7,373 mi
(11,866 km)
Duration: 14hr 40mn
CO
Flight: 87
----------------------------------------------------------------------------
----
3Economy/Coach Class , Boeing 777
----------------------------------------------------------------------------
----
Total distance: 7,373 mi (11,866 km)
Total duration: 14hr 40mn
Tip: Flight terminals may change. Please confirm the terminal with the
airline before leaving for the airport.

Additional airline fees may apply at check-in Fees may be charged by
airlines for services such as preferred seat selection and baggage handling.
Please note that fees are determined by the airline you check in with and
may change at anytime.
See fees.

Add a rental car to this trip

No thanks, I don't need a rental car.
Yes, I would like to see available rental cars next.
2 Review the rules and restrictions
----------------------------------------------------------------------------
----
Tickets are nonrefundable. Ticket changes are not allowed.
Tickets are nontransferable and name changes are not allowed.
Please read important information regarding airline liability limitations.
Prices do not include baggage fees or other fees charged directly by the
airline.
Read an overview of all the rules and restrictions applicable to this fare.
Read the complete penalty rules for changes and cancellations applicable to
this fare.
3 Continue to checkout
----------------------------------------------------------------------------
----
Continue to car selection. I have read and accept the rules and
restrictions.
avatar
s*i
19
难道不是 Time2的时候, v 已经是 4了吗?

synchronized
[发表自未名空间手机版 - m.mitbbs.com]

【在 d*********g 的大作中提到】
: 小弟刚开始学,有一事不明。
: 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized
: 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v
: Time 0
: x: y: Time 1
: x---(write v+1)--->v //v becomes 4 after write
: y---(write v+1)--->v //failed, because v is locked by x
: Time 2

avatar
m*g
20
我在 iflychina 上群发的信息,PVG-EWR, 国内代理报价周中7700起,一般都是8500上下
.还有要10000以上的(大概碰运气,看有没有凯子).
现在网络这么发达,代理旅行社时代过去了.

【在 m**i 的大作中提到】
: 在国内买比在美国买便宜
avatar
w*z
21
古德霸是对的
http://stackoverflow.com/questions/9749746/what-is-the-differen

And

【在 g*****g 的大作中提到】
: No, volatile cannot avoid race condition on add. It's fine on a boolean. And
: you don't need to understand why.
: At the end of the day, just use AtomicInteger and don't try to be super
: smart. You'll never see the performance gain from here.

avatar
l*y
22
多查查多问问就是了
人家一没给时间二没给地点你就quote出来了。。。呵呵

【在 m*****g 的大作中提到】
: 我在 iflychina 上群发的信息,PVG-EWR, 国内代理报价周中7700起,一般都是8500上下
: .还有要10000以上的(大概碰运气,看有没有凯子).
: 现在网络这么发达,代理旅行社时代过去了.

avatar
c*e
23
赵老师,synchronization 和 concurrency 不是一回事。

【在 z****e 的大作中提到】
: 这个学术上叫做读写锁,dirty read
: 你应该在数据库课上学过怎么搞transaction
: java可以搞得跟db一样
: 在read的时候就lock,把read & write写入同一个method
: 然后synchronized这个method就好了,lock加在method所属的obj上
: 但是这还不是工业界的搞法
: 工业界的搞法是上vert.x, ejb, spring etc.
: 不要自己动手写concurrency
: 如果非要自己写,用java.util.concurrency

avatar
c*4
24
还是先买半年往返,然后到了美国如果签证延期了,在改期改成一年往返就行。我写了
一个帖子是关于往返机票半年改一年的,希望可以帮到你:)
http://www.mitbbs.com/article_t/Reunion/31435957.html
至于价格,当然是货比三家,哪便宜在哪买呀。
avatar
c*e
25
读写锁有多种做法。比如说,你在读写一个数据库的表。
1。 读的时候锁住整个表,不让别的thread读这个表。
写的时候锁住整个表,不让别的thread写这个表。
读的锁不能变成写的锁;但是,写的锁可以变成读的锁。
2。读的时候不锁。
写的时候锁住整个表,不让别的thread写这个表。
3。 读的时候不锁。
写的时候只锁住要写的这一个record(row),不让别的thread写这个record(row),但是
可以写其它的record(row).
第3种情况,读的时候不锁,读的可能是写之前的老数据(静态的数据,不是reference
),也可能是读的当前的数据(读的是reference,指针指的地址的内容)
所以,这2个是不同的
ConcurrentHashMap vs Synchronized HashMap

synchronized

【在 d*********g 的大作中提到】
: 小弟刚开始学,有一事不明。
: 如果用synchronized,可以保证一个对象不会被同时写入。但我觉得就算synchronized
: 还是会有问题。比如x,y两个线程同时读并且试图写一个变量v
: Time 0
: x: y: Time 1
: x---(write v+1)--->v //v becomes 4 after write
: y---(write v+1)--->v //failed, because v is locked by x
: Time 2

avatar
d*n
26
其实代理的价格都相差不大的,一两百的差距,服务态度很重要的,如果你买到便宜的
,如果改期啥的,不给你办里啥的,那才伤心哦,当下的市场经济环境下,价格都差不
多,比的就是服务和诚信!!!

【在 c*********4 的大作中提到】
: 还是先买半年往返,然后到了美国如果签证延期了,在改期改成一年往返就行。我写了
: 一个帖子是关于往返机票半年改一年的,希望可以帮到你:)
: http://www.mitbbs.com/article_t/Reunion/31435957.html
: 至于价格,当然是货比三家,哪便宜在哪买呀。

avatar
c*e
27
volatile只能保证可见,但是不保证atomic.

【在 m****o 的大作中提到】
: 他这个情况直接用volatile就可以了,value change直接flush到main memory。
avatar
m*g
28
因为有不少票贩子故意制造高价气氛,想浑水摸鱼一把.
我上面已经明确说了,周末,到美东,直飞.不到1200.
有人就是说不信.什么张三1500+,李四1300+.
我才举例上海-纽约.
现在从东部出发到上海/北京往返,不到900的,多了去了.
旅行社现在是能骗一个是一个,还什么不收信用卡,不就是想占信用卡佣金那点蝇头小利
吗?
发拉盛那些华人超市作风一样,完全是鼠目寸光.

【在 l****y 的大作中提到】
: 多查查多问问就是了
: 人家一没给时间二没给地点你就quote出来了。。。呵呵

avatar
m*o
29
To guarantee atomicity, yes you have to use some sort of atomic data
structure. AtomicInteger will do in this case, which essentially does this,
val v = new AtomicInteger(3)
var cv = v.get()
while (v.compareAndSet(cv, cv + 1)) {
cv = v.get()
}

And

【在 g*****g 的大作中提到】
: No, volatile cannot avoid race condition on add. It's fine on a boolean. And
: you don't need to understand why.
: At the end of the day, just use AtomicInteger and don't try to be super
: smart. You'll never see the performance gain from here.

avatar
D*y
30
不是旅行社决定不收信用卡阿。否则为啥AA的收卡,UA/CO不收阿

【在 m*****g 的大作中提到】
: 因为有不少票贩子故意制造高价气氛,想浑水摸鱼一把.
: 我上面已经明确说了,周末,到美东,直飞.不到1200.
: 有人就是说不信.什么张三1500+,李四1300+.
: 我才举例上海-纽约.
: 现在从东部出发到上海/北京往返,不到900的,多了去了.
: 旅行社现在是能骗一个是一个,还什么不收信用卡,不就是想占信用卡佣金那点蝇头小利
: 吗?
: 发拉盛那些华人超市作风一样,完全是鼠目寸光.

avatar
z*e
31
靠,你太烂了

【在 c*********e 的大作中提到】
: 赵老师,synchronization 和 concurrency 不是一回事。
avatar
l*e
32
是上海直飞亚特兰大的,亏了就亏点吧,我是2个小时内就搞定机票的,怕麻烦就只好
多付点钱。

【在 m*****g 的大作中提到】
: 你从哪直飞要1300?
: 我刚买的,PVG-EWR, CO,双周末,半年往返,1201,- CITI信用卡5% = 1141.
: 另外送里程 13K。
: 网上直接买的,不需要代理,群发代理报价都在1200以上,大部分还需要银行转帐。不
: 受信用卡,更有些代理还牛皮哄哄。

avatar
z*l
34
不到900多,怎么可能,你是在哪里看到的讯息,能否详细说说?

【在 m*****g 的大作中提到】
: 因为有不少票贩子故意制造高价气氛,想浑水摸鱼一把.
: 我上面已经明确说了,周末,到美东,直飞.不到1200.
: 有人就是说不信.什么张三1500+,李四1300+.
: 我才举例上海-纽约.
: 现在从东部出发到上海/北京往返,不到900的,多了去了.
: 旅行社现在是能骗一个是一个,还什么不收信用卡,不就是想占信用卡佣金那点蝇头小利
: 吗?
: 发拉盛那些华人超市作风一样,完全是鼠目寸光.

avatar
c*e
35
你说说,这2个有啥区别:
ConcurrentHashMap vs Synchronized HashMap

【在 z****e 的大作中提到】
: 靠,你太烂了
avatar
m*g
36
怎么不可能?
看下面例子: DC-PVG-DC, $842 包括所有税费.
flychina.com
当然,你要不差钱的主,当我没说.
US$842 per adult (taxes and fees included) United
E-Ticket
Depart Arrive Total duration: 17hr 23min
Sep 22 8:32 am Washington National Apt Sep 22 9:30 am Chicago OHare
United 607
Sep 22 10:23 am Chicago OHare Sep 23 1:55 pm Shanghai Pudong United 835

•Departing Washington National Apt (DCA) !

Depart Arrive Total duration: 17hr 17min
Oct 13 3:45 pm Shanghai Pudong Oct 13 6:20 pm Newark United 86
Oct 13 7:45 pm Newark Oct 13 9:02 pm Washington National Apt United 4207

•Arriving Washington National Apt (DCA) !
•United 86 is operated by Continental.
•United 4207 is operated by XE Expressjet A/L DBA CO Express.
•New Checked Baggage Limit for International Travel: Only 1 Free Bag
50 lbs or less, please visit the airline web site to check the details.

【在 z*****l 的大作中提到】
: 不到900多,怎么可能,你是在哪里看到的讯息,能否详细说说?
avatar
z*e
37

reference
Synchronized HashMap就是HashTable,除了api名字略有区别以外,早已经不用了
就用concurrenthashmap就好了,hashtable这都是上个世纪的玩意了
可以看出让你看那些thread safe的各种frameworks,还是一个都没看

【在 c*********e 的大作中提到】
: 读写锁有多种做法。比如说,你在读写一个数据库的表。
: 1。 读的时候锁住整个表,不让别的thread读这个表。
: 写的时候锁住整个表,不让别的thread写这个表。
: 读的锁不能变成写的锁;但是,写的锁可以变成读的锁。
: 2。读的时候不锁。
: 写的时候锁住整个表,不让别的thread写这个表。
: 3。 读的时候不锁。
: 写的时候只锁住要写的这一个record(row),不让别的thread写这个record(row),但是
: 可以写其它的record(row).
: 第3种情况,读的时候不锁,读的可能是写之前的老数据(静态的数据,不是reference

avatar
m*g
38
纽瓦克-上海直飞往返,$828,含所有税费.
Tuesday, October 18, 2011 -- Traveling to Shanghai Pudong, China
Newark NJ (EWR)
Depart Oct 18 11:00 am to Shanghai Pudong China (PVG)
Arrive Oct 19 1:40 pm Boeing 777-200/300
Economy/Coach Continental Airlines
Flight 87

Total duration: 14hr 40min

Tuesday, April 10, 2012 -- Traveling to Newark, NJ
Shanghai Pudong China (PVG)
Depart Apr 10 3:45 pm to Newark NJ (EWR)
Arrive Apr 10 6:20 pm Boeing 777-200/300
Economy/Coach Continental Airlines
Flight 86

Total duration: 14hr 35min

•New Checked Baggage Limit for International Travel: Only 1 Free Bag
50 lbs or less, please visit the airline web site to check the details.
Price Information
1 adult
Price: $828.19
Total price(taxes and fees included): USD
avatar
z*e
39

这两个区别不是太明显了吧?
hashmap vs hashtable
concurrenthashmap vs hashmap
随便狗,这两个是经典的面试问题

【在 c*********e 的大作中提到】
: 你说说,这2个有啥区别:
: ConcurrentHashMap vs Synchronized HashMap

avatar
i*t
40
哪里查的啊?

【在 m*****g 的大作中提到】
: 纽瓦克-上海直飞往返,$828,含所有税费.
: Tuesday, October 18, 2011 -- Traveling to Shanghai Pudong, China
: Newark NJ (EWR)
: Depart Oct 18 11:00 am to Shanghai Pudong China (PVG)
: Arrive Oct 19 1:40 pm Boeing 777-200/300
: Economy/Coach Continental Airlines
: Flight 87
:
: Total duration: 14hr 40min
:

avatar
c*e
41
拜托,我问的是
ConcurrentHashMap vs Synchronized HashMap

【在 z****e 的大作中提到】
:
: 这两个区别不是太明显了吧?
: hashmap vs hashtable
: concurrenthashmap vs hashmap
: 随便狗,这两个是经典的面试问题

avatar
m*g
42
flychina.com

【在 i******t 的大作中提到】
: 哪里查的啊?
avatar
z*e
43

靠,所以说你不行
Synchronized HashMap vs hashtable
这两个其实是一个东西,idea是一样的
Synchronized HashMap vs hashtable vs hashmap
这个应该是1.5时代最经典的面试题了
只要涉及到thread safe,一定问
还有arraylist vs vector

【在 c*********e 的大作中提到】
: 拜托,我问的是
: ConcurrentHashMap vs Synchronized HashMap

avatar
c*e
44
Synchronized HashMap:
Each method is synchronized using an object level lock. SO the get and
put methods on synchMap acquire a lock.
Locking the entire collection is a performance overhead. While one
thread holds on to the lock, no other thread can use the collection.
------------------
ConcurrentHashMap was introduced in JDK 5.
There is no locking at the object level,The locking is at a much finer
granularity. For a concurrentHashMap, the locks may be at a hashmap bucket
level.
The effect of lower level locking is that you can have concurrent
readers and writers whilch is not possible for synchronized collections.
This leads to much more scalability.
ConcurrentHashMap does not throw a ConcurrentModificationException if
one thread tries to modify it while another is iterating over it.
http://stackoverflow.com/questions/1291836/concurrenthashmap-vs

【在 z****e 的大作中提到】
:
: 靠,所以说你不行
: Synchronized HashMap vs hashtable
: 这两个其实是一个东西,idea是一样的
: Synchronized HashMap vs hashtable vs hashmap
: 这个应该是1.5时代最经典的面试题了
: 只要涉及到thread safe,一定问
: 还有arraylist vs vector

avatar
z*e
45

没错啊,你是不是没有认真看我在说什么啊?
hashtable就是Collections.synchronizedMap(hashmap)
这里有benchmark,自己看看是不是一个level的
http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchroniz
这里还有解释
http://stackoverflow.com/questions/8875680/difference-between-h
Collections.synchronizedMap(HashMap) == HashTable
这两个都是时代的产物,legacy了
现在要用,就用ConcurrentHashMap
这么简单道理,重复了两三遍,怎么就不懂呢?

【在 c*********e 的大作中提到】
: Synchronized HashMap:
: Each method is synchronized using an object level lock. SO the get and
: put methods on synchMap acquire a lock.
: Locking the entire collection is a performance overhead. While one
: thread holds on to the lock, no other thread can use the collection.
: ------------------
: ConcurrentHashMap was introduced in JDK 5.
: There is no locking at the object level,The locking is at a much finer
: granularity. For a concurrentHashMap, the locks may be at a hashmap bucket
: level.

avatar
c*e
46
我是要你把2个的区别写出来,不是要你给我link.

【在 z****e 的大作中提到】
:
: 没错啊,你是不是没有认真看我在说什么啊?
: hashtable就是Collections.synchronizedMap(hashmap)
: 这里有benchmark,自己看看是不是一个level的
: http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchroniz
: 这里还有解释
: http://stackoverflow.com/questions/8875680/difference-between-h
: Collections.synchronizedMap(HashMap) == HashTable
: 这两个都是时代的产物,legacy了
: 现在要用,就用ConcurrentHashMap

avatar
z*e
47

靠,你说什么区别呢?
我不是最后一句说了嘛
如果要用,就用java.util.concurrency
你不会不知道ConcurrentHashMap就是其中一个最常用的类吧?
你说的synchronizedhashmap其实就是hashtable
只不过提供了一种方式把hashmap -> hashtable而不用改动api
这样可以直接做wrapper,而不用改动源代码
但是这个仅仅是api level的保留,本质上还是hashtable
所以hashtable vs concurrenthashmap
你这个区别不懂吗?
我估计你根本就没看懂我在说什么,我说的是syncrhonized method
跟下面这个人说的基本上一样,不是用synchronised hashmap
是类似
public synchronized void myFunc() {
read, incre, write etc.
}
我压根没说hashtable,因为已经凹凸很久了
所以你来告诉我这个不是一回事,我也是醉了
哪来的自信认为我不懂这个区别?
发信人: zyy (无聊(自由泳,众议院,众议员,中医院)), 信区: Programming
标 题: Re: Java concurrency的疑惑,难道我理解错了?
发信站: BBS 未名空间站 (Mon Aug 10 14:09:40 2015, 美东)
Can you try putting "read/write" inside one synchronized block, no?
Like http://tutorials.jenkov.com/java-concurrency/synchronized.html

【在 c*********e 的大作中提到】
: 我是要你把2个的区别写出来,不是要你给我link.
avatar
s*l
48
结论是?
avatar
d*g
49
结论是?
avatar
z*e
50
把account单独提出来做成一个object
然后所有关于这个account的操作
无论是read,还是write,还是read+write
塞入这个object,然后声明所有的func为synchronized
就可以了
比如
public class Account{
private int balance;
public synchronized int check(){
return balance;
}
public synchronized void deposit(int cash){
this.balance += cash;
}
}
synchronized func 相当于synchronized(this){
...
}
public void synchronized func(){...}
等价于
public void func(){
synchronized(this){...}
}
syncrhonized意思就是对于后面这个object
所有的thread,无论做什么syncrhonized的操作
都是互斥的
因为这里只有一个balance
所以你直接syncrhonized(balance){...}也是可以的
这个本质上跟古德霸说的那个AtomicInteger是一样的
当然你可以直接用轮子,方便很多
avatar
z*e
51
虽然你这么搞可以
但是现实生活中,对于threads之间的并发操作
还是能绕开就绕开,一般关键数据,如果你需要存储起来的话
交给db去做,用transaction可以很好滴完成任务
只有一些时效性非常强的才会需要你在内存中直接把这个搞定
比如游戏,比如real time pvp
但是游戏也不是完全没有办法绕开
fp的做法就是将线程之间需要分享的状态全部搞成immutable
就是thread内部,你爱怎么搞怎么搞,但是thread之间,如果你需要通信
必需immutable,这样就不怕并发了,这种可以解决real time gaming的pvp问题
用command pattern,操作方只能发送command给游戏主循环的thread
所有的command全部搞成immutable,反正也不大,这样就不存在并发更改问题了
做到这一步已经能解决99%的问题了,连游戏都能解决了
剩下1%,我很怀疑你会不会用到
如果你真碰到了,也尽量用java.util.concurrency里面的类
比如古德霸说的AtomicInteger,还有AtomicDouble etc.
而不是自己动手synchronized,我反正是没有遇到过这种问题
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。