守护线程:垃圾回收(GC)线程
线程最大优先级10,最小优先级1,默认优先级5
线程的执行状态:新建、就绪、运行、阻塞、结束
start() 分配资源
run() 执行具体的用户线程代码;不是由用户程序来调用的,当调用start()方法启动一个线程之后,只要线程获得了CPU执行时间,便进入run()方法体执行具体的用户线程
run()为同步执行,失去了线程的意义
线程创建的方法:
一、继承Thread
- package com.crazymakercircle.multithread.basic.create;
- import personal.nien.javabook.util.Print;
- public class CreateDemo {
- public static final int MAX_TURN = 5;
- public static String getCurThreadName() {
- return Thread.currentThread().getName();
- }
- //线程的编号
- static int threadNo = 1;
- static class DemoThread extends Thread { //①
- public DemoThread() {
- super("DemoThread-" + threadNo++); //②
- }
- public void run() { //③
- for (int i = 1; i < MAX_TURN; i++) {
- Print.cfo(getName() + ", 轮次:" + i);
- }
- Print.cfo(getName() + " 运行结束.");
- }
- }
- public static void main(String args[]) throws InterruptedException {
- Thread thread = null;
- //方法一:使用Thread子类创建和启动线程
- for (int i = 0; i < 2; i++) {
- thread = new DemoThread();
- thread.start();
- }
- Print.cfo(getCurThreadName() + " 运行结束.");
- }
- }
二、实现Runnable
Runnable逻辑和数据更好的分离,面向对象,避免单继承带来的局限性
- package com.crazymakercircle.multithread.basic.create;
- // 省略import
- public class SalesDemo
- {
- public static final int MAX_AMOUNT = 5; //商品数量
- //商店商品类(销售线程类),一个商品一个销售线程,每个线程异步销售4次
- static class StoreGoods extends Thread
- {
- StoreGoods(String name)
- {
- super(name);
- }
- private int goodsAmount = MAX_AMOUNT;
- public void run()
- {
- for (int i = 0; i <= MAX_AMOUNT; i++)
- {
- if (this.goodsAmount > 0)
- {
- Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
- + (--goodsAmount));
- sleepMilliSeconds(10);
- }
- }
- Print.cfo(getCurThreadName() + " 运行结束.");
- }
- }
- //商场商品类(target销售线程的目标类),一个商品最多销售4次,可以多人销售
- static class MallGoods implements Runnable
- {
- //多人销售可能导致数据出错,使用原子数据类型保障数据安全
- private AtomicInteger goodsAmount = new AtomicInteger(MAX_AMOUNT);
- public void run()
- {
- for (int i = 0; i <= MAX_AMOUNT; i++)
- {
- if (this.goodsAmount.get() > 0)
- {
- Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
- + (goodsAmount.decrementAndGet()));
- sleepMilliSeconds(10);
- }
- }
- Print.cfo(getCurThreadName() + " 运行结束.");
- }
- }
- public static void main(String args[]) throws InterruptedException
- {
- Print.hint("商店版本的销售");
- for (int i = 1; i <= 2; i++)
- {
- Thread thread = null;
- thread = new StoreGoods("店员-" + i);
- thread.start();
- }
- Thread.sleep(1000);
- Print.hint("商场版本的销售");
- MallGoods mallGoods = new MallGoods();
- for (int i = 1; i <= 2; i++)
- {
- Thread thread = null;
- thread = new Thread(mallGoods, "商场销售员-" + i);
- thread.start();
- }
- Print.cfo(getCurThreadName() + " 运行结束.");
- }
- }
三、实现Callable
Callable有返回值
- /**
- * Copyright (c) 2022,TravelSky.
- * All Rights Reserved.
- * TravelSky CONFIDENTIAL
- *
- * Project Name:ldpCoreCompApp
- * Package Name:PACKAGE_NAME
- * File Name:Test.java
- * Date:2022年08月2022/8/2日 10:01:14
- */
-
- import java.util.concurrent.*;
-
- /**
- * ClassName: Test
- * Description: TODO
- * Date: 2022年08月2022/8/2日 10:01:14
- *
- *
- * @author lixin(邮箱)
- *
- * 修改记录
- * @version 产品版本信息 yyyy-mm-dd 姓名(邮箱) 修改信息
- *
- */
- public class Test {
- /**
- * 继承Thread
- */
- static class TestThread extends Thread{
-
- @Override
- public void run() {
- System.out.println("TestThread");
- }
- }
-
- /**
- * 实现Runnable
- */
- static class TestRunnable implements Runnable{
-
- @Override
- public void run() {
- System.out.println("TestRunnable");
- }
- }
-
- /**
- * 实现Callable
- */
- static class TestCallable implements Callable
{ -
- @Override
- public String call() throws Exception {
- return "TestCallable";
- }
- }
-
- /**
- * 线程池创建
- */
- private static ExecutorService executorService = Executors.newFixedThreadPool(3);
- static class TestExecutor implements Callable
{ -
- @Override
- public Long call() throws Exception {
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < 5; i++){
- System.out.println(Thread.currentThread().getName()+" : "+i);
- Thread.sleep(100);
- }
- long used = System.currentTimeMillis() - startTime;
- return used;
- }
- }
-
- public static void main(String[] args) throws ExecutionException, InterruptedException {
-
- Thread thread = new TestThread();
- thread.start();
-
- Runnable runnable = new TestRunnable();
- Thread thread1 = new Thread(runnable);
- thread1.start();
-
- TestCallable testCallable = new TestCallable();
- FutureTask
futureTask = new FutureTask(testCallable); - Thread thread2 = new Thread(futureTask);
- thread2.start();
- System.out.println(futureTask.get());
-
- Future future = executorService.submit(new TestExecutor());
- System.out.println(future.get());
- }
- }
四、线程池Executor
submit() 接收有返回值和无返回值的入参,有返回值
execute() 只接收无返回值的入参,无返回值