zoukankan      html  css  js  c++  java
  • 实现线程的多种方式

    1.继承Thread

    public class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("继承Thread");
        }
    
        public static void main(String[] args) {
            Thread01 thread01 = new Thread01();
            thread01.start();
        }
    }

    2.实现Runnable

    public class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("实现Runnable");
        }
    
        public static void main(String[] args) {
            Thread thread = new Thread(new Runnable01());
            thread.start();
        }
    }
    

      

    3.实现Callable

    public class Callable01 implements Callable<String> {
        @Override
        public String call() throws Exception {
            return "实现 Callable";
        }
    
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            FutureTask task = new FutureTask(new Callable01());
            new Thread(task).start();
            System.out.printf("内容 "+task.get());
        }
    }
    

      源码截图

     

       

    线程的多种状态

    public enum State {
            /**
             * 初始状态,尚未启动
             */
            NEW,
    
            /**
             * 可运行状态,可随时运行
             */
            RUNNABLE,
    
            /**
             *  线程阻塞,
             */
            BLOCKED,
    
            /**
             * 等待状态
             */
            WAITING,
    
            /**
             * 指定时间等待*/
            TIMED_WAITING,
    
            /**
             * 线程已完成执行
             */
            TERMINATED;
        }

    xxxx

  • 相关阅读:
    基于网络服务器端的网口测试-软件
    JAVA中操作符的优先级
    逻辑操作符、位操作符号的忽略点
    Java配置环境变量、方法和原因
    Java中常用的字符编码-解析
    SVM
    C#与matlab混合编程
    Python 3 and MySQL
    PyMySQL Evaluation
    PYTHON3连接MYSQL数据库
  • 原文地址:https://www.cnblogs.com/huan30/p/12482952.html
Copyright © 2011-2022 走看看