Java面试

Java

线程

  1. wait 、yield 和 sleep的区别?
  • 相同点:
    • 3个在Java中能够用来暂停线程的方法中
  • 不同点:
    • wait()和sleep()的关键的区别在于,wait()是用于线程间通信的,而sleep()是用于短时间暂停当前线程。
    • 当一个线程调用wait()方法的时候,会释放它锁持有的对象的管程和锁,但是调用sleep()方法的时候,不会释放他所持有的管程。
    • yield()方法上来,与wait()和sleep()方法有一些区别,它仅仅释放线程所占有的CPU资源,从而让其他线程有机会运行,但是并不能保证某个特定的线程能够获得CPU资源。谁能获得CPU完全取决于调度器,在有些情况下调用yield方法的线程甚至会再次得到CPU资源。所以,依赖于yield方法是不可靠的,它只能尽力而为。

Spring

事务

  1. spring 事务隔离级别

  2. RequestMapping 注解的作用和参数

  • 用于将任意HTTP 请求映射到控制器方法上,注解可以在控制器类上和控制器类中的方法上使用
  • 属性
    • value

      指定请求的实际地址,指定的地址可以是URI Template 模式

    • method

      指定请求的method类型, GET、POST、PUT、DELETE等;

    • consumes

      指定处理请求的 提交内容类型 (Content-Type),例如 application/json, text/html

    • produces

      指定 返回的内容类型 ,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

    • params

      指定request中必须包含某些参数值时,才让该方法处理。

    • headers

      指定request中必须包含某些指定的header值,才能让该方法处理请求。

  • RequestMapping 快捷方式
请求 组合注解 共享注解
GET @GetMapping @RequestMapping(method = RequestMethod.GET)
POST @PostMapping @RequestMapping(method = RequestMethod.POST)
PUT @PutMapping @RequestMapping(method = RequestMethod.PUT)
DELETE @DeleteMapping @RequestMapping(method = RequestMethod.DELETE)
PATCH @PatchMapping @RequestMapping(method = RequestMethod.PATCH)
Author: suce
Link: https://haoubox.cn/2023/03/12/Java面试/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.