java多线程的三种创建方法

2021/11/27 11:12:14

本文主要是介绍java多线程的三种创建方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//一:通过继承Thread的类并重写run()方法
//二:定义一个类实现Runnable接口中的run()方法,将对象作为参数传给Thread的实例中,然后用Thread的start()开始多线程
//三:
//声明类去实现Callable中的call()方法,call方法有返回值
//然后使用FutureTask类去包装Callable 的对象,用get()方法获得返回值
//用FutureTask的对象做Thread的参数

//四:Java8开始,使用Lambda表达式去创建Callable对象,不需要先去创建Callable类
测试一
public class CreateThread extends Thread{
    public void run()
    {
        int a = 0;
        for(;a<10;a++)
        {
            System.out.println(this.getName()+a);
        }
    }
    public static void main(String[] args) {
        //一:通过继承Thread的类并重写run()方法
        CreateThread ct=new CreateThread();
        ct.start();
        char a = 'a';
        for(;a<'z';a++)
        {
            System.out.println(Thread.currentThread().getName()+a);
        }
    }
}

测试二
public class Runnable {
    public static void main(String[] args) {
        Thread ct=new Thread(new te());
        ct.start();
        char a = 'a';
        for(;a<'z';a++)
        {
            System.out.println(Thread.currentThread().getName()+a);
        }
    }

}
class te implements java.lang.Runnable
{
    @Override
    public void run() {
        //获取当前线程的名字
        for(int a=0;a<10;a++)
        {
            System.out.println(Thread.currentThread().getName()+a);
        }
    }
}
测试三

public class CallableAndFuture {
    public static void main(String[] args) {
        FutureTask<Integer> ft= new FutureTask<>(new no());
        new Thread(ft,"自定义名称").start();
        try {
            System.out.println("自定义名称线程的返回值"+ft.get());
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        //编写主线程的方法
        char a = 'a';
        for(;a<'z';a++)
        {
            System.out.println(Thread.currentThread().getName()+a);
        }
    }
}
//实现Callable中的call()
class no implements Callable{
    @Override
    public Integer  call() throws Exception {
        int b=0;
        for(int i=1;i<101;i++)
        {
            b+=i;
            System.out.println(Thread.currentThread().getName()+i);
        }
        return b;
    }
}
测试四:
public class Lambda {
    public static void main(String[] args) {
        //实现Callable中的call()
        FutureTask<Integer> ft= new FutureTask<>(
                ( Callable<Integer>)()->
                {
                    int b=0;
                    for(int i=1;i<101;i++)
                    {
                        b+=i;
                        System.out.println("lambda"+Thread.currentThread().getName()+i);
                    }
                    return b;
                }
        );
        new Thread(ft,"自定义名称").start();
        try {
            System.out.println("自定义名称线程的返回值"+ft.get());
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        //编写主线程的方法
        char a = 'a';
        for(;a<'z';a++)
        {
            System.out.println(Thread.currentThread().getName()+a);
        }
    }
}
 
 


这篇关于java多线程的三种创建方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程