什么是读写锁?请用java设计一个简单的读写锁
来源:百度知道 编辑:UC知道 时间:2024/09/21 17:53:03
package com;
import java.util.Date;
public class InterruptTest {
public static void main(String args []){
MyThread test=new MyThread();
test.start();
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
test.interrupt();
}
}
class MyThread extends Thread{
public void run(){
while(true){
System.out.println("---"+new Date()+"---");
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
return ;
}
}
}
}
一个读写锁支持多个线程同时访问一个对象,但是在同一时刻只有一个线程可以修改此对象,并且在访问进行时不能修改
RandomAccessFile fis = new RandomAccessFile("shm.lock","rw");
// 获得文件通道
FileChannel lockfc = fis.getChannel();
// 获得文件的独占锁,该方法不产生堵塞,立刻返回
FileLo