7天掌握NIO和SOCKET,第四天,通道锁的position和size详解,共享锁和互斥锁之间的关系

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
*/
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值