Java 배타적 잠금 구현에 대한 자세한 코드 설명
이 글에서는 배타적 잠금을 구현하기 위한 Java 프로그래밍 관련 내용을 주로 소개하고, 이 코드 잠금을 구현하는 데 필요한 기능과 작성자의 솔루션을 설명하며, 도움이 필요한 모든 사람이 참조할 수 있도록 디자인 소스 코드를 공유합니다.
1. 서문
특정 연도, 특정 월에 동료가 파일 단독 잠금 기능이 필요하다고 하더군요.
(1) 쓰기 작업은 배타적 속성입니다
(2) 동일한 프로세스에 적용 가능 멀티스레딩/여러 프로세스의 배타적 작업에도 적합
(3) 내결함성: 잠금을 획득한 프로세스가 충돌하더라도 정상적인 잠금 획득에 영향을 미치지 않습니다. 후속 프로세스
2. 솔루션
1. 원래 아이디어
Java 분야에서는 동일한 프로세스에서 멀티스레딩을 독점적으로 구현하는 것이 비교적 간단합니다. 예를 들어 스레드 동기화 변수를 사용하여 잠겨 있는지 여부를 나타낼 수 있습니다. 그러나 다양한 프로세스를 배타적으로 구현하는 것은 더 번거롭습니다. 기존 API를 사용하다 보니 자연스럽게 java.nio.channels.FileLock: 다음과 같은 생각이 들었습니다.
/** * @param file * @param strToWrite * @param append * @param lockTime 以毫秒为单位,该值只是方便模拟排他锁时使用,-1表示不考虑该字段 * @return */ public static boolean lockAndWrite(File file, String strToWrite, boolean append,int lockTime){ if(!file.exists()){ return false; } RandomAccessFile fis = null; FileChannel fileChannel = null; FileLock fl = null; long tsBegin = System.currentTimeMillis(); try { fis = new RandomAccessFile(file, "rw"); fileChannel = fis.getChannel(); fl = fileChannel.tryLock(); if(fl == null || !fl.isValid()){ return false; } log.info("threadId = {} lock success", Thread.currentThread()); // if append if(append){ long length = fis.length(); fis.seek(length); fis.writeUTF(strToWrite); //if not, clear the content , then write }else{ fis.setLength(0); fis.writeUTF(strToWrite); } long tsEnd = System.currentTimeMillis(); long totalCost = (tsEnd - tsBegin); if(totalCost < lockTime){ Thread.sleep(lockTime - totalCost); } } catch (Exception e) { log.error("RandomAccessFile error",e); return false; }finally{ if(fl != null){ try { fl.release(); } catch (IOException e) { e.printStackTrace(); } } if(fileChannel != null){ try { fileChannel.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; }
모든 것이 너무 아름답고 흠 잡을 데 없는 것 같습니다. 그래서 두 개의 테스트 시나리오 코드가 추가되었습니다:
(1) 동일한 프로세스에서 두 개의 스레드가 동시에 잠금을 위해 경쟁하며 가칭 테스트 프로그램 A. 예상 결과: 하나의 스레드가 잠금을 획득하지 못했습니다
(2) 두 개의 프로세스를 실행합니다. 즉, 두 개의 테스트 프로그램 A를 실행하고 결과를 기대합니다. 한 프로세스와 스레드는 잠금을 획득하고 다른 스레드는 잠금을 획득하지 못합니다
public static void main(String[] args) { new Thread("write-thread-1-lock"){ @Override public void run() { FileLockUtils.lockAndWrite(new File("/data/hello.txt"), "write-thread-1-lock" + System.currentTimeMillis(), false, 30 * 1000);} }.start(); new Thread("write-thread-2-lock"){ @Override public void run() { FileLockUtils.lockAndWrite(new File("/data/hello.txt"), "write-thread-2-lock" + System.currentTimeMillis(), false, 30 * 1000); } }.start(); }
2. 당신이 생각하는 것과는 다릅니다
위의 테스트 코드는 단일 프로세스 내에서 우리의 기대를 충족할 수 있습니다. 그러나 두 프로세스를 동시에 실행할 경우 Mac 환경(java8)에서는 두 번째 프로세스가 정상적으로 잠금을 획득할 수 있지만, Win7(java7)에서는 두 번째 프로세스가 잠금을 획득할 수 없습니다. 왜? TryLock은 독점적이지 않습니까?
사실 TryLock이 배타적이지 않다는 것은 아니지만, 채널.닫기에 문제가 있습니다. 공식 성명은 다음과 같습니다.
On some systems, closing a channel releases all locks held by the Java virtual machine on the underlying file regardless of whether the locks were acquired via that channel or via another channel open on the same file.It is strongly recommended that, within a program, a unique channel be used to acquire all locks on any given file.
그 이유는 일부 운영 체제에서는 채널을 닫으면 JVM이 발생하기 때문입니다. 모든 잠금을 해제합니다. 즉, 위의 두 번째 테스트 사례가 실패한 이유를 이해합니다. 첫 번째 프로세스의 두 번째 스레드가 잠금을 획득하는 데 실패한 후 모든 잠금이 해제되도록 하는 모든 잠금을 해제하는 모든 잠금을 유발하는 모든 두 번째 프로세스 잠금을 호출하기 때문입니다. 성공적으로 획득하게 됩니다.
진실을 찾기 위한 고된 여정 끝에 마침내 stackoverflow에서 Lucence의 NativeFSLock도 여러 프로세스에 의한 독점 작성이 필요하다는 내용의 게시물을 발견했습니다. 저자는 Lucence 4.10.4의 NativeFSLock 소스 코드를 참조합니다. 구체적인 표시 주소와 구체적인 획득 방법은 다음과 같습니다. NativeFSLock의 설계 아이디어는 다음과 같습니다.
(1) 각 잠금에는 로컬에 해당하는 파일이 있습니다.
(2) 로컬 정적 유형 스레드 안전 Set
(3) LOCK_HELD에 해당 파일 경로가 없으면 파일 채널을 TryLock할 수 있습니다.
public synchronized boolean obtain() throws IOException { if (lock != null) { // Our instance is already locked: return false; } // Ensure that lockDir exists and is a directory. if (!lockDir.exists()) { if (!lockDir.mkdirs()) throw new IOException("Cannot create directory: " + lockDir.getAbsolutePath()); } else if (!lockDir.isDirectory()) { // TODO: NoSuchDirectoryException instead? throw new IOException("Found regular file where directory expected: " + lockDir.getAbsolutePath()); } final String canonicalPath = path.getCanonicalPath(); // Make sure nobody else in-process has this lock held // already, and, mark it held if not: // This is a pretty crazy workaround for some documented // but yet awkward JVM behavior: // // On some systems, closing a channel releases all locks held by the // Java virtual machine on the underlying file // regardless of whether the locks were acquired via that channel or via // another channel open on the same file. // It is strongly recommended that, within a program, a unique channel // be used to acquire all locks on any given // file. // // This essentially means if we close "A" channel for a given file all // locks might be released... the odd part // is that we can't re-obtain the lock in the same JVM but from a // different process if that happens. Nevertheless // this is super trappy. See LUCENE-5738 boolean obtained = false; if (LOCK_HELD.add(canonicalPath)) { try { channel = FileChannel.open(path.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); try { lock = channel.tryLock(); obtained = lock != null; } catch (IOException | OverlappingFileLockException e) { // At least on OS X, we will sometimes get an // intermittent "Permission Denied" IOException, // which seems to simply mean "you failed to get // the lock". But other IOExceptions could be // "permanent" (eg, locking is not supported via // the filesystem). So, we record the failure // reason here; the timeout obtain (usually the // one calling us) will use this as "root cause" // if it fails to get the lock. failureReason = e; } } finally { if (obtained == false) { // not successful - clear up and move // out clearLockHeld(path); final FileChannel toClose = channel; channel = null; closeWhileHandlingException(toClose); } } } return obtained; }
요약
위 내용은 Java 배타적 잠금 구현에 대한 자세한 코드 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Java의 Weka 가이드. 여기에서는 소개, weka java 사용 방법, 플랫폼 유형 및 장점을 예제와 함께 설명합니다.

Java의 Smith Number 가이드. 여기서는 정의, Java에서 스미스 번호를 확인하는 방법에 대해 논의합니다. 코드 구현의 예.

이 기사에서는 가장 많이 묻는 Java Spring 면접 질문과 자세한 답변을 보관했습니다. 그래야 면접에 합격할 수 있습니다.

Java 8은 스트림 API를 소개하여 데이터 컬렉션을 처리하는 강력하고 표현적인 방법을 제공합니다. 그러나 스트림을 사용할 때 일반적인 질문은 다음과 같은 것입니다. 기존 루프는 조기 중단 또는 반환을 허용하지만 스트림의 Foreach 메소드는이 방법을 직접 지원하지 않습니다. 이 기사는 이유를 설명하고 스트림 처리 시스템에서 조기 종료를 구현하기위한 대체 방법을 탐색합니다. 추가 읽기 : Java Stream API 개선 스트림 foreach를 이해하십시오 Foreach 메소드는 스트림의 각 요소에서 하나의 작업을 수행하는 터미널 작동입니다. 디자인 의도입니다

Java의 TimeStamp to Date 안내. 여기서는 소개와 예제와 함께 Java에서 타임스탬프를 날짜로 변환하는 방법에 대해서도 설명합니다.

캡슐은 3 차원 기하학적 그림이며, 양쪽 끝에 실린더와 반구로 구성됩니다. 캡슐의 부피는 실린더의 부피와 양쪽 끝에 반구의 부피를 첨가하여 계산할 수 있습니다. 이 튜토리얼은 다른 방법을 사용하여 Java에서 주어진 캡슐의 부피를 계산하는 방법에 대해 논의합니다. 캡슐 볼륨 공식 캡슐 볼륨에 대한 공식은 다음과 같습니다. 캡슐 부피 = 원통형 볼륨 2 반구 볼륨 안에, R : 반구의 반경. H : 실린더의 높이 (반구 제외). 예 1 입력하다 반경 = 5 단위 높이 = 10 단위 산출 볼륨 = 1570.8 입방 단위 설명하다 공식을 사용하여 볼륨 계산 : 부피 = π × r2 × h (4

Java는 초보자와 숙련된 개발자 모두가 배울 수 있는 인기 있는 프로그래밍 언어입니다. 이 튜토리얼은 기본 개념부터 시작하여 고급 주제를 통해 진행됩니다. Java Development Kit를 설치한 후 간단한 "Hello, World!" 프로그램을 작성하여 프로그래밍을 연습할 수 있습니다. 코드를 이해한 후 명령 프롬프트를 사용하여 프로그램을 컴파일하고 실행하면 "Hello, World!"가 콘솔에 출력됩니다. Java를 배우면 프로그래밍 여정이 시작되고, 숙달이 깊어짐에 따라 더 복잡한 애플리케이션을 만들 수 있습니다.
