Java多线程-02之 多线程实现方式

实现多线程的几种方式

  1. 继承Thread类
  2. 实现Runnable接口
  3. 通过Callable和Future
  4. 通过线程池

Thread和Runnable简介

Runnable

Runnable是一个接口库,该接口只包含了一个run()方法,定义如下:

1
2
3
public interface Runnable {
public abstract void run();
}

Runnable的作用,实现多线程。我们可以定义一个类A实现Runnable接口;然后,通过new Thread(new A())等方式新建线程。

Thread

Thread 是一个类。Thread本身就实现了Runnable接口。它的声明如下:

1
public class Thread implements Runnable {}

Thread和Runnable的异同点

Thread 和 Runnable 的相同点:都是“多线程的实现方式”。
Thread 和 Runnable 的不同点:
Thread 是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。我们知道“一个类只能有一个父类,但是却能实现多个接口”,因此Runnable具有更好的扩展性。
此外,Runnable还可以用于“资源的共享”。即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。
通常,建议通过“Runnable”实现多线程!

Thread和Runnable的多线程示例

Thread的多线程示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ThreadTest {
public static void main(String[] args) {
// 启动3个线程t1,t2,t3;每个线程各卖10张票!
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
t2.start();
t3.start();
}
}

class MyThread extends Thread {
private int ticket = 10;

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

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Thread-2 卖票:ticket10
Thread-1 卖票:ticket10
Thread-0 卖票:ticket10
Thread-1 卖票:ticket9
Thread-2 卖票:ticket9
Thread-1 卖票:ticket8
Thread-0 卖票:ticket9
Thread-1 卖票:ticket7
Thread-2 卖票:ticket8
Thread-1 卖票:ticket6
Thread-0 卖票:ticket8
Thread-1 卖票:ticket5
Thread-2 卖票:ticket7
Thread-1 卖票:ticket4
Thread-0 卖票:ticket7
Thread-1 卖票:ticket3
Thread-2 卖票:ticket6
Thread-1 卖票:ticket2
Thread-0 卖票:ticket6
Thread-1 卖票:ticket1
Thread-2 卖票:ticket5
Thread-0 卖票:ticket5
Thread-2 卖票:ticket4
Thread-0 卖票:ticket4
Thread-2 卖票:ticket3
Thread-0 卖票:ticket3
Thread-2 卖票:ticket2
Thread-0 卖票:ticket2
Thread-2 卖票:ticket1
Thread-0 卖票:ticket1

结果说明:
(01) MyThread继承于Thread,它是自定义个线程。每个MyThread都会卖出10张票。
(02) 主线程main创建并启动3个MyThread子线程。每个子线程都各自卖出了10张票。

Runnable的多线程示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyThread2 implements Runnable {
private int ticket = 10;

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

public class RunnableTest {
public static void main(String[] args) {
MyThread2 mt = new MyThread2();

// 启动3个线程t1,t2,t3(它们共用一个Runnable对象),这3个线程一共卖10张票!
Thread t1 = new Thread(mt);
Thread t2 = new Thread(mt);
Thread t3 = new Thread(mt);
t1.start();
t2.start();
t3.start();
}
}

运行结果:

1
2
3
4
5
6
7
8
9
10
Thread-0 卖票:ticket10
Thread-0 卖票:ticket7
Thread-0 卖票:ticket6
Thread-0 卖票:ticket5
Thread-2 卖票:ticket8
Thread-1 卖票:ticket9
Thread-1 卖票:ticket2
Thread-1 卖票:ticket1
Thread-2 卖票:ticket3
Thread-0 卖票:ticket4

结果说明:
(01) 和上面“MyThread继承于Thread”不同;这里的MyThread实现了Thread接口。
(02) 主线程main创建并启动3个子线程,而且这3个子线程都是基于“mt这个Runnable对象”而创建的。运行结果是这3个子线程一共卖出了10张票。这说明它们是共享了MyThread接口的。

本文标题:Java多线程-02之 多线程实现方式

文章作者:王洪博

发布时间:2018年05月26日 - 21:05

最后更新:2019年09月12日 - 10:09

原始链接:http://whb1990.github.io/posts/3868ed28.html

▄︻┻═┳一如果你喜欢这篇文章,请点击下方"打赏"按钮请我喝杯 ☕
0%