请教一个多线程lock机制的问题# Java - 爪哇娇娃
d*r
1 楼
请教一个多线程lock机制的问题
我有这样的一个method:
public void methodA(int i) {
Integer key = new Integer(i);
synchronized (key) {
.......... (codeXXX)
}
}
methodA将会被多线程调用。
但codeXXX部分的代码,我要求是对整数i这个参数synchronized的,
即:任何时间内,对同一个参数i,不可以同时执行代码段: codeXXX,
但对不同的整数i,则可以多线程同时执行。
请问我这样实现对不对?
如果不对,该如何实现?
多谢。
我有这样的一个method:
public void methodA(int i) {
Integer key = new Integer(i);
synchronized (key) {
.......... (codeXXX)
}
}
methodA将会被多线程调用。
但codeXXX部分的代码,我要求是对整数i这个参数synchronized的,
即:任何时间内,对同一个参数i,不可以同时执行代码段: codeXXX,
但对不同的整数i,则可以多线程同时执行。
请问我这样实现对不对?
如果不对,该如何实现?
多谢。