mysql并发查询怎么避免文件指针混乱?

2024-02-01  本文已影响0人  屎倒淋头还嚼便

应该是一个文件流对应一个文件指针,不是一个文件一个指针。指针属于文件流。
一个线程对应一个文件流不会读错,
多个线程对应一个文件流就会读错,多个线程共用一个文件指针会出错

mysql应该是一个连接对应一个线程,同一个连接中多个查询语句应该只能同步运行。

// RandomAccessFile详解,出自:
https://blog.csdn.net/qq_40100414/article/details/120179117

测试1:每一个线程new一个RandomAccessFile对象,文件指针没有出错

public class Raf {
    static int count = 0;
    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("C:\\Users\\lz\\Desktop\\rwd.txt");
        while(true) {
            new Thread(()->{ t(0,file); }).start(); 
            new Thread(()->{ t(1,file); }).start(); 
            new Thread(()->{ t(2,file); }).start(); 
            new Thread(()->{ t(3,file); }).start(); 
            new Thread(()->{ t(4,file); }).start(); 
            new Thread(()->{ t(5,file); }).start(); 
            new Thread(()->{ t(6,file); }).start(); 
            new Thread(()->{ t(7,file); }).start(); 
            new Thread(()->{ t(8,file); }).start(); 
            Thread.sleep(10);
        }
    }
    public static void t(int pointer,File file) {
        // 文件内容(文件指针设置为0,readByte()结果为文件第一个字符1,以此类推):123456789abcdefg
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rws");
            raf.seek(pointer); // 定位文件指针
            byte byte1;
            byte1 = raf.readByte(); // 读取指针后面的一个字节
            byte[] bs = new byte[1];
            bs[0] = byte1;
            pointer++;
//          System.out.println(pointer+":"+new String(bs));
            if(pointer != Integer.parseInt(new String(bs))) { // 如果不相等就表明出现了指针错误
                System.exit(0); // 实测没有出错
            }
            count++;
            if(count%100 == 0) {
                System.out.println("计数:"+count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试2:多个线程公用一个RandomAccessFile对象,文件指针混乱出错

public class Raf {
    static int count = 0;
    static int cuowu = 0;
    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("C:\\Users\\lz\\Desktop\\rwd.txt");
        RandomAccessFile raf = new RandomAccessFile(file, "rws");
        while(true) {
            new Thread(()->{ t(0,raf); }).start(); 
            new Thread(()->{ t(1,raf); }).start(); 
            new Thread(()->{ t(2,raf); }).start(); 
            new Thread(()->{ t(3,raf); }).start(); 
            new Thread(()->{ t(4,raf); }).start(); 
            new Thread(()->{ t(5,raf); }).start(); 
            new Thread(()->{ t(6,raf); }).start(); 
            new Thread(()->{ t(7,raf); }).start(); 
            new Thread(()->{ t(8,raf); }).start(); 
            Thread.sleep(1000);
        }
    }
    public static void t(int pointer,RandomAccessFile raf) {
        // 文件内容(文件指针设置为0,readByte()结果为文件第一个字符1,以此类推):123456789abcdefg
        try {
            raf.seek(pointer); // 定位文件指针
            byte byte1;
            byte1 = raf.readByte(); // 读取指针后面的一个字节
            byte[] bs = new byte[1];
            bs[0] = byte1;
            pointer++;
//          System.out.println(pointer+":"+new String(bs));
            if(pointer != Integer.parseInt(new String(bs))) { // 如果不相等就表明出现了指针错误
                cuowu++;
                System.out.println("cuowu:"+cuowu); // 实测会出错
                //System.exit(0);
            }
            count++;
            if(count%100 == 0) {
                System.out.println("计数:"+count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读