java多线程三种实现方式(thread与runnable区别)

2 篇文章 0 订阅

Java中创建线程主要有三种方式:
①继承thread重写run,调用start

package example.testForJava;

public class threadTest extends  Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+" "+i);
        }
    }

    public static void main(String[] args) {
        threadTest threadTest1=new threadTest();
        threadTest threadTest2=new threadTest();
        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"  "+i);
            if (i==10){
                new Thread(threadTest1,"threadTest example1").start();
                new Thread(threadTest2,"threadTest example2").start();
            }
        }
    }
}

②实现runnable接口,重写run方法,创建当前实例,new一个thread对象参数为创建的实例,调用start方法

package example.testForJava;

public class runnableTest implements Runnable {
    @Override
    public void run() {
        for (int 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) {
                runnableTest runnableTest = new runnableTest();
                new Thread(runnableTest, "first thread").start();
                new Thread(runnableTest, "second thread").start();
            }
        }
    }
}

③实现callable接口,重写call方法,创建当前实例,传入futureTask(),将future作为参数传入创建new thread,执行。最后调用future的get方法得到一个返回值

package example.testForJava;

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 callableThreadTest = new callableThreadTest();
        FutureTask<Integer> futureTask = new FutureTask<>(callableThreadTest);
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " 的循环变量的i的值" + i);
            if (i == 20) {
                new Thread(futureTask, "futureTask thread").start();
            }
        }
        try {
            System.out.println("子 thread return " + futureTask.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;
    }
}

总结:常用后两种,因为没有继承类,所以可以继承其他类,多个线程共享一个target,用多个相同的线程处理同一份资源。比如买票多窗口,thread就是每个窗口都卖那么多的票,实现接口就是所有窗口共同去卖这么多的票

//发现每一个thread都卖出了十张,这不是我们需要的
package example.testForJava;

public class threadTest extends Thread {
    private int ticket = 10;

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            if (this.ticket>0) {
                System.out.println(this.getName()+"卖出第"+(10-this.ticket-- + 1)+"票");
            }
        }

    }

    public static void main(String[] args) {
        threadTest threadTest1 = new threadTest();
        threadTest threadTest2 = new threadTest();
        threadTest threadTest3 = new threadTest();
        new Thread(threadTest1, "thread1").start();
        new Thread(threadTest2, "thread2").start();
        new Thread(threadTest3, "thread3").start();
    }
}

//三个线程共同实现卖10张
package example.testForJava;

public class runnableTest implements Runnable {
    private int ticket = 10;

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            if (this.ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出第" + (10 - this.ticket-- + 1) + "票");
            }
        }
    }

    public static void main(String[] args) {

        runnableTest runnableTest = new runnableTest();
        new Thread(runnableTest, "first thread").start();
        new Thread(runnableTest, "second thread").start();
        new Thread(runnableTest, "third thread").start();

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟程序员李老板专业码代码三十年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值