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();
}
}

运行结果

 

 

二.实现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();
}
}

运行结果

用到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!");
}
}

运行结果

介绍下FutureTask和Future

java.util.concurrent

Interface Future<V>是一个接口

 

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

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

FutureTask提供两个构造器:

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值