设置表锁标记,在操作前先检测此标记
设置记录锁标记,在操作前检测此标记public void synchronized run(){
..................
.................
wait();
}public void lock(){
...........
...........
unlockARecord(i);
notifyAll();
}
设置记录锁标记,在操作前检测此标记public void synchronized run(){
..................
.................
wait();
}public void lock(){
...........
...........
unlockARecord(i);
notifyAll();
}
private Vector recordHasBeenLocked;
private int recordLockNumber;
public LockInterface(String tableName,int recordCount){
table = new Table(tableName,recordCount);
recordHasBeenLocked = new Vector(10,3);
recordLockNumber = 0;
}
public void lockTable(){
while(true){
synchronized(table){
if(table.getWeatherLocked()==true){
try{
table.wait();
}catch(InterruptedException e){}
continue;
}
}
synchronized(recordHasBeenLocked){
if(recordLockNumber>0){
try{
recordHasBeenLocked.wait();
}catch(InterruptedException e){}
continue;
}
}
synchronized(table){
table.setWeatherLocked(true);
}
break;
}
}
public void unlockTable(){
synchronized(table){
table.setWeatherLocked(false);
notify();
}
}
public void lockRecord(int recordNumber){
while(true){
synchronized(table){
if(table.getWeatherLocked()){
try{
table.wait();
}catch(InterruptedException e){}
continue;
}
}
Record record = table.findRecordById(recordNumber);
synchronized(record){
if( record.getWeatherLocked() ){
try{
record.wait();
}catch(InterruptedException e){}
continue;
}
record.setWeatherLocked(true);
}
synchronized(recordHasBeenLocked){
recordHasBeenLocked.add(record);
recordLockNumber++;
}
break;
}
}
public void unlockRecord(int recordNumber){
Record record = table.findRecordById(recordNumber);
synchronized(record){
record.setWeatherLocked(false);
notify();
}
synchronized(recordHasBeenLocked){
recordHasBeenLocked.remove(record);
this.recordLockNumber--;
notify();
}
}
}class Table{
private boolean weatherLocked;
private String tableName = null;
private Vector vRecords;
public Table(String tableName,int recordCount){
this.weatherLocked = false;
this.tableName = tableName;
vRecords = new Vector(recordCount);
for(int i=0;i<recordCount;i++){
vRecords.add(new Record(i+1));
}
}
public boolean getWeatherLocked(){
return this.weatherLocked;
}
public void setWeatherLocked(boolean trueOrFalse){
this.weatherLocked = trueOrFalse;
}
public Record findRecordById(int recordNumber){
for(int i=0;i<vRecords.size();i++){
if( ((Record)vRecords.elementAt(i)).getRecordNumber()==recordNumber)
return (Record)vRecords.elementAt(i);
}
return null;
}
}class Record{
private int recordNumber;
private boolean weatherLocked;
public Record(int recordNumber){
this.recordNumber = recordNumber;
this.weatherLocked = false;
}
public int getRecordNumber(){
return this.recordNumber;
}
public void setWeatherLocked(boolean trueOrFalse){
this.weatherLocked = trueOrFalse;
}
public boolean getWeatherLocked(){
return this.weatherLocked;
}
}
操作对象放两把锁,TableLock和RecordLock
分别有getlock和freelock方法 但方法要synchronized,就可以了.更安全的做法是opetion()方法所在对象是唯一实例的,只能通过getLock()方法获取,(不能获得锁就返回null,在外面人为控制等待)
//用一个唯一实例的操作区对象Access对象包装synchronized 的getlock(),getRecordLock(int i),两个方法分别用你的条件检测锁定情况,这里用index 的i来指定记录;细节看你的程序应该就知道了。这是我一年前在一个公司设计服务器程序做的,应该是可以的.
//get lock
while(true)
{Access access=Access.getLock();// 或者 Access.getRecordLock(i);
if (table!=null) break;
sleep(300);
}
//action
access.operate();
//freelock
access.freeLock();// 或者 Access.freeRecordLock(i);