java并发编程专题连载系列

曾经整理过一个并发专题的连载系列,但由于时间久远,且查阅资料有限,本次尽量按照项目实际应用遇到的问题进行举例。由于时间有限,本文将长时间进行更新…一开始就将一个类设计成是线程安全的,比在后期重新修复它更容易。

网易云课堂知识大纲
知识大纲
相关官方文档:https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

并发

多线程程序在较低的层次上扩展了多任务的概念:一个程序同时执行多个任务。通常,
每一个任务称为一个线程( thread), 它是线程控制的简称。可以同时运行一个以上线程的程
序称为多线程程序(multithreaded)。

线程

在java api文档中原文对Thread类解释如下

1
2
3
4
5
6
7
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from t

译文

1
2
3
4
5
6
线程是程序中的执行线程。 Java虚拟机允许应用程序同时运行多个执行线程。
每个线程都有一个优先级。优先级较高的线程优先执行优先级较低的线程。每个线程可能也可能不会标记为守护程序。当在某些线程中运行的代码创建一个新线程时,新线程的优先级最初设置等于创建线程的优先级,并且仅当创建线程是守护程序时,才是守护程序线程。
当Java虚拟机启动时,通常会有一个非daemon线程(通常调用某些指定类的主题的方法)。 Java虚拟机继续执行线程,直到发生以下任何一个:

已经调用了类运行时的退出方法,安全管理器已允许进行退出操作。
所有不是守护程序线程的线程都死了,要么从T返回

对于本段话则可以进行提问

1
2
什么是守护线程?
什么是daemon线程?

为什么需要多线程

1.资源利用,多CPU系统中提高CPU利用率
2.提高应用程序响应,程序的运行效率会提高
3.编译程序优化指令执行次序,使得缓存能够得到更加合理地利用

线程安全问题

多线程并发执行时,对共享内存中共享对象的属性发生修改时所导致的数据冲突问题,称之为线程安全问题

即使你的程序没有显式地创建任何线程,框架也可能为你创建了一些线程,这些线程
调用的代码必须是线程安全的(thread-safe)。这一点给开发人员的设计和实现赋予了更
重要的一份责任,因为开发线程安全的类要比非线程安全的类需要更加仔细,进行更多的
分析。

线程状态

线程可以有如下 6 种状态:
•New (新创建)
•Runnable (可运行)
•Blocked (被阻塞)
•Waiting (等待)
•Timed waiting (计时等待)
•Terminated (被终止)
下一节对每一种状态进行解释。
要确定一个线程的当前状态, 可调用 getState 方法。

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
55
56
57
58
59
60
61
62
63
64
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,

/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,

/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,

/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,

/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,

/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}

新创建线程

当用 new 操作符创建一个新线程时,如 newThread(r), 该线程还没有开始运行。这意味
着它的状态是 new。当一个线程处于新创建状态时,程序还没有开始运行线程中的代码。在
线程运行之前还有一些基础工作要做。
那么所有创建线程的本质都是new Thread()

示例

1
2
3
4
5
6
7
public static void main(String[] args) {
new Thread(()->{ System.out.printf("线程%s 正在执行",Thread.currentThread().getName()); },"thread-01").start();
System.out.printf("线程%s 是否有存活%s",Thread.currentThread().getName(),Thread.currentThread().isAlive());
}

控制台输出
线程main 是否有存活true线程thread-01 正在执行Disconnected from the target VM, address: '127.0.0.1:53273', transport: 'socket'
{% if post.top %} 置顶 | {% endif %}