【Java】Java多线程

线程相关概念

  • 一个进程至少有一个线程
  • 在Java中,一个进程至少有两个线程,一个是main线程,一个是垃圾回收线程
  • 守护线程:JVM会在只剩下守护线程的时候结束,也就是说所有非守护线程结束,JVM就会结束
    - 典型例子:main线程(非守护线程,不能设置成守护线程),垃圾回收线程(守护线程)
  • 线程的状态:NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED
  • 线程优先级:[1,10]. 优先级越高不代表一定优先于优先级低的线程执行,而是会比低的得到更多的CPU资源
  • 打印线程的相关信息
        System.out.println("getName: "+Thread.currentThread().getName());
        System.out.println("getContextClassLoader: "+Thread.currentThread().getContextClassLoader());
        System.out.println("getId: "+Thread.currentThread().getId());
        System.out.println("getPriority: "+Thread.currentThread().getPriority());
        System.out.println("getStackTrace: "+Thread.currentThread().getStackTrace());
        System.out.println("getState: "+Thread.currentThread().getState());
        System.out.println("getThreadGroup: "+Thread.currentThread().getThreadGroup());
        System.out.println("getUncaughtExceptionHandler: "+Thread.currentThread().getUncaughtExceptionHandler());
        System.out.println("getClass: "+Thread.currentThread().getClass());
        System.out.println("toString: "+Thread.currentThread().toString());
        System.out.println("isAlive: "+Thread.currentThread().isAlive());
        System.out.println("isDaemon: "+Thread.currentThread().isDaemon());
        System.out.println("isInterrupted: "+Thread.currentThread().isInterrupted());
        System.out.println("Thread: "+Thread.currentThread());

创建线程的四种方式

  • 继承Thread
  • Runnable
  • Callable
  • 线程池
posted @ 2021-07-27 22:12  拉布  阅读(27)  评论(0编辑  收藏  举报