首页 > 编程知识 正文

java线程阻塞方法,java线程与进程的区别

时间:2023-05-03 20:07:43 阅读:159645 作者:1648

Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例。Java可以用三种方式来创建线程,如下所示:

1)继承Thread类创建线程

2)实现Runnable接口创建线程

3)使用Callable和Future创建线程

下面让我们分别来看看这三种创建线程的方法。

------------------------继承Thread类创建线程---------------------

继承Thread类以创建和启动多线程的一般步骤如下

1 ) d定义Thread类的子类,并重写该类的run()方法。 该方法的方法主体是线程应该执行的任务,run )方法也称为线程执行主体。

2 )创建Thread子类的实例,也就是创建线程对象

3 )启动线程,即调用线程的start()方法

代码示例

继承publicclassmythreadextendsthread {//thread类

公共语音运行(}

重写//run方法

() ) ) ) )。

() ) ) ) )。

public class Main {

publicstaticvoidmain (string [ ] args ) {

new MyThread ().start; //创建并启动线程

() ) ) ) )。

() ) ) ) )。

------------------------实现Runnable接口创建线程---------------------

实现Runnable接口以创建和启动线程的一般步骤如下:

1 )定义Runnable接口的安装类,同样改写Thread的run (和方法一样是线程的执行体run ) )方法

2 )创建Runnable实现类的实例,并使用该实例作为Thread的目标来创建Thread对象。 这个Thread对象才是真正的线程对象

3 )第三部分仍然是调用线程对象的start )方法启动线程

代码示例:

实现public class my thread2implements runnable {//runnable接口

公共语音运行(}

重写//run方法

() ) ) ) )。

() ) ) ) )。

public class Main {

publicstaticvoidmain (string [ ] args ) {

//创建并启动线程

MyThread2 myThread=new MyThread2(;

thread thread=new thread (我的thread;

thread ().start;

//或newthread(newmythread2) ).start );

() ) ) ) )。

() ) ) ) )。

------------------------使用Callable和Future创建线程---------------------

http://www.Sina.com/http://www.Sina.com /

和Runnable接口不一样,Callable接口提供了一个call()方法作为线程执行体,call()方法比run()方法

”call (方法可以声明抛出异常

Java5是Callable接口中的call ) )提供一个表示方法返回值的Future接口,并实现Future接口和Runnable接口,从而实现Thread类的Thread Future接口定义了一些公共方法,用于控制相关的Callable任务。

布尔取消(boolean mayinterruptifrunning ) :视图取消其Future中的相关可取消任务

V get () :返回呼叫

able里call()方法的返回值,调用这个方法会导致程序阻塞,必须等到子线程结束后才会得到返回值

>V get(long timeout,TimeUnit unit):返回Callable里call()方法的返回值,最多阻塞timeout时间,经过指定时间没有返回抛出TimeoutException

>boolean isDone():若Callable任务完成,返回True

>boolean isCancelled():如果在Callable任务正常完成前被取消,返回True

介绍了相关的概念之后,创建并启动有返回值的线程的步骤如下:

1】创建Callable接口的实现类,并实现call()方法,然后创建该实现类的实例(从java8开始可以直接使用Lambda表达式创建Callable对象)。

2】使用FutureTask类来包装Callable对象,该FutureTask对象封装了Callable对象的call()方法的返回值

3】使用FutureTask对象作为Thread对象的target创建并启动线程(因为FutureTask实现了Runnable接口)

4】调用FutureTask对象的get()方法来获得子线程执行结束后的返回值

代码实例:

public class Main {

  public static void main(String[] args){

   MyThread3 th=new MyThread3();

   //使用Lambda表达式创建Callable对象

     //使用FutureTask类来包装Callable对象

   FutureTask<Integer> future=new FutureTask<Integer>(

    (Callable<Integer>)()->{

      return 5;

    }

    );

   new Thread(task,"有返回值的线程").start();//实质上还是以Callable对象来创建并启动线程

    try{

    System.out.println("子线程的返回值:"+future.get());//get()方法会阻塞,直到子线程执行结束才返回

    }catch(Exception e){

    ex.printStackTrace();

   }

  }

}

--------------------------------------三种创建线程方法对比--------------------------------------

实现Runnable和实现Callable接口的方式基本相同,不过是后者执行call()方法有返回值,后者线程执行体run()方法无返回值,因此可以把这两种方式归为一种这种方式与继承Thread类的方法之间的差别如下:

1、线程只是实现Runnable或实现Callable接口,还可以继承其他类。

2、这种方式下,多个线程可以共享一个target对象,非常适合多线程处理同一份资源的情形。

3、但是编程稍微复杂,如果需要访问当前线程,必须调用Thread.currentThread()方法。

4、继承Thread类的线程类不能再继承其他父类(Java单继承决定)。

注:一般推荐采用实现接口的方式来创建多线程

 

转载于:https://www.cnblogs.com/3s540/p/7172146.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。