Thread JDK1.5

一、Java5 前时代

1. 并发实现(两种)

Java Green Thread (模拟线程实现方式。)

Java Native Thread

2. 局限性

缺少线程管理的支持。(线程池)
缺少“锁”API。
缺少执行完成的状态。
执行结果获取困难。
Double Check locking 不确定性。

3. Code 例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ThreadMain {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
/**
* synchronized 关键字是是一种编程语言修饰符。
* */
@Override
public synchronized void run() {
System.out.printf("[Thread : %s] Hello , World... \n",Thread.currentThread().getName());
}
},"Sub");

thread.start();
System.out.printf("[Thread : %s] Starting......\n",Thread.currentThread().getName());
}
}
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
/**
* 可完成的{@link Runnable}
*
* @author Yangkai
* @create 2018-10-24-12:11
*/
public class CompletableRunnableMain {
public static void main(String[] args) throws InterruptedException {
CompletableRunnable completableRunnable = new CompletableRunnable();
Thread thread = new Thread(completableRunnable, "Sub");

thread.start();
/**
* Waits for this thread to die.
* 等着线程执行结束。串行操作。
**/
thread.join();
System.out.printf("[Thread : %s] Starting......\n", Thread.currentThread().getName());

System.out.printf("running is completed .....: %s\n", completableRunnable.isCompleted());
}

private static class CompletableRunnable implements Runnable {
private volatile boolean completed = false;

@Override
public void run() {
System.out.printf("[Thread : %s] Hello , World... \n", Thread.currentThread().getName());
completed = true;
}

public boolean isCompleted() {
return completed;
}
}
}