首页 > 其他分享 >线程的六种状态

线程的六种状态

时间:2023-12-02 16:25:47浏览次数:24  
标签:状态 Thread 六种 System 线程 println new public out

New:初始状态 (未调用start )刚刚创建完

Runnable:运行状态 执行start

Blocked:阻塞状态  

Waiting:等待状态 通过Join sleep等方法使线程在等待

Timed Waiting :计时等待状态   sleep join 等方法设置了参数

Terminated:终止状态,线程执行完毕

package org.example.test1;

public class StateThread {
    public static void test1(){//NEW
        Thread t1 = new Thread(()->{
            System.out.println("Thread1");
        });
        System.out.println(t1.getState());
    }
    public static void test2() throws InterruptedException {//Terminated
        Thread t1 = new Thread(()->{
            System.out.println("线程开始执行");
            System.out.println("线程结束执行");
        });
        t1.start();
        Thread.sleep(3000);
        System.out.println(t1.getState());
    }
    public static void test3(){//Runnable
        Thread t1 = new Thread(()->{

        });
        t1.start();
        System.out.println(t1.getState());
    }
    public static void test4() throws InterruptedException {//Blocked
        class Table{
            public synchronized void use(){
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        System.out.println(Thread.currentThread().getName());
        Table table = new Table();
        Thread student1 = new Thread(()->{
            table.use();
        },"s1");
        Thread student2 = new Thread(()->{
            table.use();
        },"s2");
        student1.start();
        Thread.sleep(1000);
        student2.start();
        Thread.sleep(1000);
        System.out.println(student2.getState());
    }
    public static void test5() throws InterruptedException {//Waiting
        class Table{
            public synchronized void use() throws InterruptedException {
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                System.out.println("忘记点餐了");
                wait();
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        Table table = new Table();
        Thread student1 = new Thread(()->{
            try {
                table.use();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"s1");
        student1.start();
        Thread.sleep(100);
        System.out.println(student1.getState());
    }
    public static void test6() throws InterruptedException {//Timed_Waiting
        class Table{
            public synchronized void use() throws InterruptedException {
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                System.out.println("忘记点餐了");
                wait(3000);
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        Table table = new Table();
        Thread student1 = new Thread(()->{
            try {
                table.use();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"s1");
        student1.start();
        Thread.sleep(100);
        System.out.println(student1.getState());
    }

    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        test6();
    }
}

标签:状态,Thread,六种,System,线程,println,new,public,out
From: https://www.cnblogs.com/lin513/p/17871747.html

相关文章

  • 学习笔记4:JavaSE & API(网络编程 & 多线程)
    1、java.net.Socket:(1)定义:Socket(套接字)封装了TCP协议的通讯细节,是的我们使用它可以与服务端建立网络链接,并通过它获取两个流(一个输入一个输出),然后使用这两个流的读写操作完成与服务端的数据交互。(2)方法getInputStream():获取输入流,返回值是InputStream的一个子类实例。ge......
  • 线程合并
    主要来说就是主线程等待子线程可以设置参数(1000)就是等待子线程一秒如果子线程中要执行3秒则还是主线程先执行完packageorg.example.test1;importjava.util.concurrent.TimeUnit;publicclassJoinThread{staticintvalue=1;publicstaticvoidmain(Stri......
  • C++多线程编程:利用线程提高程序并发性
    C++多线程编程:利用线程提高程序并发性引言在现代计算机系统中,程序的并发性已经变得越来越重要。多线程编程是一种利用计算机的多核处理器来提高程序性能的方法。C++是一种功能强大的编程语言,提供了丰富的多线程编程支持。本文将介绍如何利用C++多线程编程来提高程序的并发性。什么......
  • Java并发(十六)----线程八锁
    所谓的“线程八锁”其实就是看synchronized锁住的是哪个对象情况1:12或21都是有可能的,就看cpu先调度哪个线程@Slf4j(topic="c.Number")classNumber{  publicsynchronizedvoida(){    log.debug("1"); }  publicsynchronizedvoidb(){......
  • 查看进程线程的方法
    windows任务管理器可以查看进程和线程数,也可以用来杀死进程tasklist查看进程taskkill杀死进程netstat-ano|findstr端口号查看某个端口号下的进程taskkill/f/pid强制杀死某个进程linuxps-ef查看所有进程ps-fT-p<PID>查看某个进程(PID)的所有线程kill杀死进程......
  • Redis为什么是单线程及高并发的原因
    Redis的高并发和快速原因1.redis是基于内存的,内存的读写速度非常快2.redis是单线程的,省去了很多上下文切换线程的时间3.redis使用多路复用技术,可以外理并发的连接。非阻塞I0内部实现采用epol,采用了epol+自己实现的简单的事件框架。epol中的读、写、关闭、连接都转化成了事件,然后......
  • 进程,线程,协程
    1、进程进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位。每个进程都有自己的独立内存空间,不同进程通过进程间通信来通信。由于进程比较重量,占据独立的内存,所以上下文进程间的切换开销(栈、寄存器、虚拟内存、文件句柄等......
  • .net core 使用Task多线程执行任务,限制线程数量,并等待所有任务结束
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceDataService.ETL_ApiData{publicclassMultiTask{///<summary>///最大线程数量///</summa......
  • 【Nginx38】Nginx学习:SSL模块(二)错误状态码、变量及宝塔配置分析
    Nginx学习:SSL模块(二)错误状态码、变量及宝塔配置分析继续我们的SSL模块的学习。上回其实我们已经搭建起了一个HTTPS服务器了,只用了三个配置,其中一个是listen的参数,另外两个是指定密钥文件的地址,一个是crt文件,一个是key文件。今天我们将学习到的是SSL中的错误状态码、变......
  • springboot创建线程池
    1.线程池的配置:@ConfigurationpublicclassThreadPoolConfig{@Bean(name="myThreadPool")//告诉你这个线程池的名字publicThreadPoolTaskExecutorthreadPoolTaskExecutor(){ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor();ex......