import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class test_04 { public static void main(String[] args) throws Exception{ //lock的positon和size的含义:position:从那个位置开始上锁,size:上锁范围 //1:验证position和size的含义: //method_01();
private static void method_01() throws Exception{
RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(2,5,true);
System.out.println("1:");
fileChannel.write(ByteBuffer.wrap("1".getBytes()));
System.out.println("2:");
fileChannel.write(ByteBuffer.wrap("1".getBytes()));
System.out.println("3:");
fileChannel.write(ByteBuffer.wrap("1".getBytes()));
System.out.println("3:");
fileChannel.write(ByteBuffer.wrap("1".getBytes()));
/*输出
1:
2:
3:
Exception in thread "main" java.io.IOException: 另一个程序已锁定文件的一部分,进程无法访问。
* */
}
//2:提前锁:当position大于filechannel的大小时,文件的position到size就已经上锁了,不管文件的大小有没有达到这个位置 //method_02();
private static void method_02() throws Exception{
//文件内容:123
RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(4,1,true);
fileChannel.write(ByteBuffer.wrap("a".getBytes()));
fileChannel.write(ByteBuffer.wrap("b".getBytes()));
fileChannel.write(ByteBuffer.wrap("c".getBytes()));
fileChannel.write(ByteBuffer.wrap("d".getBytes()));
fileChannel.write(ByteBuffer.wrap("e".getBytes()));
/*输出:Exception in thread "main" java.io.IOException: 另一个程序已锁定文件的一部分,进程无法访问。
文件内容:abcd
* */
}
//共享锁和独占锁之间的关系:共享和共享之间非互斥,共享和独占互斥,独占和共享互斥,独占和独占互斥 //1:共享和共享之间非互斥,lock锁时进程级别的,所以用,method_03()时,报错为:java.nio.channels.OverlappingFileLockException //已经上过锁了,同一个进程就不要再次对它进行上锁了 method_03();
private static void method_03() throws Exception{
Thread thread1 = new Thread("线程1"){
@Override
public void run() {
try {
RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,true);
Thread.sleep(Integer.MAX_VALUE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread2 = new Thread("线程2"){
@Override
public void run() {
try {
RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread1.start();
Thread.sleep(1000);
thread2.start();
}
//可以用test1和test2来进行验证: //1:共享和共享之间非互斥
public class test1 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,true);
Thread.sleep(Integer.MAX_VALUE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class test2 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
System.out.println("test2:begin");
fileChannel.lock(0,Long.MAX_VALUE,true);
System.out.println("拿到锁了");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//test2输出为:
/*
test2:begin
拿到锁了
*/
//2:共享和独占互斥
public class test1 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,true);
Thread.sleep(Integer.MAX_VALUE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class test2 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
System.out.println("test2:begin");
fileChannel.lock(0,Long.MAX_VALUE,false);
System.out.println("拿到锁了");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//test2输出:
/*
test2:begin
*/
//3:独占和共享互斥
public class test1 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,false);
Thread.sleep(Integer.MAX_VALUE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class test2 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
System.out.println("test2:begin");
fileChannel.lock(0,Long.MAX_VALUE,true);
System.out.println("拿到锁了");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//test2输出
/*
test2:begin
*/
//4:独占和独占互斥
public class test1 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
fileChannel.lock(0,Long.MAX_VALUE,false);
Thread.sleep(Integer.MAX_VALUE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class test2 {
public static void main(String[] args) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("C:\\Users\\10167\\Desktop\\test.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
System.out.println("test2:begin");
fileChannel.lock(0,Long.MAX_VALUE,false);
System.out.println("拿到锁了");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//test2输出
/*
test2:begin
*/
}
}