常见笔试题

这边准备列举一下某些常见的非算法的笔试题

多个线程打印abc

public class OtherTest {

    static int count = 0;
    static Object obj = new Object();

    private static void doWork(int num){
        for(int i= 0; i< 10; i++) {
            try {
                synchronized (obj) {
                    while (count % 3 != num) {
                        obj.wait();
                    }
                    System.out.println(Thread.currentThread().getName());
                    count ++;
                    obj.notifyAll();
                }
            } catch (Exception e){
                System.out.println(e);
            }
        }


    }


    public static void main(String [] args) {
        Thread a = new Thread(() -> doWork(0), "a");
        Thread b = new Thread(() -> doWork(1), "b");
        Thread c = new Thread(() -> doWork(2), "c");
        a.start();
        b.start();
        c.start();
    }
}

两个线程交替打印奇数偶数

// 奇偶数打印
public class Test2 {
    private static int count = 1;
    private static Object obj = new Object();

    public static void main (String [] args) {
        Thread t1 = new Thread(() -> doWork(1), "奇数");
        Thread t2 = new Thread(() -> doWork(0), "偶数");
        t1.start();
        t2.start();

    }

    private static void doWork(int num) {
        for(int i = 0; i < 10; i++) {
            synchronized (obj) {
                while (count % 2 != num) {
                    try {
                        obj.wait();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
                System.out.println(Thread.currentThread().getName() + count);
                count++;
                obj.notifyAll();

            }
        }
    }
}

双重校验锁的单例模式

public class Singleton {
    private static volatile Singleton singleton;

    private Singleton() {};

    public static Singleton getSingle() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

读取文件

package com.xiaomi.fm.storage.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * ClassName:    FileTest
 * Package:    com.xiaomi.fm.storage.service
 * Description:
 * Datetime:    2021/5/16   下午1:31
 * Author:   liuwei
 */
public class FileTest {

    public final static String  FILE_NAME = "/tmp/test.txt";

    public static void main(String[] args) throws IOException {
        // ReadFileByBytes(FILE_NAME);
        // ReadFileByArray(FILE_NAME);
        // readFileByChars(FILE_NAME);
        ReadFileByLines(FILE_NAME);
        System.out.println(1);
    }

    // 单个字节单个字节读取
    public static void ReadFileByBytes(String fileName) throws IOException {
        File file = new File(fileName);
        InputStream in = null;
        try {
            in = new FileInputStream(fileName);
            int tmp;
            while ((tmp = in.read()) != -1) {
                System.out.write(tmp);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            in.close();
        }
    }
    
    // 一次读取多个字节
    public static void ReadFileByArray(String fileName) throws IOException {
        File file = new File(fileName);
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte [] arr = new byte[1000];
            int tmp;
            while ((tmp = in.read(arr)) != -1) {
                System.out.write(arr, 0, tmp);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            in.close();
        }
        
    }

    // 按照字符来读
    public static void readFileByChars(String fileName) throws IOException {
        File file = new File(fileName);
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(file), "utf-8");
            int tmp;
            while((tmp = reader.read()) != -1) {
                System.out.write(tmp);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            reader.close();
        }
    }

    public static void ReadFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String str = null;
            while ((str = reader.readLine()) != null) {
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(常见笔试题)