Java多线程系列·一创建线程的几种方式

线程的生命周期

Java多线程系列·一创建线程的几种方式

创建线程的方式

看下Thread构造方法

    • Thread()

      Allocates a new Thread object.

      Thread(Runnable target)

      Allocates a new Thread object.

      Thread(Runnable target, String name)

      Allocates a new Thread object.

      Thread(String name)

      Allocates a new Thread object.

      Thread(ThreadGroup group, Runnable target)

      Allocates a new Thread object.

      Thread(ThreadGroup group, Runnable target, String name)

      Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.

      Thread(ThreadGroup group, Runnable target, String name, long stackSize)

      Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.

      Thread(ThreadGroup group, String name)

      Allocates a new Thread object.

ThreadGroup类现在基本已经废弃,因为可有可无。

 

 

一.继承Thread类,实现该类的public void run()方法

public class OtherT extends Thread{
@Override
public void run(){
System.out.println("learning feels well");
}
}

public class Test{
public static void main(String[] args){
System.out.println("Hello you");
Thread t1=new OtherT();
System.out.println("Hello he");
t1.start();
}
}

运行结果

Java多线程系列·一创建线程的几种方式

 

 

二.实现Runnable接口的public void run()方法

public class Test{
public static void main(String[] args){
System.out.println("Hello you");
Thread t1=new Thread(()->{
System.out.println("Hello world!");
});

System.out.println("Hello he");
t1.start();
}
}

运行结果

Java多线程系列·一创建线程的几种方式

用到Thread的构造参数Thread(Runnable target)

()->{System.out.println("Hello world!");这个就是Runnable接口的实现,不过是用了lambda表达式,JDK1.8后加入的。

也可以用这种方式

new Runnable() {
    @Override
    public void run() {
       System.out.println("Hello world");
});

也可以用一个类实现Runnable接口,再把该类的实例代入进去

 

三.实现Callable接口中的public <T> call()方法,该方法区别于前两种方式,具有返回值

import java.util.concurrent.Callable;
public class ThirdThread implements Callable<String>{
public String call() throws Exception{
try{
Thread.sleep(500L);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("This is thread test");
return "thread B";
}
}

 

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Test{
public static void main(String[] args){
ThirdThread thread=new ThirdThread();
System.out.println("Hello you");
FutureTask<String> feature=new FutureTask<String>(thread);
System.out.println("Hello he");
new Thread(feature).start();
System.out.println("This is Main Thread");
try{
System.out.println("the result from return"+feature.get());//该方法会阻塞直到得到返回结果
System.out.println("Hello lolo");
}catch(InterruptedException e){
e.printStackTrace();
}catch(ExecutionException e){
e.printStackTrace();
}
System.out.println("This is Main Thread:end!");
}
}

运行结果

Java多线程系列·一创建线程的几种方式

介绍下FutureTask和Future

java.util.concurrent

Interface Future<V>是一个接口

Java多线程系列·一创建线程的几种方式

 

先简单讲到这里,后面再详细讲这个内容

上面讲到FutureTask<V>实现了Runnable接口,所以它的实例可以代入Thread类构造方法

FutureTask提供两个构造器:

 
  1. public FutureTask(Callable<V> callable) {}

  2. public FutureTask(Runnable runnable, V result) {}