Java并发编程初级篇(一):线程实现与运行的两种方式
Java并发编程初级篇(一):线程实现与运行的两种方式
阿拉德大陆的魔法师 发表于1年前
Java并发编程初级篇(一):线程实现与运行的两种方式
  • 发表于 1年前
  • 阅读 29
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

摘要: Java中可以通过两种方式来实现线程,看似一样的效果,他们之间又有什么区别呢?

通过继承Thread类来实现一个线程

线程的创建:

public class MyThread extends Thread {
    private int num = 5;

    @Override
    public void run() {
        while (num > 0) {
            System.out.println("Thread:" + Thread.currentThread().getName() + ", consume " + num);
            num--;
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

线程的运行:

public static void main(String[] args) {
    MyThread thread1 = new MyThread();
    MyThread thread2 = new MyThread();
    MyThread thread3 = new MyThread();

    thread1.start();
    thread2.start();
    thread3.start();
}

运行结果:

Thread:Thread-0, consume 5
Thread:Thread-1, consume 5
Thread:Thread-2, consume 5
Thread:Thread-0, consume 4
Thread:Thread-2, consume 4
Thread:Thread-1, consume 4
Thread:Thread-0, consume 3
Thread:Thread-1, consume 3
Thread:Thread-2, consume 3
Thread:Thread-0, consume 2
Thread:Thread-2, consume 2
Thread:Thread-1, consume 2
Thread:Thread-0, consume 1
Thread:Thread-2, consume 1
Thread:Thread-1, consume 1

通过实现Runnable接口来实现一个线程

线程的创建:

public class MyRunnable implements Runnable {
    private int num = 5;

    @Override
    public void run() {
        while (num > 0) {
            System.out.println("Thread:" + Thread.currentThread().getName() + ", consume " + num);
            num--;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

第一种运行方式:

public static void main(String[] args) {
    Thread thread1 = new Thread(new MyRunnable());
    Thread thread2 = new Thread(new MyRunnable());
    Thread thread3 = new Thread(new MyRunnable());

    thread1.start();
    thread2.start();
    thread3.start();
}

第一种方式运行结果:

Thread:Thread-0, consume 5
Thread:Thread-2, consume 5
Thread:Thread-1, consume 5
Thread:Thread-0, consume 4
Thread:Thread-2, consume 4
Thread:Thread-1, consume 4
Thread:Thread-2, consume 3
Thread:Thread-1, consume 3
Thread:Thread-0, consume 3
Thread:Thread-1, consume 2
Thread:Thread-0, consume 2
Thread:Thread-2, consume 2
Thread:Thread-1, consume 1
Thread:Thread-2, consume 1
Thread:Thread-0, consume 1

第二种运行方式:

public static void main(String[] args) {
	Runnable runnable = new MyRunnable();

	Thread thread1 = new Thread(runnable);
	Thread thread2 = new Thread(runnable);
	Thread thread3 = new Thread(runnable);
        
	thread1.start();
	thread2.start();
	thread3.start();
}

第二种方式运行结果:

Thread:Thread-0, consume 5
Thread:Thread-1, consume 4
Thread:Thread-2, consume 3
Thread:Thread-0, consume 2
Thread:Thread-1, consume 1

总结:

通过继承Thread类和实现Runnable接口都能够实现一个线程,但是实现Runnable接口创建的线程类可以交给多个Thread来运行。这类似于需要加工30个零件,第一种方式为交给三个人去做每个人加工10个零件,完成时间取决于最慢的哪个人;第二种方式为一共30个零件交给3个人去加工,速度快的人会为速度慢的人分担加工的零件,总体速度会提升。

共有 人打赏支持
粉丝 20
博文 91
码字总数 83019
×
阿拉德大陆的魔法师
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: