笔试常见题目

一、递归遍历路径下的文件

public class GetFile {

    public static void main(String[] args) {
        String path="D:/ss";
        getPath(path);
    }
     private static void getPath(String path){
        File file = new File(path);//读取路径下的文件
        File[] files = file.listFiles();
        if(files==null){
            return;
        }
        for (File file1 : files) {
            if(file1.isFile()){ //是文件,输出文件名
                System.out.println(file1.getPath());
            }else {	//否则调用getPath()方法递归获取文件夹下的文件
                getPath(file1.getPath());
            }
        }
    }
   

二、冒泡排序

public class Order {
    public static void main(String[] args) {

        int [] arry={12,32,45,21,2,6,22};
        int temp;
        for (int i = 0; i < arry.length-1; i++) {
            for (int j = 0; j < arry.length-i-1; j++) {
                if(arry[j]>arry[j+1]){//后一个数比前一个数大
                    temp=arry[j+1];
                    arry[j+1]=arry[j];
                    arry[j]=temp;//两者位置互换,依次比较,可以将最大值依次往后移动
                }
            }
        }
        for (int i : arry) {
            System.out.println(i);
        }
    }
}

三、单例模式

饿汉式
public class SingleDemo01 {
    public static void main(String[] args) {
        //饿汉式测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.equals(instance1));



    }

}
//单例模式  饿汉式(静态变量)
 class Singleton{
    //1.构造器私有化,外部无法new
    private Singleton(){
    }  
    //2.本类内部创建对象实例
    private final static Singleton instance =new Singleton();

    //3.对外提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }
}
懒汉式(线程不安全)
public class SingleDemo03 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance==instance1);
    }
}

//懒汉式(线程不安全)
class Singleton{
    private static Singleton instance;
    private Singleton(){
    }

    //提供一个静态的共有方法,当使用到该方法时,才会创建instance
    public static Singleton getInstance(){
        if (instance==null){//线程不安全,多个线程会进入判断在创建实例之前
            instance=new Singleton();
        }
        return instance;
    }

}
懒汉式(线程安全、同步方法、效率低)
public class SingleDemo04 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance==instance1);
    }
}
//懒汉式(线程安全,同步方法,效率低)
class Singleton{
    private static Singleton instance;
    private Singleton(){
    }
    //提供一个静态的共有方法,当使用到该方法时,才会创建instance,加入同步处理的代码,解决线程安全问题,每次都需要等待,效率低
    public static synchronized Singleton getInstance(){
        if (instance==null){
            instance=new Singleton();
        }
        return instance;
    }

}
懒汉式(双重检查,线程安全,效率高)
public class SingleDemo05 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance==instance1);
    }
}

//双重检查(线程安全,且效率高)
class Singleton{
    private static volatile Singleton instance;
    private Singleton(){}

    public static Singleton getInstance(){
        if(instance==null){//锁之前加一层判断,实例创建后不需要进入同步锁等待,直接返回,提高效率
            synchronized (Singleton.class){
                if (instance==null){
                    instance=new Singleton();
                }
            }
        }
        return instance;
    }
}

生产者、消费者

/**
 * 生产者、消费者
 */

public class A {
    public static void main(String[] args) {
        Data data = new Data();

        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {

                    data.increment();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();


        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();


    }

}
//等待,业务,通知
class Data{//数字资源类
private int num=0;

public synchronized void increment() throws InterruptedException {
    while(num!=0){
        //等待
        this.wait();
    }
    num++;
    System.out.println(Thread.currentThread().getName()+"---"+num);
    //通知其他线程,我+1完毕了
    this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
        while(num==0){
        //等待
            this.wait();
        }
        num--;
    System.out.println(Thread.currentThread().getName()+"---"+num);
        //通知其他线程,我-1完毕了
    this.notifyAll();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值