常見筆試題

這邊準(zhǔn)備列舉一下某些常見的非算法的筆試題

多個線程打印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();
    }
}

兩個線程交替打印奇數(shù)偶數(shù)

// 奇偶數(shù)打印
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), "奇數(shù)");
        Thread t2 = new Thread(() -> doWork(0), "偶數(shù)");
        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);
    }

    // 單個字節(jié)單個字節(jié)讀取
    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();
        }
    }
    
    // 一次讀取多個字節(jié)
    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();
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容