JAVA--创建多线程的四种方法

39 篇文章 0 订阅
4 篇文章 0 订阅

一、通过继承Thread类

(1)定义子类继承Thread类

(2)子类中重写Thread中的run方法

(3)创建Thread子类对象,即创建了线程对象

(4)调用线程对象的start方法(即启动线程同时调用了其中重写的run方法)

实例:

class MyThread extends Thread{
    public void run(){
        for(int i=0;i<100;i++){
            if (i%2==0)
                System.out.println(i);
        }
    }
}

public class ThreadTest {
    public static void main(String[] args) {
        MyThread th= new MyThread();
        th.start();
    }
}

二、实现Runnable接口

(1)定义子类实现Runnable接口

(2)子类中重写Runnable接口的run方法

(3)创建实现Runnable接口的子类对象

(4)将实现了Runnable接口的子类对象作为实际参数,发送到Thread类的含参构造器中创建线程对象

(5)运用Thread类对象调用Thread类的start方法(启动线程,调用Runnable接口重写的run方法)

实例:

class MyThread1 implements Runnable{
    public void run(){
        for(int i=0;i<100;i++){
            if (i%2==0)
                System.out.println(i);
        }
    }
}

public class ThreadTest1 {
    public static void main(String[] args) {
        MyThread1 mythread= new MyThread1();
        Thread th1=new Thread(mythread);
        th1.start();

    }
}

三、实现Callable接口

(1)定义子类实现Callable接口

(2)子类中重写Callable接口的call方法

(3)创建实现Callable接口的子类对象

(4)将实现了Callable接口的子类对象作为实际参数,发送到FutureTask类中的含参构造器中创建FutureTask类的对象

(5)将FutureTask类的对象作为实际参数,发送到Thread类的含参构造器中创建线程对象

(6)运用Thread类对象调用Thread类的start方法(启动线程,调用Callable接口重写的call方法)

实例:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread2 implements Callable{
    @Override
    public Object call() throws Exception {
       
        for (int i=0;i<=100;i++){
            if(i%2==0){
                System.out.println(i);
            }
        }
        return null;
    }
}
public class ThreadTest2 {
    public static void main(String[] args) {
        MyThread2 myThread2=new MyThread2();
        FutureTask futureTask=new FutureTask(myThread2);
        new Thread(futureTask).start();
    }
}

四、使用线程池

(1)定义子类实现Runnable接口(或Callable接口)

(2)子类中重写Runnable接口的run方法(或Callable接口的call方法)

(3)用ExecutorService接口实例化来接收创建的线程池

(4)将实现了Runnable(Callable)接口的子类对象作为实际参数,发送到ExecuttorService接口的实例化调用的execute(submit)方法中(启动线程,调用Runnable(Callable)接口重写的run(call)方法)

实例:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread3 implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<=100;i++){
            if(i%2==0){
                System.out.println(i);
            }
        }
    }
}
public class ThreadTest3 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new MyThread3());
        service.shutdown();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值