NIO-DO Java 线上笔试(编程)题,蔚来汽车

NIO-DO Java 线上笔试(编程)题

1.使用二分查找的方式来定位某一元素

2.请用你熟悉的开发语言,完成如下题目:
输入:若干个集合,各集合中的元素不会重复
输出:求这些集合的笛卡尔积例如:
输入:N个集合(这里N=3) :(a,b)(x,y)(1,2,3)
输出: ((a,x,1), (a,x,2)…(b,y,3))
在保证正确性的情况下尽可能优化效率,同时注意代码风格

3.利用循环的方式实现我输入n 得到n对应的裴波拉契数字,裴波拉契举例:1 1 2 3 5 8 13 21 。。。。。。。

4.用Java编写一个会导致死锁的程序

5.Java写代码来解决生产者——消费者问题

6.在Java中Lock接口比synchronized块的优势是什么?你需要实现一个高效的缓存,它允许多个用户读,但只允许一个用户写,以此来保持它的完整性,你会怎样去实现它?

1.使用二分查找的方式来定位某一元素
public int search(List listA, int keyword) {
int startNo=0;
int endNo=listA.size()-1;
while (startNo< endNo) {
int middleNo = (startNo + endNo)>>>1;
if (keyword == listA.get(middle)) {
return middle;
} else if (keyword< listA.get(middle)) {
endNo = middle -1;
} else {
startNo = middle +1;
}
}
return -1;
}

4.用Java编写一个会导致死锁的程序
public class Entrance {
public static void main(String[] args) {
DeadLockThread deadlock1 = new DeadLockThread();
DeadLockThread deadlock2 = new DeadLockThread();
deadlock1.flag = true;
deadlock2.flag = false;
new Thread(deadlock1).start();
new Thread(deadlock2).start();
}
}

public class DeadLockThread implements Runnable {
private static final Object objectA = new Object();
private static final Object objectB = new Object();
public boolean flag;

@Override
public void run() {
    if (flag) {
        synchronized (objectA) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("flag=true && lock objectA");
            synchronized (objectB) {
                System.out.println("flag=true && lock objectB");
            }
        }
    } else {
        synchronized (objectB) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("flag=false && lock objectB");
            synchronized (objectA) {
                System.out.println("flag=false && lock objectA");
            }
        }
    }
}

}

5.Java写代码来解决生产者——消费者问题
public class ProducerConsumer {
private LinkedList storeHouse = new LinkedList();
private int MAX = 5;

public ProducerConsumer() {
}

public void start() {
    new Producer().start();
    new Comsumer().start();
}

class Producer extends Thread {
    public void run() {
        while (true) {
            synchronized (storeHouse) {
                try {
                    while (storeHouse.size() == MAX) {
                        System.out
                                .println("storeHouse is full , please wait");
                    storeHouse.wait();
                }
                Object newOb = new Object();
                if (storeHouse.add(newOb)) {
                    System.out
                            .println("Producer put a Object to storeHouse");
                    Thread.sleep((long) (Math.random() * 3000));
                    storeHouse.notify();
                }
            } catch (InterruptedException ie) {
                System.out.println("producer is interrupted!");
            }

        }
    }
}

}

class Comsumer extends Thread {
public void run() {
while (true) {
synchronized (storeHouse) {
try {
while (storeHouse.size() == 0) {
System.out
.println(“storeHouse is empty , please wait”);
storeHouse.wait();
}
storeHouse.removeLast();
System.out
.println(“Comsumer get a Object from storeHouse”);
Thread.sleep((long) (Math.random() * 3000));
storeHouse.notify();
} catch (InterruptedException ie) {
System.out.println(“Consumer is interrupted”);
}
}
}
}
}

public static void main(String[] args) throws Exception {
    ProducerConsumer pc = new ProducerConsumer();
    pc.start();
}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值