21xrx.com
2025-03-24 07:50:17 Monday
文章检索 我的文章 写文章
Java线下面试必备:5道经典面试题
2023-06-11 16:25:12 深夜i     9     0
单例模式 懒汉式 线程安全

在Java开发领域,面试是一个必经之路。无论是初入行的应届毕业生还是有一定经验的技术人员,都需要经过各种形式的面试。为了帮助大家更好地准备Java面试,本文将介绍5道经典面试题,并附上代码案例,以帮助大家更好地理解和掌握。

1. 单例模式

单例模式是面试中最常见的问题之一。请编写一个线程安全的懒汉式单例模式,并解释为什么需要做线程安全处理。

代码案例:

public 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;
  }
}

2. 反射机制

请简要介绍Java反射机制,并编写一个使用反射机制调用有参构造方法的示例。

代码案例:

public class User {
  private String name;
  
  public User(String name)
    this.name = name;
  
  
  public String getName()
    return this.name;
  
}
public class ReflectionTest {
  public static void main(String[] args) throws Exception {
    Class clazz = Class.forName("User");
    Constructor constructor = clazz.getConstructor(String.class);
    User user = (User) constructor.newInstance("Jack");
    System.out.println(user.getName());
  }
}

关键词:反射机制、有参构造方法

3. 异常处理

请编写一个用户自定义异常类,并在示例中抛出该异常。

代码案例:

public class MyException extends Exception {
  public MyException(String message) {
    super(message);
  }
}
public class ExceptionTest {
  public static void main(String[] args) throws MyException {
    throw new MyException("This is a custom exception.");
  }
}

关键词:异常处理、用户自定义异常、抛出异常

4. 多线程

请编写一个多线程程序,在主线程中打印奇数,子线程中打印偶数,并保证结果输出正确。

代码案例:

public class NumberPrinter implements Runnable {
  private int num;
  private int max;
  
  public NumberPrinter(int num, int max)
    this.num = num;
    this.max = max;
  
  
  public void run() {
    while(num <= max) {
      synchronized(NumberPrinter.class) {
        System.out.println(Thread.currentThread().getName() + ": " + num);
        num += 2;
        NumberPrinter.class.notifyAll();
        try {
          NumberPrinter.class.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
public class ThreadTest {
  public static void main(String[] args) {
    NumberPrinter np = new NumberPrinter(1, 10);
    Thread t = new Thread(np);
    t.start();
    
    while(np.num <= np.max) {
      synchronized(NumberPrinter.class) {
        System.out.println(Thread.currentThread().getName() + ": " + np.num);
        np.num += 2;
        NumberPrinter.class.notifyAll();
        
        try {
          NumberPrinter.class.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

关键词:多线程、子线程、同步锁

5. Java集合

请简要介绍Java中的Map和List集合,并编写一个示例程序,演示Map集合的使用。

代码案例:

import java.util.HashMap;
import java.util.Map;
public class MapTest {
  public static void main(String[] args) {
    Map
  map = new HashMap<>();
 
    map.put(1, "one");
    map.put(2, "two");
    map.put(3, "three");
    
    for(Integer key : map.keySet())
      System.out.println(key + ": " + map.get(key));
  }
}

关键词:Java集合、Map、List

  
  

评论区