markdown
### (一)Thread.State
```text
#创建
NEW: A thread that has not yet started is in this state.
#运行
RUNNABLE: A thread executing in the Java virtual machine is in this state.
#阻塞
BLOCKED: A thread that is blocked waiting for a monitor lock is in this state.
#等待
WAITING: A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
#消亡
TERMINATED: A thread that has exited is in this state.
```
### (二)线程安全
#### 1. 同步代码块 synchronized
```text
synchronized(同步监视器){
需要同步的代码;
}
# 任何一个类的对象 都可以视为同步监视器,称之为锁
# 当多个线程共同操作同一变量时,该锁需要为同一个
# Runnable可以考虑 this。Thread可以考虑 CurrentThreadClass.class
```
示例代码
```text
class ThreadTest implements Runnable{
private static int ticket = 100;
@Override
public void run() {
synchronized (this) {
while (true){
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " 票号 " + (ticket --));
} else {
break;
}
}
}
}
}
```
#### 2. 同步方法
```text
private synchronized void func(){}
#将同步代码块的内容,抽象为方法并在方法权限修饰器后添加synchronized即可
```
示例代码
```text
class ThreadTest implements Runnable{
private static int ticket = 100;
private static boolean signal = true;
@Override
public void run() {
while (signal){
sell();
}
}
private synchronized void sell(){
if (ticket > 0){
System.out.println(Thread.currentThread().getName() + "票号" + (ticket--));
}else{
signal = false;
}
}
}
```
#### 3. Lock
```text
#常用子类
public class ReentrantLock extends Object implements Lock, Serializable
#reentrantLock.lock()后紧随try代码块,reentrantLock.unlock(),一定要出现在finally代码块中
#手动上锁,手动解锁
```
示例代码
```text
class ThreadTestTwo implements Runnable{
private static int ticket = 100;
private final ReentrantLock reentrantLock = new ReentrantLock(false);
@Override
public void run() {
while (true) {
reentrantLock.lock();
try {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " 票号 " + (ticket--));
} else {
break;
}
}finally {
reentrantLock.unlock();
}
}
}
}
```
#### 死锁
```text
死锁出现后不会出现异常,但会导致当前资源无法释放,无法申请,进程处于停滞状态,
无法继续向前运行
```
#### sleep/wait
```text
相同点:
* 一旦执行方法,该线程进入阻塞状态
不同点:
* sleep声明在Thread类中,可以在任何方法中使用
* wait声明在Object类中,必须使用在同步方法/同步代码块中
* 同步方法/同步代码块中sleep不会释放锁,wait会释放锁
```
#### notify/notifyall
```text
唤醒wait()方法阻塞的进程,区别在于是唤醒一个还是全部
```
### (三)线程创建的新方法
#### 1. Callable
```text
相比于Runnable的优势:
允许有返回值(包括泛型)
可以抛出异常
```
示例代码
```text
class CreateNew implements Callable{
@Override
public Object call() throws Exception {
int[] temp = new int[10];
for (int i = 0; i < 10; i++) {
temp[i] = i;
}
return temp;
}
}
#配合FutureTask使用
CreateNew createNew = new CreateNew();
FutureTask futureTask = new FutureTask(createNew);
futureTask.run();
try {
int[] temp = (int[]) futureTask.get();
for (int i = 0; i < 10; i++) {
System.out.println(temp[i]);
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
```
#### 2. 线程池
```text
#关系
public interface Executor
public interface ExecutorService extends Executor
public class Executors extends Object
Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory,
and Callable classes defined in this package
```
```text
#优势
* 提高响应速度
* 降低资源消耗
* 便于线程管理
```
*
*[返回教程主页](https://www.monody.net/p/blog-page_3.html)*
*
评论