Java concurrent programming example--1. Create and run a thread

Start writing Java concurrent programming examples from this article, and the content is translated from the book: "Java 7 Concurrency Cookbook"

When it comes to threads, one inescapable question is:


The difference between concurrency and parallelism:


Explanation 1: Concurrency means that two or more events occur at the same time interval; Parallelism means that two or more events occur at the same time;
Explanation 2: Concurrency means that multiple tasks are processed "simultaneously" on a single processor ;Parallel is the simultaneous processing of multiple tasks on multiple processors (or multi-core CPUs);
Explanation 3: Concurrency is the out-of-order execution of multiple threads; Parallelism is the use of multiple threads to execute in a certain order to simplify processing a certain problem;
    
Therefore, the goal of concurrent programming is to fully utilize each core of the processor to achieve the highest processing performance.


There are 2 ways to create and run a thread in Java


1. Inherit the Thread class and override the run() method

2. Create a thread class to implement the Runnable interface, and then create a thread class object.


In this example, we use the second option, creating and running 10 threads. Each thread prints a multiplication table between 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();
        }
    }
}

Output ( different results each run ):

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






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169063&siteId=291194637