• 线程 相关


    守护线程:垃圾回收(GC)线程

    线程最大优先级10,最小优先级1,默认优先级5

    线程的执行状态:新建、就绪、运行、阻塞、结束

    start() 分配资源

    run() 执行具体的用户线程代码;不是由用户程序来调用的,当调用start()方法启动一个线程之后,只要线程获得了CPU执行时间,便进入run()方法体执行具体的用户线程

    run()为同步执行,失去了线程的意义

    线程创建的方法:

    一、继承Thread

    1. package com.crazymakercircle.multithread.basic.create;
    2. import personal.nien.javabook.util.Print;
    3. public class CreateDemo {
    4. public static final int MAX_TURN = 5;
    5. public static String getCurThreadName() {
    6. return Thread.currentThread().getName();
    7. }
    8. //线程的编号
    9. static int threadNo = 1;
    10. static class DemoThread extends Thread { //
    11. public DemoThread() {
    12. super("DemoThread-" + threadNo++); //
    13. }
    14. public void run() { //
    15. for (int i = 1; i < MAX_TURN; i++) {
    16. Print.cfo(getName() + ", 轮次:" + i);
    17. }
    18. Print.cfo(getName() + " 运行结束.");
    19. }
    20. }
    21. public static void main(String args[]) throws InterruptedException {
    22. Thread thread = null;
    23. //方法一:使用Thread子类创建和启动线程
    24. for (int i = 0; i < 2; i++) {
    25. thread = new DemoThread();
    26. thread.start();
    27. }
    28. Print.cfo(getCurThreadName() + " 运行结束.");
    29. }
    30. }

    二、实现Runnable 

    Runnable逻辑和数据更好的分离,面向对象,避免单继承带来的局限性

    1. package com.crazymakercircle.multithread.basic.create;
    2. // 省略import
    3. public class SalesDemo
    4. {
    5. public static final int MAX_AMOUNT = 5; //商品数量
    6. //商店商品类(销售线程类),一个商品一个销售线程,每个线程异步销售4
    7. static class StoreGoods extends Thread
    8. {
    9. StoreGoods(String name)
    10. {
    11. super(name);
    12. }
    13. private int goodsAmount = MAX_AMOUNT;
    14. public void run()
    15. {
    16. for (int i = 0; i <= MAX_AMOUNT; i++)
    17. {
    18. if (this.goodsAmount > 0)
    19. {
    20. Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
    21. + (--goodsAmount));
    22. sleepMilliSeconds(10);
    23. }
    24. }
    25. Print.cfo(getCurThreadName() + " 运行结束.");
    26. }
    27. }
    28. //商场商品类(target销售线程的目标类),一个商品最多销售4次,可以多人销售
    29. static class MallGoods implements Runnable
    30. {
    31. //多人销售可能导致数据出错,使用原子数据类型保障数据安全
    32. private AtomicInteger goodsAmount = new AtomicInteger(MAX_AMOUNT);
    33. public void run()
    34. {
    35. for (int i = 0; i <= MAX_AMOUNT; i++)
    36. {
    37. if (this.goodsAmount.get() > 0)
    38. {
    39. Print.cfo(getCurThreadName() + " 卖出一件,还剩:"
    40. + (goodsAmount.decrementAndGet()));
    41. sleepMilliSeconds(10);
    42. }
    43. }
    44. Print.cfo(getCurThreadName() + " 运行结束.");
    45. }
    46. }
    47. public static void main(String args[]) throws InterruptedException
    48. {
    49. Print.hint("商店版本的销售");
    50. for (int i = 1; i <= 2; i++)
    51. {
    52. Thread thread = null;
    53. thread = new StoreGoods("店员-" + i);
    54. thread.start();
    55. }
    56. Thread.sleep(1000);
    57. Print.hint("商场版本的销售");
    58. MallGoods mallGoods = new MallGoods();
    59. for (int i = 1; i <= 2; i++)
    60. {
    61. Thread thread = null;
    62. thread = new Thread(mallGoods, "商场销售员-" + i);
    63. thread.start();
    64. }
    65. Print.cfo(getCurThreadName() + " 运行结束.");
    66. }
    67. }

    三、实现Callable

    Callable有返回值

    1. /**
    2. * Copyright (c) 2022,TravelSky.
    3. * All Rights Reserved.
    4. * TravelSky CONFIDENTIAL
    5. *

    6. * Project Name:ldpCoreCompApp
    7. * Package Name:PACKAGE_NAME
    8. * File Name:Test.java
    9. * Date:2022年08月2022/8/2日 10:01:14
    10. */
    11. import java.util.concurrent.*;
    12. /**
    13. * ClassName: Test
    14. * Description: TODO
    15. * Date: 2022年08月2022/8/2日 10:01:14
    16. *
    17. *
    18. * @author lixin(邮箱)
    19. *
    20. * 修改记录
    21. * @version 产品版本信息 yyyy-mm-dd 姓名(邮箱) 修改信息
    22. *
    23. */
    24. public class Test {
    25. /**
    26. * 继承Thread
    27. */
    28. static class TestThread extends Thread{
    29. @Override
    30. public void run() {
    31. System.out.println("TestThread");
    32. }
    33. }
    34. /**
    35. * 实现Runnable
    36. */
    37. static class TestRunnable implements Runnable{
    38. @Override
    39. public void run() {
    40. System.out.println("TestRunnable");
    41. }
    42. }
    43. /**
    44. * 实现Callable
    45. */
    46. static class TestCallable implements Callable {
    47. @Override
    48. public String call() throws Exception {
    49. return "TestCallable";
    50. }
    51. }
    52. /**
    53. * 线程池创建
    54. */
    55. private static ExecutorService executorService = Executors.newFixedThreadPool(3);
    56. static class TestExecutor implements Callable {
    57. @Override
    58. public Long call() throws Exception {
    59. long startTime = System.currentTimeMillis();
    60. for (int i = 0; i < 5; i++){
    61. System.out.println(Thread.currentThread().getName()+" : "+i);
    62. Thread.sleep(100);
    63. }
    64. long used = System.currentTimeMillis() - startTime;
    65. return used;
    66. }
    67. }
    68. public static void main(String[] args) throws ExecutionException, InterruptedException {
    69. Thread thread = new TestThread();
    70. thread.start();
    71. Runnable runnable = new TestRunnable();
    72. Thread thread1 = new Thread(runnable);
    73. thread1.start();
    74. TestCallable testCallable = new TestCallable();
    75. FutureTask futureTask = new FutureTask(testCallable);
    76. Thread thread2 = new Thread(futureTask);
    77. thread2.start();
    78. System.out.println(futureTask.get());
    79. Future future = executorService.submit(new TestExecutor());
    80. System.out.println(future.get());
    81. }
    82. }

    四、线程池Executor

    submit() 接收有返回值和无返回值的入参,有返回值

    execute() 只接收无返回值的入参,无返回值

  • 相关阅读:
    算法训练营 - 贪心
    Python tkinter实现复刻Windows记事本UI和菜单的文本编辑器(一)
    计算机网络实验第五次 11月8日
    Linux:进程的本质和fork初识
    AndroidT(13) -- natvie LOG 输出的实现(三)
    项目管理工具的功能与帮助一览
    Nginx反向代理和负载均衡
    C++--位图和布隆过滤器
    基于Kafka和Elasticsearch构建实时站内搜索功能的实践
    Python——函数
  • 原文地址:https://blog.csdn.net/weixin_41139035/article/details/126101618