What's thread in Java?

Let's learn and create thread in Java.

Lifecycle of a thread in Java

Life cycle of a thread in Java

Lifecycle and States of a Thread in Java

How?

Thread()

1
2
3
4
5
6
7
8
9
public class CreateThreadByThreadClass extends Thread {
    @Override
    public void run() {
        int loopTimes = 100;
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}

Runnable()

1
2
3
4
5
6
7
8
9
public class CreateThreadByRunnableInterface implements Runnable {
    @Override
    public void run() {
        int loopTimes = 100;
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}

Callable()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class CreateThreadByCallableInterface implements Callable<Integer> {
    @Override
    public Integer call() {
        int sum = 0, loopTimes = 100;
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            sum += i;
        }
        return sum;
    }
}

Tester

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class Tester {


    /**
     * 创建线程池
     */
    private static final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("pool-%d").build();
    private static final ExecutorService executorService = new ThreadPoolExecutor(5, 200, 0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        int loopTimes = 100;
        //testThread(loopTimes);
        testRunnable(loopTimes);
        //testCallable(loopTimes);
    }

    private static void testRunnable(int loopTimes) {
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                CreateThreadByRunnableInterface myRunnable = new CreateThreadByRunnableInterface();
                // 不要显示创建线程,通过线程池创建
                executorService.execute(myRunnable);
                executorService.execute(myRunnable);
                executorService.shutdown();
            }
        }
    }

    private static void testThread(int loopTimes) {
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                Thread thread1 = new CreateThreadByThreadClass();
                Thread thread2 = new CreateThreadByThreadClass();
                thread1.start();
                thread2.start();
            }
        }
    }

    private static void testCallable(int loopTimes) {
        for (int i = 0; i < loopTimes; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                Callable<Integer> myCallable = new CreateThreadByCallableInterface();
                FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
                executorService.submit(futureTask);
                executorService.shutdown();
            }
        }
    }
}
Licensed under CC BY-NC-SA 4.0
Last updated on May 15, 2020 15:25 UTC