JAVA创建线程的三种方法及创建线程的简写方法、简单写法

本文详细介绍了Java中创建线程的三种方式:实现Runnable接口、继承Thread类以及使用Callable和Future。通过实例展示了每种方法的实现过程和执行效果,包括线程并发的输出情况。同时,代码示例中包含了简写形式和正常写法的对比。
摘要由CSDN通过智能技术生成
JAVA创建线程的三种方法及创建线程的简写方法、简单写法
1、通过实现 Runnable 接口;
  • 简写形式:

public class TestThread {

    public static void main(String[] args) {
    
        //第一种方式的简写方式,先创建Runnable对象,创建线程对象时,Runnable对象作为参数
        Runnable r1 = () -> {

            String threadName = Thread.currentThread().getName();
            System.out.println("Running thread: "+threadName);
            for (int i = 0; i < 7; i++) {

                System.out.println("Thread name: "+threadName + ", " + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        Runnable r2 = () -> {

            String threadName = Thread.currentThread().getName();
            System.out.println("Running thread: "+threadName);
            for (int i = 0; i < 7; i++) {

                System.out.println("Thread name: "+threadName + ", " + i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();

    }

}


//执行结果如下:
Running thread: Thread-0
Thread name: Thread-0, 0
Running thread: Thread-1
Thread name: Thread-1, 0
Thread name: Thread-1, 1
Thread name: Thread-0, 1
Thread name: Thread-0, 2
Thread name: Thread-1, 2
Thread name: Thread-0, 3
Thread name: Thread-1, 3
Thread name: Thread-0, 4
Thread name: Thread-1, 4
Thread name: Thread-1, 5
Thread name: Thread-0, 5
Thread name: Thread-0, 6
Thread name: Thread-1, 6

Process finished with exit code 0
  • 正常写法:

/*
* 实现Runnable接口来创建线程,并重写run()方法
* */
public class RunnableDemo implements Runnable{

    private Thread t;
    private String threadName;

    public RunnableDemo(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {

        System.out.println("Running thread: "+threadName);
        for (int i = 0; i < 7; i++) {

            System.out.println("Thread name: "+threadName + ", " + i);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void start(){
        System.out.println("Starting thread: "+threadName);
        if (t==null){
            t = new Thread(this, threadName);
            t.start();
        }
    }

}

/*
* 对应测试类如下:
* */

public class TestThread {

    public static void main(String[] args) {

        RunnableDemo demo1 = new RunnableDemo("Thread-1");
        demo1.start();
        RunnableDemo demo2 = new RunnableDemo("Thread-2");
        demo2.start();

    }

}

//执行结果如下:
Starting thread: Thread-1
Starting thread: Thread-2
Running thread: Thread-1
Running thread: Thread-2
Thread name: Thread-1, 0
Thread name: Thread-2, 0
Thread name: Thread-2, 1
Thread name: Thread-1, 1
Thread name: Thread-2, 2
Thread name: Thread-1, 2
Thread name: Thread-1, 3
Thread name: Thread-2, 3
Thread name: Thread-1, 4
Thread name: Thread-2, 4
Thread name: Thread-1, 5
Thread name: Thread-2, 5
Thread name: Thread-1, 6
Thread name: Thread-2, 6

Process finished with exit code 0
2、通过继承 Thread 类本身;
package com.example.utils.thread;

public class ExtendsThread extends Thread{

    private Thread t;
    private String threadName;

    public ExtendsThread(String threadName) {
        this.threadName = threadName;
    }

    public void run(){
        System.out.println("Running thread: "+threadName);
        for (int i = 0; i < 7; i++) {

            System.out.println("Thread name: "+threadName + ", " + i);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void start(){
        System.out.println("Starting Thread: "+threadName);
        if(t==null){
            t = new Thread(this, threadName);
            t.start();
        }
    }
}


/*
* 对应测试类如下:
* */
package com.example.utils.thread;

public class TestThread {

    public static void main(String[] args) {
    
        ExtendsThread demo1 = new ExtendsThread("Thread-1");
        demo1.start();

        ExtendsThread demo2 = new ExtendsThread("Thread-2");
        demo2.start();
    }

}

//执行结果如下:
Starting Thread: Thread-1
Starting Thread: Thread-2
Running thread: Thread-1
Thread name: Thread-1, 0
Running thread: Thread-2
Thread name: Thread-2, 0
Thread name: Thread-2, 1
Thread name: Thread-1, 1
Thread name: Thread-1, 2
Thread name: Thread-2, 2
Thread name: Thread-2, 3
Thread name: Thread-1, 3
Thread name: Thread-2, 4
Thread name: Thread-1, 4
Thread name: Thread-2, 5
Thread name: Thread-1, 5
Thread name: Thread-1, 6
Thread name: Thread-2, 6

Process finished with exit code 0
3、通过 Callable 和 Future 创建线程;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableThreadTest implements Callable<Integer> {
    
    public static void main(String[] args) {

        CallableThreadTest ctt = new CallableThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        for(int i = 0;i < 7;i++) {
            System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);
            if(i==5) {
                new Thread(ft,"有返回值的线程").start();
            }
        }
        try {
            System.out.println("子线程的返回值:"+ft.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Integer call() throws Exception {
        int i = 0;
        for(; i<5; i++) {
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
        return i;
    }
}

//执行结果如下:
main 的循环变量i的值0
main 的循环变量i的值1
main 的循环变量i的值2
main 的循环变量i的值3
main 的循环变量i的值4
main 的循环变量i的值5
main 的循环变量i的值6
有返回值的线程 0
有返回值的线程 1
有返回值的线程 2
有返回值的线程 3
有返回值的线程 4
子线程的返回值:5

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值