2023-06-14  阅读(1)
原文作者:求和的小熊猫 原文地址:https://blog.csdn.net/qq_38219153/article/details/108470258

Java 中线程的基础知识

线程的属性

  • ID: 线程的ID,由系统自动分配

  • Priority: 线程的优先级,若果不设置,将会和调用他的父线程等级相同

  • Name: 线程的名称,由系统自动创建,格式为“ Thread + 线程初始化数字”

  • Status: 线程的状态,初始化为 0,代表 new 状态
    上述属性在源码中的定义为:

    202306142125491271.png

202306142125501412.png

202306142125506683.png

线程优先级

Java 中线程的优先级范围为 1~10,是一个 int 类型的值
其中最小的优先级 (MIN_PRIORITY) 为 1
正常优先级 (NORM_PRIORITY) 为 5
最高优先级 (MAX_PRIORITY) 为 10
源码中定义的线程优先级

202306142125510974.png

线程的状态

Java 中的线程有 6 中状态

  • NEW:线程创建完毕,但是并未开始执行
  • RUNABLE: 线程正在 JVM 中运行
  • BLOCKED: 线程处于阻塞状态,并且等待另一个线程
  • WAITING: 线程正在等待另一个线程
  • TIMED_WAITING: 线程等待另一个线程一段时间
  • TERMINATED: 线程执行完毕

线程的状态在源码中定义为枚举类型

        public enum State {
            /**
             * Thread state for a thread which has not yet started.
             */
            NEW,
    
            /**
             * Thread state for a runnable thread.  A thread in the runnable
             * state is executing in the Java virtual machine but it may
             * be waiting for other resources from the operating system
             * such as processor.
             */
            RUNNABLE,
    
            /**
             * Thread state for a thread blocked waiting for a monitor lock.
             * A thread in the blocked state is waiting for a monitor lock
             * to enter a synchronized block/method or
             * reenter a synchronized block/method after calling
             * {@link Object#wait() Object.wait}.
             */
            BLOCKED,
    
            /**
             * Thread state for a waiting thread.
             * A thread is in the waiting state due to calling one of the
             * following methods:
             * <ul>
             *   <li>{@link Object#wait() Object.wait} with no timeout</li>
             *   <li>{@link #join() Thread.join} with no timeout</li>
             *   <li>{@link LockSupport#park() LockSupport.park}</li>
             * </ul>
             *
             * <p>A thread in the waiting state is waiting for another thread to
             * perform a particular action.
             *
             * For example, a thread that has called <tt>Object.wait()</tt>
             * on an object is waiting for another thread to call
             * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
             * that object. A thread that has called <tt>Thread.join()</tt>
             * is waiting for a specified thread to terminate.
             */
            WAITING,
    
            /**
             * Thread state for a waiting thread with a specified waiting time.
             * A thread is in the timed waiting state due to calling one of
             * the following methods with a specified positive waiting time:
             * <ul>
             *   <li>{@link #sleep Thread.sleep}</li>
             *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
             *   <li>{@link #join(long) Thread.join} with timeout</li>
             *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
             *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
             * </ul>
             */
            TIMED_WAITING,
    
            /**
             * Thread state for a terminated thread.
             * The thread has completed execution.
             */
            TERMINATED;
        }

线程的创建方式

Java 中创建线程的三种方式:

  • 继承 Thread 类
  • 实现 Runnable 接口
  • 实现 Callable 接口(并发)

继承 Thread 类方式创建线程

    package com.stu.edu.demo1;
    
    public class ThreadDemo1 extends Thread {
    	
    	public static void main(String[] args) {
    		ThreadDemo1 thread1 = new ThreadDemo1();
    		ThreadDemo1 thread2 = new ThreadDemo1();
    		thread1.setName("THREAD_ONE");
    		thread2.setName("THREAD_TWO");
    		thread1.start();
    		thread2.start();
    	}
    	
    	@Override
    	public void run() {
    		for(int i = 0; i < 1000; i++) {
    			System.out.println(Thread.currentThread().getName() + " ---------> " + i);
    		}
    	}
    }

实现 Runable 接口方式创建线程

    package com.stu.edu.demo1;
    
    public class ThreadDemo1 implements Runnable {
    	
    	public static void main(String[] args) {
    		ThreadDemo1 run1 = new ThreadDemo1();
    		ThreadDemo1 run2 = new ThreadDemo1();
    		Thread thread1 = new Thread(run1,"THREAD_ONE");
    		Thread thread2 = new Thread(run2,"THREAD_TWO");
    		thread1.start();
    		thread2.start();
    	}
    	
    	@Override
    	public void run() {
    		for(int i = 0; i < 1000; i++) {
    			System.out.println(Thread.currentThread().getName() + " ---------> " + i);
    		}
    	}
    }

在创建线程时调用

202306142125515975.png

实现 Callable 接口方式创建线程

    package com.stu.edu.demo1;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.FutureTask;
    
    public class ThreadDemo1 implements Callable {
    	
    	public static void main(String[] args) {
    		ThreadDemo1 call1 = new ThreadDemo1();
    		ThreadDemo1 call2 = new ThreadDemo1();
    		FutureTask<Object> thread1 = new FutureTask<>(call1);
    		FutureTask<Object> thread2 = new FutureTask<>(call2);
    		new Thread(thread1,"THREAD_ONE").start();
    		new Thread(thread2,"THREAD_TWO").start();
    	}
    	
    
    	@Override
    	public Object call() throws Exception {
    		for(int i = 0; i < 1000; i++) {
    			System.out.println(Thread.currentThread().getName() + " ---------> " + i);
    		}
    		return null;
    	}
    }

call接口代码图片

202306142125520466.png

FuthureTask 实现了 RunnableFuture 接口,RunableFuture 接口继承了 Runable 接口。

202306142125524507.png

202306142125529018.png


Java 面试宝典是大明哥全力打造的 Java 精品面试题,它是一份靠谱、强大、详细、经典的 Java 后端面试宝典。它不仅仅只是一道道面试题,而是一套完整的 Java 知识体系,一套你 Java 知识点的扫盲贴。

它的内容包括:

  • 大厂真题:Java 面试宝典里面的题目都是最近几年的高频的大厂面试真题。
  • 原创内容:Java 面试宝典内容全部都是大明哥原创,内容全面且通俗易懂,回答部分可以直接作为面试回答内容。
  • 持续更新:一次购买,永久有效。大明哥会持续更新 3+ 年,累计更新 1000+,宝典会不断迭代更新,保证最新、最全面。
  • 覆盖全面:本宝典累计更新 1000+,从 Java 入门到 Java 架构的高频面试题,实现 360° 全覆盖。
  • 不止面试:内容包含面试题解析、内容详解、知识扩展,它不仅仅只是一份面试题,更是一套完整的 Java 知识体系。
  • 宝典详情:https://www.yuque.com/chenssy/sike-java/xvlo920axlp7sf4k
  • 宝典总览:https://www.yuque.com/chenssy/sike-java/yogsehzntzgp4ly1
  • 宝典进展:https://www.yuque.com/chenssy/sike-java/en9ned7loo47z5aw

目前 Java 面试宝典累计更新 400+ 道,总字数 42w+。大明哥还在持续更新中,下图是大明哥在 2024-12 月份的更新情况:

想了解详情的小伙伴,扫描下面二维码加大明哥微信【daming091】咨询

同时,大明哥也整理一套目前市面最常见的热点面试题。微信搜[大明哥聊 Java]或扫描下方二维码关注大明哥的原创公众号[大明哥聊 Java] ,回复【面试题】 即可免费领取。

阅读全文