• 多线程基础一之(线程的3种实现方式)


    实现线程的三种方式:

       (1)继承Thread类,重写Run方法 

    class MyThreadDemo extends Thread {
    @Override
    public void run() {
    System.out.println("This is my Thread!");
    }
    public static void main(String[] args) {
    MyThreadDemo thread = new MyThreadDemo();
    thread.start();
    }
    }

    (2)实现Runnable接口,重写Run方法
    class RunnableThread implements Runnable{

    @Override
    public void run() {
    System.out.println("This is my RunnableThread!");
    }
    public static void main(String[] args) {
    Thread thread = new Thread(new RunnableThread());
    thread.start();
    }
    }
    (3)实现callable接口,重写call()方法,并带返回值
    public class CallableDemo implements Callable<String> {

    public static void main(String[] args) throws Exception{

    ExecutorService executorService = Executors.newCachedThreadPool();
    CallableDemo callableDemo = new CallableDemo();
    Future<String> future = executorService.submit(callableDemo);
    //call未执行完
    //放一些其他业务逻辑的处理
    System.out.println(future.get());//阻塞
    executorService.shutdown();
    }
    @Override
    public String call() throws Exception {
    return "String"+1;
    }
    }
     
  • 相关阅读:
    Angular 双向数据绑定
    Angular 过滤器
    Angular 自定义指令传参
    润滑油 标号
    Oracle concat
    sqlldr load UTF8 error
    linux中shell变量$#,$@,$0,$1,$2的含义解释
    shell 执行结果赋给变量
    linux 如何显示一个文件的某几行(中间几行)
    linux shell date 用当天时间做备份文件名
  • 原文地址:https://www.cnblogs.com/flgb/p/9902655.html
Copyright © 2020-2023  润新知