S**Y 发帖数: 136 | 1 我有2个database,
一个线程 有两句话 :
prog1
{
A; //访问数据库1
B; //访问数据库2
}
另外一个定期cron的线程也要访问一个数据科1,
我想让第一个线程里面A语句和B语句是atomic的,必须一起执行完了,第二个线程
才可以执行
感觉应该非常简单的,大概就几句,但是对java不熟。谁能说说是用什么?
包子谢 |
g**e 发帖数: 6127 | 2 pass a simple object to both threads, use synchronized keyword to lock this
object when you are accessing the DB.
or use reentrant lock (read/write lock maybe)
【在 S**Y 的大作中提到】 : 我有2个database, : 一个线程 有两句话 : : prog1 : { : A; //访问数据库1 : B; //访问数据库2 : } : 另外一个定期cron的线程也要访问一个数据科1, : 我想让第一个线程里面A语句和B语句是atomic的,必须一起执行完了,第二个线程 : 才可以执行
|
t***e 发帖数: 3601 | 3 Be careful if you have many users. Database A may get locked for too long.
There's no good solutions here. |
x***i 发帖数: 585 | 4 对 而且第1个先执行的话,在thread 1 末尾加notify(),在thread2 前加wait()。
this
【在 g**e 的大作中提到】 : pass a simple object to both threads, use synchronized keyword to lock this : object when you are accessing the DB. : or use reentrant lock (read/write lock maybe)
|