一、线程
线程:一个程序同事执行多个任务,每个任务称为一个线程。可以同时运行一个以上线程的程序称为多线程程序。
不要调用Thread类或Runnable对象的run方法。直接调用run方法,只会执行同一线程中的任务,而不会启动新线程。应该调用Thread.start方法,这个方法将创建一个执行run方法的新线程。每次点击Start按钮,球会移入一个新线程。
Ball
package concurrence;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
public class Ball {
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
public void move(Rectangle2D bounds) {
x += dx;
y += dy;
if (x < bounds.getMinX()) {
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX()) {
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY()) {
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY()) {
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
}
public Ellipse2D getShape() {
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
}
}
BallComponent
package concurrence;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class BallComponent extends JPanel {
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
private List<Ball> balls = new ArrayList<>();
public void add(Ball b) {
balls.add(b);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
g2.fill(b.getShape());
}
}
public Dimension getPreferredSize() {
return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}
BounceThread
package concurrence;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class BounceThread {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setTitle("radioButton Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class BounceFrame extends JFrame {
private BallComponent comp;
public static final int STEPS = 1000;
public static final int DELAY = 5;
public BounceFrame() {
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", event -> addBall());
addButton(buttonPanel, "Close", event -> System.exit(0));
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void addBall() {
Ball ball = new Ball();
comp.add(ball);
Runnable r = () -> {
try{
for(int i=1; i <= STEPS; i++) {
ball.move(comp.getBounds());
comp.repaint();
Thread.sleep(DELAY);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread t = new Thread(r);
t.start();
}
}
- 中断线程
当线程的run方法执行方法体中最后一条语句后,并经由执行return语句返回时,或者出现了在方法中没有捕获的异常时,线程将终止。还有一个stop方法,其他线程可以调用它终止线程,但是现在被弃用了。判断当前线程是否中断,Thread.currentThread().isInterrupted().
直接在catch子句中处理InterruptedException不推荐,更合理的选择:
1)在catch子句中调用Tread.currentThread().interrupt()来设置中断状态。
2)用throws InterruptedException标记你的方法,不采用try语句块捕获异常。 - 线程状态
new、Runnable、Blocked、Waiting、Timed waiting、Terminated
1)new Thread®,该线程还没有开始运行,状态是new
2)一旦调用start方法,线程就处于Runnable状态。一个可运行的线程可能正在运行也可能没有运行,这取决于操作系统给线程提供的运行时间。线程不必始终保持运行,为了让其他线程获得运行机会,运行中的线程就可能被中断。
3)当线程处于被阻塞和等待状态时,它暂时不活动。它不运行任何代码且消耗最少的资源。直到线程调度器重新激活它。
阻塞状态:当一个线程试图获取一个内部的对象锁,而该锁被其他线程持有,该线程进入阻塞状态。当所有其他线程释放该锁,并且线程调度器允许本线程持有它的时候,该线程变成非阻塞状态。
等待状态:当线程等待另一个线程通知调度器一个条件时,它自己进入等待状态。在调用Object.wait或Thread.join,或者等待java.util.concurrent库中的Lock或Condition时,会出现这种情况。
计时等待:调用有超时参数的方法,进入计时等待,这一状态将一直保持到超时期满或者接收到适当的通知。
4)被终止的线程,被终止的原因:因为run方法正常退出而自然死亡;因为一个没有捕获的异常终止了run方法而意外死亡。
- 线程属性
1)优先级
每个线程都有优先级,并继承它父线程的优先级。可以用serPriority方法提高或降低任何一线程的优先级
2)守护线程
t.setDaemon(true);设置线程转换为守护线程。守护线程唯一用途是为了其他线程提供服务,应该永远不去访问固有资源,如文件。数据库。
3)线程组
线程组是一个可以统一管理的线程集合。默认情况下,创建的所有线程属于相同的线程组。TreadGroup类实现了Thread.UncaughtExceptionHandler接口。它的uncaughtException方法作如下操作
~ 如果该线程有父线程组,那么父线程组的uncaughtException方法被调用
~ 否则,如果Thread.getDefaultExceptionHandler方法返回一个非空的处理器,则调用该处理器。
~ 否则,如果Throwable是ThreadDeath的一个实例,什么都不做。
~ 否则,线程的名字以及Throwable的栈轨迹被输出到System.err上
4)处理未捕获异常的处理器
线程的run方法不能抛出任何受查异常,但是,非受查异常会导致线程终止。这种情况下,线程就死亡了。就在线程死亡之前,异常被传递到一个用于未捕获异常的处理器,该处理器必须实现Thread.UncaughtExceptionHandler接口的类。
可以用setUncaughtExceptionHandler方法为任何线程安装一个处理器。也可以用Thread类的静态方法setDefaultUncaughtExcetionHandler为所有线程安装一个默认的处理器。
如果不安装默认的处理器,默认的处理器为空。但是,如果不为独立的线程安装处理器,此时的处理器就是该线程的ThreadGroup对象。
二、同步
竞争条件:两个或者两个以上的进程需要共享对同一数据进行存取,修改了同一数据的状态。
锁对象:防止代码块受并发访问的干扰,关键字synchronized、ReentrantLock类,一旦一个线程封锁了锁对象,其他任何线程都无法通过lock语句。当其他线程调用lock时,它们被阻塞,直到第一个线程释放锁对象。
条件对象:通常,线程进入临界区,却发现在某一条件满足之后它才执行。要使用一个条件对象来管理那些已经获得一个锁但是却不能做有用工作的线程。
模拟的银行转账程序:
package synch;
import java.util.Arrays;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Bank {
private final double[] accounts;
private Lock bankLock;
private Condition sufficientFunds;
public Bank(int n, double initialBalance) {
accounts = new double[n];
Arrays.fill(accounts, initialBalance);// 初始化数组
bankLock = new ReentrantLock();// 非公平锁
sufficientFunds = bankLock.newCondition();
}
public void transfer(int from, int to, double amount) throws InterruptedException {
bankLock.lock();
try {
while (accounts[from] < amount) {
sufficientFunds.await();
}
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("%10.2f from %d to %d ", amount, from , to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
sufficientFunds.signalAll();
} finally {
bankLock.unlock();
}
}
public double getTotalBalance() {
bankLock.lock();
try {
double sum = 0;
for (double a : accounts) {
sum += a;
}
return sum;
} finally {
bankLock.unlock();
}
}
public int size() {
return accounts.length;
}
}
package synch;
public class BankTest {
private static final int NACCOUNTS = 100;
private static final double INITIAL_BALANCE = 1000;
private static final double MAX_AMOUNT = 100;
private static final double DELAY = 10;
public static void main(String[] args) {
Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE);
for (int i=0; i<NACCOUNTS; i++) {
int fromAccount = i;
Runnable r = () -> {
try {
while (true) {
int toAccount = (int) (bank.size() * Math.random());
double amount = MAX_AMOUNT * Math.random();
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int) (DELAY * Math.random()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread t = new Thread(r);
t.start();
}
}
}
- synchronized关键字
从1.0版开始,Java中的每一个对象都有一个内部锁。如果一个方法用synchronized关键字声明,那么对象的锁将保护整个方法。
public synchronized void method(){ method body } 等价于
public void method() {
this.intrinsicLock.lock();
try{
method body
}finally{
this.intrinsicLock.unlock();
}
}
内部对象锁只有一个相关条件。wait方法添加一个线程到等待集中,notifyAll或notify方法解除等待线程的阻塞状态。
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
try {
while (accounts[from] < amount) {
wait();
}
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf("%10.2f from %d to %d ", amount, from , to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());
notifyAll();
}finally {
}
}
public synchronized double getTotalBalance() {
try {
double sum = 0;
for (double a : accounts) {
sum += a;
}
return sum;
} finally {
}
}
- Volatile域
volatile关键字为实例域的同步访问提供了一种免锁机制。如果声明一个域为volatile,那么编译器和虚拟机就知道该域是可能被另一个线程并发更新的。
private volatile boolean done;
public boolean isDone(){return done;}
public void setDone(){done = true;}
volatile变量不能提供原子性。例如,方法public void flipDone(){done = !done;} 不能确保翻转域中的值。不能保证读取、翻转和写入不被中断。 - 原子性
java.util.concurrent.atomic包中有很多类使用了很高效的机器级指令(而不是使用锁)来保证其他操作的原子性。
package synch;
import java.util.concurrent.atomic.AtomicLong;
public class AtmoticTest {
public static AtomicLong nextNumber = new AtomicLong();
public static void main(String[] args) {
for (int i=0;i<10;i++) {
Thread t = new Thread(() -> {
long id = nextNumber.incrementAndGet();
System.out.println(Thread.currentThread().getName() + " " + id);
});
t.start();
}
}
}
incrementAndGet方法以原子方式将AtomicLong自增,并返回自增后的值。也就是说,获得值增1并设置然后生成新值的操作不会中断。可以保证即使是多个线程并发访问同一个实例,也会计算并返回正确的值。
compareAndSet完成复杂的更新
do {
oldValue = largest.get();
newValue = Math.max(oldValue, observed);
} while (!largest.compareAndSet(oldValue, newValue));
换成lambda表达式largest.updateAndGet(x->Math.max(x, observed));或largest.accumulateAndGet(observed, Math::max);
accumulateAndGet方法利用一个二元操作符来合并原子值和所提供的参数。
如果有大量线程要访问相同的原子性,性能会大幅降低。Java SE 8提供了LongAdder和LongAccumulator类来解决这个问题。LongAdder包括多个变量,其总和为当前值。多个线程更新不同的加数,线程个数增加时会自动提供新的加数。
final LongAdder adder = new LongAdder();
for (…)
pool.submit(() -> {
while(…){
…
if(…) adder.increment();
}
});
…
long total = adder.sum());
- 线程局部变量
ThreadLocal,在一个给定线程中首次调用get时,会调用initialValue方法。在此之后,get方法会返回属于当前线程的那个实例。
package synch;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadLocalTest {
public static final ThreadLocal<SimpleDateFormat> dateFormat = ThreadLocal.withInitial(()-> new SimpleDateFormat("yyyy-MM-dd"));
public static void main(String[] args) {
String dateStamp = dateFormat.get().format(new Date());
System.out.println(dateStamp);
}
}
随机生成类ThreadLocalRandom.current().nextInt(upperBound);
三、小记
慢慢探索、深入,争取写出点有意义的东西。学习笔记,一步一个脚印。
四、引用
[1]《Java核心技术卷一》