阿里测开一面面经

1.自我介绍

2.分享自己做项目的过程

3.淘宝一元秒杀的场景的测试用例。

追问:如果有一亿个人,高并发你应该怎么测试。

追问:怎么保证抢购产品的库存,比如只有一百件,但是有1万个人抢?
追问:用到什么数据结构存储请求呢

4.jvm内存分区+垃圾回收过程+新生代和老年代

5.并行和并发的区别

6.hashmap底层理解

7.线程的五个状态
追问:线程池的使用

追问: 新建Executor的线程用什么

追问:wait()和sleep()的区别,父类是什么

追问:sychornized和lock的区别

追问: notify()和wait()底层实现。

8..分享自己最值得骄傲的事情,项目方面+大学生活方面

9.数据库的存储引擎区别。

10.String s=new String("abc")的存储位置,s是什么。

手撕代码:

//评测题目1:
//使用两个不同的线程将会共用一个 HelloWorld 实例。
//其中一个线程将会调用 hello() 方法,另一个线程将会调用 word() 方法。
//请设计修改程序,以确保 "HelloWorld" 被输出 n 次。
//示例 1:
//输入: n = 1
//输出: "HelloWorld"
//解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
//示例 2:
//输入: n = 2
//输出: "HelloWorldHelloWorld"
//解释: "HelloWorld" 将被输出两次。

import java.util.HashMap;
import java.util.concurrent.Semaphore;

public class HelloWorld {
    private int n;
    Semaphore semaphore;
    volatile boolean flag=false;//实现线程可见性,禁止重排序
    public static void main(String[] args) {
        HelloWorld helloWorld=new HelloWorld(5);//模拟打印五个
        Thread t1=new Thread(helloWorld::hello);//lambda表达式进行注入
        Thread t2=new Thread(helloWorld::world);
        t1.start();
        t2.start();
    }
    public  HelloWorld(int n){
        this.n = n;
        semaphore=new Semaphore(1);//设置信号量为1个,相当于独占资源
    }

    public void hello() {
        for (int i = 0; i < n; i++) {
          //通过flag进行判断,这里用一个死循环等待,保证执行的顺序,防止执行顺序错误,。
            while (flag){

            }
            try {
                semaphore.acquire();
            }catch (InterruptedException e) {//捕获异常

            }
            System.out.print("Hello");
            semaphore.release();
            flag=!flag;
        }
    }

    public void world() {
        for (int i = 0; i < n; i++) {
            while (!flag){

            }
            try {
                semaphore.acquire();
            }catch (InterruptedException e) {

            }
            System.out.println("World");
            semaphore.release();
            flag=!flag;
        }
    }
}

//评测题目2: 
//含有 x + 1 个整数的数组 nums,其数字都在 1 到 x 之间(包括 1 和 x),
//假设只有一个重复的数字,请找出这个重复的数。
//示例 1:
//输入: [1,3,4,2,2]   输出: 2
//示例 2:
//输入: [3,1,3,4,2]   输出: 3
//说明:
//不能改变原来的数组(假设数组只读)。
//只能使用额外的 O(1) 的空间。
//时间复杂度小于 O(n2) 。


public class Solution {
    public static void main(String[] args) {
        int[] nums=new int[]{1,3,4,2,2};//模拟的数组
        System.out.println(findDup(nums));
    }
    public static  int findDup(int[] nums) {
      //因为限制不能用额外的空间,所以只能二分找,但是牺牲了时间复杂性
      //如果不限制空间可以用hashmap进行判断,只需要O(n)
        int len=nums.length;
        int i=0,j=len-1;
        int mid=0;
        while(i<=j){//相当于二分找值
             mid=i+(j-i)/2;
            int flag=cat(nums,mid);//计数
            if(flag>mid)j=mid-1;
            else i=mid+1;
        }
        return i;
    }
  //计算当前比当前值大的数量
    public static int cat(int[] nums,int mid){
        int flag=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]<=mid)flag++;
        }
        return  flag;
    }

}


//评测题目3实现一个单例模式
/*
阿里笔试
 */


//sychronized实现
public class Singlton {
    public volatile Singlton singlton;//禁止指令重排序,并且实现可见性
    public Singlton(){
        
    }
    //外届通过此函数获取单例
    public Singlton getSinglton(){ 
        if(singlton==null)
            synchronized (this){
              //再这里上锁主要是为了防止两个线程同时进入if语句内代码块的情况,所以需要在内部再判断是否为null
                if(singlton==null){
                    singlton=new Singlton();
                }
            }
       
        return  singlton;
    }
}

//静态内部类实现
public class Singlton1 {
    public Singlton1(){

    }
    static class Singltonstatic{
      //这里主要利用静态内部类的特点,初始化为一次调用getSinglton1()的时候,在没有用过的单例时,不会去初始化
      //这样可以节省的一定的空间
        private static final Singlton1 singlton1=new Singlton1();
    }
    public static Singlton1 getSinglton1(){
        return Singltonstatic.singlton1;
    }
}


 

发布了55 篇原创文章 · 获赞 17 · 访问量 4980

猜你喜欢

转载自blog.csdn.net/weixin_43698704/article/details/105470409