从这一篇开始写Java并发编程实例,内容都翻译整理自书籍:《Java 7 Concurrency Cookbook》
谈到线程,无法逃避的一个问题就是:
并发(concurrency)和并行(parallellism)区别:
解释一:并发是指两个或多个事件在同一时间间隔发生;并行是指两个或者多个事件在同一时刻发生;
解释二:并发是多个任务在一台处理器上“同时”处理;并行是在多台处理器(或多核心CPU)上同时处理多个任务;
解释三:并发是多个线程无序执行;并行是使用多个线程按一定顺序执行以简化处理某一问题;
所以并发编程的目标是充分的利用处理器的每一个核,以达到最高的处理性能。
Java中有2种方式创建并运行一个线程
1.继承Thread类并重写run()方法
2.创建一个线程类实现Runnable接口,然后创建一个线程类对象。这个例子中,我们采用第二种方案,创建并运行10个线程。每个线程打印1-10之间乘法表。
package com.dylan.thread;
/**
* @author xusucheng
* @create 2018-04-11
**/
public class Calculator implements Runnable {
private int number;
public Calculator(int number) {
this.number = number;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.printf("%s: %d * %d = %d\n", Thread.
currentThread().getName(), number, i, i * number);
}
}
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
Calculator calculator = new Calculator(i);
Thread thread = new Thread(calculator);
thread.start();
}
}
}
输出(每次运行结果不一样):
Thread-0: 1 * 1 = 1
Thread-9: 10 * 1 = 10
Thread-8: 9 * 1 = 9
Thread-8: 9 * 2 = 18
Thread-7: 8 * 1 = 8
Thread-7: 8 * 2 = 16
Thread-7: 8 * 3 = 24
Thread-6: 7 * 1 = 7
Thread-6: 7 * 2 = 14
Thread-5: 6 * 1 = 6
Thread-4: 5 * 1 = 5
Thread-4: 5 * 2 = 10
Thread-4: 5 * 3 = 15
Thread-4: 5 * 4 = 20
Thread-4: 5 * 5 = 25
Thread-4: 5 * 6 = 30
Thread-4: 5 * 7 = 35
Thread-4: 5 * 8 = 40
Thread-4: 5 * 9 = 45
Thread-4: 5 * 10 = 50
Thread-3: 4 * 1 = 4
Thread-2: 3 * 1 = 3
Thread-2: 3 * 2 = 6
Thread-2: 3 * 3 = 9
Thread-2: 3 * 4 = 12
Thread-1: 2 * 1 = 2
Thread-2: 3 * 5 = 15
Thread-3: 4 * 2 = 8
Thread-3: 4 * 3 = 12
Thread-3: 4 * 4 = 16
Thread-3: 4 * 5 = 20
Thread-3: 4 * 6 = 24
Thread-3: 4 * 7 = 28
Thread-3: 4 * 8 = 32
Thread-3: 4 * 9 = 36
Thread-3: 4 * 10 = 40
Thread-5: 6 * 2 = 12
Thread-6: 7 * 3 = 21
Thread-6: 7 * 4 = 28
Thread-6: 7 * 5 = 35
Thread-6: 7 * 6 = 42
Thread-6: 7 * 7 = 49
Thread-6: 7 * 8 = 56
Thread-6: 7 * 9 = 63
Thread-6: 7 * 10 = 70
Thread-7: 8 * 4 = 32
Thread-7: 8 * 5 = 40
Thread-7: 8 * 6 = 48
Thread-7: 8 * 7 = 56
Thread-7: 8 * 8 = 64
Thread-7: 8 * 9 = 72
Thread-7: 8 * 10 = 80
Thread-8: 9 * 3 = 27
Thread-9: 10 * 2 = 20
Thread-0: 1 * 2 = 2
Thread-0: 1 * 3 = 3
Thread-0: 1 * 4 = 4
Thread-9: 10 * 3 = 30
Thread-9: 10 * 4 = 40
Thread-9: 10 * 5 = 50
Thread-9: 10 * 6 = 60
Thread-8: 9 * 4 = 36
Thread-8: 9 * 5 = 45
Thread-8: 9 * 6 = 54
Thread-8: 9 * 7 = 63
Thread-5: 6 * 3 = 18
Thread-2: 3 * 6 = 18
Thread-2: 3 * 7 = 21
Thread-1: 2 * 2 = 4
Thread-1: 2 * 3 = 6
Thread-1: 2 * 4 = 8
Thread-2: 3 * 8 = 24
Thread-2: 3 * 9 = 27
Thread-5: 6 * 4 = 24
Thread-8: 9 * 8 = 72
Thread-8: 9 * 9 = 81
Thread-8: 9 * 10 = 90
Thread-9: 10 * 7 = 70
Thread-0: 1 * 5 = 5
Thread-9: 10 * 8 = 80
Thread-9: 10 * 9 = 90
Thread-5: 6 * 5 = 30
Thread-5: 6 * 6 = 36
Thread-2: 3 * 10 = 30
Thread-1: 2 * 5 = 10
Thread-5: 6 * 7 = 42
Thread-5: 6 * 8 = 48
Thread-5: 6 * 9 = 54
Thread-5: 6 * 10 = 60
Thread-9: 10 * 10 = 100
Thread-0: 1 * 6 = 6
Thread-0: 1 * 7 = 7
Thread-0: 1 * 8 = 8
Thread-1: 2 * 6 = 12
Thread-1: 2 * 7 = 14
Thread-1: 2 * 8 = 16
Thread-1: 2 * 9 = 18
Thread-1: 2 * 10 = 20
Thread-0: 1 * 9 = 9
Thread-0: 1 * 10 = 10