javaSE (三十七)互斥鎖、執行緒組、執行緒的生命週期、執行緒池
阿新 • • 發佈:2018-12-16
1、互斥鎖(ReentrantLock):
互斥鎖是JDK1.5的新特性,目的就是為了替換同步程式碼塊(同步鎖),使程序間的通訊更加簡潔
下面重寫我們上一篇的三個及三個以上執行緒間的通訊:
思路:
- ReentrantLock類的lock() 和unlock() 方法可以替換synchronized同步程式碼塊(基本上就是可以直接替換)
- newCondition() 方法返回一個實現了Condition介面的物件,再用await() 方法使之等待,用signal() 喚醒下一個執行緒,這兩個方法就相當於Object的wait() 和notify() 方法(每一個執行緒都要建立一個實現了Condition
package cn.xinhua;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadTest {
public static void main(String[] args) {
Print p = new Print();
Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 1000; i++) {
try {
p.print1();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 1000; i++) {
try {
p.print2();
} catch ( InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t3 = new Thread() {
public void run() {
for (int i = 0; i < 1000; i++) {
try {
p.print3();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t1.start();
t2.start();
t3.start();
}
}
// 互斥鎖
class Print extends Thread {
final private ReentrantLock r = new ReentrantLock();
final private Condition c1 = r.newCondition();
final private Condition c2 = r.newCondition();
final private Condition c3 = r.newCondition();
final Object obj = new Object();
private int target = 1;
public void print1() throws InterruptedException {
r.lock();
if (target != 1) {
c1.await();
}
System.out.print("我");
System.out.print("是");
System.out.print("吳");
System.out.print("彥");
System.out.print("祖");
System.out.println();
target = 2;
c2.signal();
r.unlock();
}
public void print2() throws InterruptedException {
r.lock();
if (target != 2) {
c2.await();
}
System.out.print("你");
System.out.print("要");
System.out.print("相");
System.out.print("信");
System.out.print("我");
System.out.println();
target = 3;
c3.signal();
r.unlock();
}
public void print3() throws InterruptedException {
r.lock();
if (target != 3) {
c3.await();
}
System.out.print("不");
System.out.print("信");
System.out.print("我");
System.out.print("拉");
System.out.print("倒");
System.out.println();
target = 1;
c1.signal();
r.unlock();
}
}
輸出:
我是吳彥祖
你要相信我
不信我拉倒
我是吳彥祖
你要相信我
不信我拉倒
我是吳彥祖
你要相信我
不信我拉倒
我是吳彥祖
你要相信我
不信我拉倒
。。。
2、執行緒組:
執行緒組就是利用Thread的一個構造方法,將我們建立的執行緒統一放到一個執行緒組裡方便操作
- 建立執行緒組物件
- 用構造方法建立Thread物件並將執行緒放到執行緒組裡
1. 建立執行緒組物件
ThreadGroup tg = new ThreadGroup("我是執行緒組");
2. 用構造方法建立Thread物件並將執行緒放到執行緒組裡
Thread t1 = new Thread(tg,"吳彥祖")
Thread t2 = new Thread(tg,"周杰倫")
Thread t3 = new Thread(tg,"林宥嘉")
System.out.println(t1.getName());
System.out.println(t2.getName());
System.out.println(t3.getThreadGroup().getName());
輸出:
吳彥祖
周杰倫
我是執行緒組
3、執行緒的生命週期:
4、執行緒池:
- 建立執行緒池
- 將執行緒放進執行緒池裡並執行
- 關閉執行緒池
1. 建立執行緒池
ExecutorService pool = Executors.newFixedThreadPool(2);
2. 將執行緒放進執行緒池裡並執行
pool.submit(new MyRunable);
pool.submit(new MyRunable);
3. 關閉執行緒池
pool.shutdown();