用java实现一个行锁(RowLock)

本文介绍了一种使用Java实现的数据库行锁机制,通过wait/notify方法控制并发访问,确保同一行数据在多线程环境下的安全性。代码示例展示了如何创建JavaRowLock类,以及在多线程环境下对特定标识的数据行进行锁定和解锁的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 版本的数据库行锁,使用wait/notify实现,当然可以使用别的方式如Lock下的await/signal

需求使用java写一个类,这个类有一个lock(String identifier)方法,用于传入一个唯一的标识标识数据库的某一行或者某个集合的某一条记录,当上锁之后,如果不适用unLock(String identifier)方法解锁,那么用于就不能访问这一条数据

 

直接上代码:

package com.lyzx.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class JavaRowLock{
    private ConcurrentHashMap<String,Object> lockedMap = new ConcurrentHashMap();

    public synchronized void lock(String id) throws InterruptedException{
        while(lockedMap.containsKey(id)){
            wait();
        }
        lockedMap.put(id,"");
    }

    public synchronized void unLock(String id){
        if(lockedMap.containsKey(id)){
            lockedMap.remove(id);
            notifyAll();
        }
    }

    public static void main(String[] args){
        JavaRowLock lock = new JavaRowLock();

        List<Runnable> arr = new ArrayList<>();

        for(int i = 0;i<100;i++){
            arr.add(new A1(lock,"A"));
        }

        for(int i=0;i<arr.size();i++){
            new Thread(arr.get(i),"A"+(i+1)).start();
        }
        try{Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}
    }
}

class A1 implements Runnable{
    private JavaRowLock lock;
    private String id;
    
    public A1(JavaRowLock lock,String id){
        this.lock = lock;
        this.id = id;
    }

    @Override
    public void run(){
        String threadName = Thread.currentThread().getName();
        try{
            lock.lock(id);
            System.out.println("线程:"+threadName+" 为["+id+"]加锁");
            System.out.println("线程:"+threadName+" 执行业务代码");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            lock.unLock(id);
            System.out.println("线程:"+threadName+" 释放锁");
        }
    }
}

 

url:http://10.10.21.109/logs/?status=2&page=1&input=10.10.10.11 {"code": 1, "info": "success", "count": 6024, "current_page": 1, "total_page": 335, "this_page_first_number": 1, "data": [{"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:05", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:04", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:04", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:03", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:03", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:02", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:01", "status": 2, "retry": 1, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:01", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 16:31:01", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:05", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:04", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:04", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:03", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:02", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:02", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:01", "status": 2, "retry": 1, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:01", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}, {"host_name": "mesdb1", "host_id": 2278, "service_description": "Oracle争用检查", "ctime": "2025-07-17 13:11:01", "status": 2, "retry": 0, "output": "CRITICAL - select count(*) from v$session where event ='enq: tx - row lock contention' : 1", "manager": "undefined", "company": "Not Found", "tel": "undefined", "ip": "10.10.10.11"}]} 帮我写一个maven项目提前以上分页url内的数据到excel表里,表设置表头
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值