首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Java 多线程系列(1) —— 线程入门

Java 多线程系列(1) —— 线程入门

作者头像
求和小熊猫
发布2020-11-25 15:14:39
发布2020-11-25 15:14:39
4420
举报

Java 中线程的基础知识

线程的属性

  • ID: 线程的ID,由系统自动分配
  • Priority: 线程的优先级,若果不设置,将会和调用他的父线程等级相同
  • Name: 线程的名称,由系统自动创建,格式为“ Thread + 线程初始化数字”
  • Status: 线程的状态,初始化为 0,代表 new 状态 上述属性在源码中的定义为:

线程优先级

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

线程的状态

Java 中的线程有 6 中状态

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

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

代码语言:javascript
复制
    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 类方式创建线程
代码语言:javascript
复制
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 接口方式创建线程
代码语言:javascript
复制
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);
		}
	}
}

在创建线程时调用

实现 Callable 接口方式创建线程
代码语言:javascript
复制
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接口代码图片

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Java 中线程的基础知识
  • 线程的创建方式
    • 继承 Thread 类方式创建线程
    • 实现 Runable 接口方式创建线程
    • 实现 Callable 接口方式创建线程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档