zoukankan      html  css  js  c++  java
  • 创建线程及启动的几种方式

    创建线程及启动的几种方式

    public class ThreadNew {
        public static void main(String[] args) {
            new MyThread1().start();
    
            new Thread(new MyThread2()).start();
    
            FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
            new Thread(futureTask).start();
            try {
                Integer integer = futureTask.get();
                System.out.println(integer);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    //1.继承Thread类
    class MyThread1 extends Thread{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    }
    //2.实现Runnable接口
    class MyThread2 implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    }
    //3.实现Callable接口
    class MyThread3 implements Callable{
        @Override
        public Integer call() throws Exception {
            System.out.println(Thread.currentThread().getName());
            return 100;
        }
    }
    
    刚刚参加工作,很有很多不懂不会的,发现错误,欢迎指正,谢谢!
  • 相关阅读:
    SSM简单实现文件上传和下载
    Web发送邮件
    scala写算法-快排
    scala写算法-从后缀表达式构造
    scalajs_初体验
    scala写算法-用小根堆解决topK
    scala-Future和Promise
    python基础之函数
    python基础知识(五)
    python基础知识(四)
  • 原文地址:https://www.cnblogs.com/xd-study/p/13196701.html
Copyright © 2011-2022 走看看