Java中创建多线程的三种方法

目录

一、继承Thread类,重写run方法

1.准备一个普通的学生类

2.学生类继承Thread接口,并重写run方法

3.在Main方法中生成两个学生线程,并启动

4.输出结果

二、实现Runnable接口

1.准备一个普通的学生类

2.实现Runnable接口,重写run方法

3.在Main方法中,把学生类对象作为Thread对象的构造函数参数传入,并启动

4.输出结果

三、实现Callable接口

1.准备一个普通的学生类

2.实现Callable接口,重写call方法

3.创建FutureTask,传入实现Callable的学生类对象作为参数

4.输出结果


一、继承Thread类,重写run方法

示例:

1.准备一个普通的学生类

学生类属性有学生的名字学生跳的次数。

方法有

构造方法初始化学生名字。

跳行为方法执行该方法一次、学生跳的次数就加1,并输出语句。

public class StudentThread {
    private String name;
    private int jump;

    public StudentThread(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }
}

2.学生类继承Thread接口,并重写run方法

在run方法中,我们让跳方法执行20次,也代表线程开启后会执行20次。

public class StudentThread extends Thread {
    private String name;
    private int jump;

    public StudentThread(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }

    @Override
    public void run() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }

    }
}

3.在Main方法中生成两个学生线程,并启动

public class Main {
    public static void main(String[] args) {
        StudentThread sh1 = new StudentThread("冷少");
        StudentThread sh2 = new StudentThread("小松");
        sh1.start();
        sh2.start();
    }
}

4.输出结果

“小松” 与 “冷少” 交替输出 证明创建成功

 

 

二、实现Runnable接口

示例:

1.准备一个普通的学生类

学生类属性有学生的名字学生跳的次数。

方法有

构造方法初始化学生名字。

跳行为方法执行该方法一次、学生跳的次数就加1,并输出语句。

public class StudentRunnable{
    private String name;
    private int jump;

    public StudentRunnable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }
}

2.实现Runnable接口,重写run方法

与重写Thread的run方法类似

public class StudentRunnable implements Runnable {
    private String name;
    private int jump;

    public StudentRunnable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }

    @Override
    public void run() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }

    }
}

3.在Main方法中,把学生类对象作为Thread对象的构造函数参数传入,并启动

public class Main {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new StudentRunnable("冷少"));
        Thread thread2 = new Thread(new StudentRunnable("小松"));
        thread1.start();
        thread2.start();
    }
}

4.输出结果

“小松” 与 “冷少” 交替输出 证明创建成功

 

三、实现Callable接口

示例:

1.准备一个普通的学生类

学生类属性有学生的名字学生跳的次数。

方法有

构造方法初始化学生名字。

跳行为方法执行该方法一次、学生跳的次数就加1,并输出语句。

public class StudentCallable{
    private String name;
    private int jump;

    public StudentCallable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }
}

2.实现Callable接口,重写call方法

1.Callable<T> T的返回值与call方法的返回值相同

2.Callable<T> T为Callable实现类FutureTask的get方法返回类型

public class StudentCallable implements Callable<String> {
    private String name;
    private int jump;

    public StudentCallable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("我的名字是:" + this.name +",我跳了:" + ++jump + "下");
    }

    @Override
    public String call() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }
        return this.name + "跳完了!";

    }
}

3.创建FutureTask,传入实现Callable的学生类对象作为参数

FutureTask<T> T 与 实现Callable的对象类的call方法返回类型相同

public class Main {
    public static void main(String[] args) {
        FutureTask<String> ft1 = new FutureTask<>(new StudentCallable("冷少"));
        FutureTask<String> ft2 = new FutureTask<>(new StudentCallable("小松"));
        Thread thread1 = new Thread(ft1);
        Thread thread2 = new Thread(ft2);
        thread1.start();
        thread2.start();
        try {
            String ft1_return = ft1.get();
            String ft2_return = ft2.get();
            System.out.println(ft1_return);
            System.out.println(ft2_return);
        }
        catch (InterruptedException | ExecutionException e)
        {
            e.printStackTrace();
        }

    }
}

4.输出结果

“小松” 与 “冷少” 交替输出 证明创建成功

并且在最后输出了线程结束时的返回值

 

 

以上:为个人学习时的笔记,如果有错误,欢迎各位大神指出

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值