欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中实现线程的三种方式及对比_动力节点Java学院整理

程序员文章站 2023-12-22 19:04:28
java中创建线程主要有三种方式: 一、继承thread类创建线程类 (1)定义thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任...

java中创建线程主要有三种方式:

一、继承thread类创建线程类

(1)定义thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务。因此把run()方法称为执行体。

(2)创建thread子类的实例,即创建了线程对象。

(3)调用线程对象的start()方法来启动该线程。

package com.thread; 
public class firstthreadtest extends thread{ 
  int i = 0; 
  //重写run方法,run方法的方法体就是现场执行体 
  public void run() 
  { 
    for(;i<100;i++){ 
    system.out.println(getname()+" "+i); 
    } 
  } 
  public static void main(string[] args) 
  { 
    for(int i = 0;i< 100;i++) 
    { 
      system.out.println(thread.currentthread().getname()+" : "+i); 
      if(i==20) 
      { 
        new firstthreadtest().start(); 
        new firstthreadtest().start(); 
      } 
    } 
  } 
} 

上述代码中thread.currentthread()方法返回当前正在执行的线程对象。getname()方法返回调用该方法的线程的名字。

二、通过runnable接口创建线程类

(1)定义runnable接口的实现类,并重写该接口的run()方法,该run()方法的方法体同样是该线程的线程执行体。

(2)创建 runnable实现类的实例,并依此实例作为thread的target来创建thread对象,该thread对象才是真正的线程对象。

(3)调用线程对象的start()方法来启动该线程。

示例代码为:

package com.thread; 
public class runnablethreadtest implements runnable 
{ 
  private int i; 
  public void run() 
  { 
    for(i = 0;i <100;i++) 
    { 
      system.out.println(thread.currentthread().getname()+" "+i); 
    } 
  } 
  public static void main(string[] args) 
  { 
    for(int i = 0;i < 100;i++) 
    { 
      system.out.println(thread.currentthread().getname()+" "+i); 
      if(i==20) 
      { 
        runnablethreadtest rtt = new runnablethreadtest(); 
        new thread(rtt,"新线程1").start(); 
        new thread(rtt,"新线程2").start(); 
      } 
    } 
  } 
} 

三、通过callable和future创建线程

(1)创建callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。

(2)创建callable实现类的实例,使用futuretask类来包装callable对象,该futuretask对象封装了该callable对象的call()方法的返回值。

(3)使用futuretask对象作为thread对象的target创建并启动新线程。

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

实例代码:

package com.thread; 
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 < 100;i++) 
    { 
      system.out.println(thread.currentthread().getname()+" 的循环变量i的值"+i); 
      if(i==20) 
      { 
        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<100;i++) 
    { 
      system.out.println(thread.currentthread().getname()+" "+i); 
    } 
    return i; 
  } 
} 

二、创建线程的三种方式的对比

采用实现runnable、callable接口的方式创见多线程时,优势是:

线程类只是实现了runnable接口或callable接口,还可以继承其他类。

在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将cpu、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。

劣势是:

编程稍微复杂,如果要访问当前线程,则必须使用thread.currentthread()方法。

使用继承thread类的方式创建多线程时优势是:

编写简单,如果需要访问当前线程,则无需使用thread.currentthread()方法,直接使用this即可获得当前线程。

劣势是:

线程类已经继承了thread类,所以不能再继承其他父类。

以上所述是小编给大家介绍的java中实现线程的三种方式及对比_动力节点java学院整理,希望对大家有所帮助

上一篇:

下一篇: