Java进程&线程(整理)

时间:2023-03-08 22:30:45
Java进程&线程(整理)

Java进程&线程

程序:程序员写的代码,就是代码,不运行好像不会发生什么;

进程:一个进程可以理解为“运行的”一个程序,当我们启动一个java程序后,对应的jvm就会创建一个进程;

线程:jvm有一个进程,然而程序的实际执行是通过线程来完成的,进程之间是相互独立的,而线程之间是共享进程的资源的,就是说,进程是由n个线程组成的,而main函数就是进程创建后启动的主线程,另外,有一个用于垃圾回收的线程也是会事先启动的,所以说,一个java程序运行后,至少包含了2个线程(可能还会有其它的);

 

实现多线程的几种方式:最常用的,继承Thread或者实现Runnable接口,还有我们可能不怎么熟悉的使用ExecutorService,Callable,Future实现有返回结果的多线程,它们都是属于Executor框架中的功能类,可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须实现Runnable接口。执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了,再结合线程池接口ExecutorService就可以实现传说中有返回结果的多线程了;

线程池概念的好处:pool,顾名思义,就是一个容器,装了很多线程,一般来说,降低资源消耗:通过重用已经创建的线程来降低线程创建和销毁的消耗,提高响应速度:任务到达时不需要等待线程创建就可以立即执行,提高线程的可管理性:线程池可以统一管理、分配、调优和监控,这3点是我们看重的;

Executor框架:Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等,并发编程的一种编程方式是把任务拆分为一些列的小任务,即Runnable,然后再提交给一个Executor执行,Executor.execute(Runnalbe)。Executor在执行时使用内部的线程池完成操作。CompletionService:调用CompletionService的take方法时,会返回按完成顺序放回任务的结果,CompletionService内部维护了一个阻塞队列BlockingQueue,如果没有任务完成,take()方法也会阻塞;

一个例子:

package executorservice;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask; public class ConcurrentCalculator { private ExecutorService exec;
private int cpuCoreNumber;
private List<Future<Long>> tasks = new ArrayList<Future<Long>>();
Map<String,String> mapM = new HashMap<String,String>();
// 内部类
class SumCalculator implements Callable<Long> {
private int[] numbers;
private int start;
private int end; public SumCalculator(final int[] numbers, int start, int end) {
this.numbers = numbers;
this.start = start;
this.end = end;
} public Long call() throws Exception {
Long sum = 0l;
for (int i = start; i < end; i++) {
sum += numbers[i];
}
return sum;
}
} public ConcurrentCalculator() {
cpuCoreNumber = Runtime.getRuntime().availableProcessors();
exec = Executors.newFixedThreadPool(cpuCoreNumber);
} public Long sum(final int[] numbers) {
// 根据CPU核心个数拆分任务,创建FutureTask并提交到Executor
for (int i = 0; i < cpuCoreNumber; i++) {
int increment = numbers.length / cpuCoreNumber + 1;
int start = increment * i;
int end = increment * i + increment;
if (end > numbers.length)
end = numbers.length;
SumCalculator subCalc = new SumCalculator(numbers, start, end);
FutureTask<Long> task = new FutureTask<Long>(subCalc);
tasks.add(task);
if (!exec.isShutdown()) {
exec.submit(task);
}
}
return getResult();
} /**
* 迭代每个只任务,获得部分和,相加返回
*
* @return
*/
public Long getResult() {
Long result = 0l;
for (Future<Long> task : tasks) {
try {
// 如果计算未完成则阻塞
Long subSum = task.get();
result += subSum;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return result;
} public void close() {
exec.shutdown();
}
}

使用CompletionService改进:

package executorservice;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger; public class ConcurrentCalculator2 { private ExecutorService exec;
private CompletionService<Long> completionService;
AtomicInteger ai;
private int cpuCoreNumber; // 内部类
class SumCalculator implements Callable<Long> {
private int[] numbers;
private int start;
private int end; public SumCalculator(final int[] numbers, int start, int end) {
this.numbers = numbers;
this.start = start;
this.end = end;
} public Long call() throws Exception {
Long sum = 0l;
for (int i = start; i < end; i++) {
sum += numbers[i];
}
return sum;
}
} public ConcurrentCalculator2() {
cpuCoreNumber = Runtime.getRuntime().availableProcessors();
exec = Executors.newFixedThreadPool(cpuCoreNumber);
completionService = new ExecutorCompletionService<Long>(exec); } public Long sum(final int[] numbers) {
// 根据CPU核心个数拆分任务,创建FutureTask并提交到Executor
for (int i = 0; i < cpuCoreNumber; i++) {
int increment = numbers.length / cpuCoreNumber + 1;
int start = increment * i;
int end = increment * i + increment;
if (end > numbers.length)
end = numbers.length;
SumCalculator subCalc = new SumCalculator(numbers, start, end);
if (!exec.isShutdown()) {
completionService.submit(subCalc); } }
return getResult();
} /**
* 迭代每个只任务,获得部分和,相加返回
*
* @return
*/
public Long getResult() {
Long result = 0l;
for (int i = 0; i < cpuCoreNumber; i++) {
try {
Long subSum = completionService.take().get();
result += subSum;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return result;
} public void close() {
exec.shutdown();
}
}

ThreadPoolExecutor:它是一个ExecutorService,它使用可能的几个池线程之一执行每个提交的任务,通常使用Executors
工厂方法配置;当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程,当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行,当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务,当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理,当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程,当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭;

Java进程&线程(整理)

ThreadPoolExecutor

Executors提供的线程池配置方案,从而生成不同的ExecutorService;

注意非阻塞队列和阻塞队列,*和有界队列:用ThreadPoolExecutor自定义线程池,看线程是的用途,如果任务量不大,可以用*队列,如果任务量非常大,要用有界队列,防止OOM,如果任务量很大,还要求每个任务都处理成功,要对提交的任务进行阻塞提交,重写拒绝机制,改为阻塞提交,保证不抛弃一个任务,最大线程数一般设为2N+1最好,N是CPU核数,核心线程数,看应用,如果是任务,一天跑一次,设置为0,合适,因为跑完就停掉了,如果是常用线程池,看任务量,是保留一个核心还是几个核心线程数,如果要获取任务执行结果,用CompletionService,但是注意,获取任务的结果的要重新开一个线程获取,如果在主线程获取,就要等任务都提交后才获取,就会阻塞大量任务结果,队列过大OOM,所以最好异步开个线程获取结果;

线程安全:线程安全无非是要控制多个线程对某个资源的访问或修改,感觉这个说的非常明了,对于java的内存模型来说,要解决可见性和有序性;

那么,何谓可见性?多个线程之间是不能互相传递数据通信的,它们之间的沟通只能通过共享变量来进行。Java内存模型(JMM)规定了jvm有主内存,主内存是多个线程共享的。当new一个对象的时候,也是被分配在主内存中,每个线程都有自己的工作内存,工作内存存储了主存的某些对象的副本,当然线程的工作内存大小是有限制的。当线程操作某个对象时,执行顺序如下:

(1) 从主存复制变量到当前工作内存 (read and load);

(2) 执行代码,改变共享变量值 (use and assign);

(3) 用工作内存数据刷新主存相关内容 (store and write);

JVM规范定义了线程对主存的操作指令:read,load,use,assign,store,write。当一个共享变量在多个线程的工作内存中都有副本时,如果一个线程修改了这个共享变量,那么其他线程应该能够看到这个被修改后的值,这就是多线程的可见性问题。

那么,什么是有序性呢?线程在引用变量时不能直接从主内存中引用,如果线程工作内存中没有该变量,则会从主内存中拷贝一个副本到工作内存中,这个过程为read-load,完成后线程会引用该副本。当同一线程再度引用该字段时,有可能重新从主存中获取变量副本(read-load-use),也有可能直接引用原来的副本(use),也就是说read,load,use顺序可以由JVM实现系统决定。

线程不能直接为主存中中字段赋值,它会将值指定给工作内存中的变量副本(assign),完成后这个变量副本会同步到主存储区(store- write),至于何时同步过去,根据JVM实现系统决定,有的字段,则会从主内存中将该字段赋值到工作内存中,这个过程为read-load,完成后线程会引用该变量副本,当同一线程多次重复对字段赋值时,比如:

for(int i=0;i<10;i++)

a++;

线程有可能只对工作内存中的副本进行赋值,只到最后一次赋值后才同步到主存储区,所以assign,store,weite顺序可以由JVM实现系统决定。假设有一个共享变量x,线程a执行x=x+1。从上面的描述中可以知道x=x+1并不是一个原子操作,它的执行过程如下:

1:从主存中读取变量x副本到工作内存;

2:给x加1;

3:将x加1后的值写回主存;

如果另外一个线程b执行x=x-1,执行过程如下:

1:从主存中读取变量x副本到工作内存;

2:给x减1;

3:将x减1后的值写回主存 ;

那么显然,最终的x的值是不可靠的。假设x现在为10,线程a加1,线程b减1,从表面上看,似乎最终x还是为10,但是多线程情况下会有这种情况发生:

1:线程a从主存读取x副本到工作内存,工作内存中x值为10;

2:线程b从主存读取x副本到工作内存,工作内存中x值为10;

3:线程a将工作内存中x加1,工作内存中x值为11;

4:线程a将x提交主存中,主存中x为11;

5:线程b将工作内存中x值减1,工作内存中x值为9;

6:线程b将x提交到中主存中,主存中x为9;

同样,x有可能为11,如果x是一个银行账户,线程a存款,线程b扣款,显然这样是有严重问题的,要解决这个问题,必须保证线程a和线程b是有序执行的,并且每个线程执行的加1或减1是一个原子操作。看看下面代码:

public class Account {

private int balance;

public Account(int balance) {

this.balance = balance;

}

public int getBalance() {

return balance;

}

public void add(int num) {

balance = balance + num;

}

public void withdraw(int num) {

balance = balance - num;

}

public static void main(String[] args) throws InterruptedException {

Account account = new Account(1000);

Thread a = new Thread(new AddThread(account, 20), "add");

Thread b = new Thread(new WithdrawThread(account, 20), "withdraw");

a.start();

b.start();

a.join();

b.join();

System.out.println(account.getBalance());

}

static class AddThread implements Runnable {

Account account;

int     amount;

public AddThread(Account account, int amount) {

this.account = account;

this.amount = amount;

}

public void run() {

for (int i = 0; i < 200000; i++) {

account.add(amount);

}

}

}

static class WithdrawThread implements Runnable {

Account account;

int     amount;

public WithdrawThread(Account account, int amount) {

this.account = account;

this.amount = amount;

}

public void run() {

for (int i = 0; i < 100000; i++) {

account.withdraw(amount);

}

}

}

}

第一次执行结果为10200,第二次执行结果为1060,每次执行的结果都是不确定的,因为线程的执行顺序是不可预见的。这是java同步产生的根源,synchronized关键字保证了多个线程对于同步块是互斥的,synchronized作为一种同步手段,解决java多线程的执行有序性和内存可见性,而volatile关键字之解决多线程的内存可见性问题。后面将会详细介绍。

synchronized关键字:

上面说了,java用synchronized关键字做为多线程并发环境的执行有序性的保证手段之一。当一段代码会修改共享变量,这一段代码成为互斥区或 临界区,为了保证共享变量的正确性,synchronized标示了临界区。典型的用法如下:

synchronized(锁){

临界区代码

}

为了保证银行账户的安全,可以操作账户的方法如下:

public synchronized void add(int num) {

balance = balance + num;

}

public synchronized void withdraw(int num) {

balance = balance - num;

}

刚才不是说了synchronized的用法是这样的吗:

synchronized(锁){

临界区代码

}

那么对于public synchronized void add(int num)这种情况,意味着什么呢?其实这种情况,锁就是这个方法所在的对象。同理,如果方法是public static synchronized void add(int num),那么锁就是这个方法所在的class。

理论上,每个对象都可以做为锁,但一个对象做为锁时,应该被多个线程共享,这样才显得有意义,在并发环境下,一个没有共享的对象作为锁是没有意义的。假如有这样的代码:

public class ThreadTest{

public void test(){

Object lock=new Object();

synchronized (lock){

//do something

}

}

}

lock变量作为一个锁存在根本没有意义,因为它根本不是共享对象,每个线程进来都会执行Object lock=new Object();每个线程都有自己的lock,根本不存在锁竞争。

每个锁对象都有两个队列,一个是就绪队列,一个是阻塞队列,就绪队列存储了将要获得锁的线程,阻塞队列存储了被阻塞的线程,当一个被线程被唤醒 (notify)后,才会进入到就绪队列,等待cpu的调度。当一开始线程a第一次执行account.add方法时,jvm会检查锁对象account的就绪队列是否已经有线程在等待,如果有则表明account的锁已经被占用了,由于是第一次运行,account的就绪队列为空,所以线程a获得了锁,
执行account.add方法。如果恰好在这个时候,线程b要执行account.withdraw方法,因为线程a已经获得了锁还没有释放,所以线程b要进入account的就绪队列,等到得到锁后才可以执行。

一个线程执行临界区代码过程如下:

1:获得同步锁;

2:清空工作内存;

3:从主存拷贝变量副本到工作内存;

4:对这些变量计算;

5:将变量从工作内存写回到主存;

6:释放锁;

可见,synchronized既保证了多线程的并发有序性,又保证了多线程的内存可见性(也有缺陷哦,性能,死锁都是问题)。

生产者/消费者模式:

生产者/消费者模式其实是一种很经典的线程同步模型,很多时候,并不是光保证多个线程对某共享资源操作的互斥性就够了,往往多个线程之间都是有协作的。

假设有这样一种情况,有一个桌子,桌子上面有一个盘子,盘子里只能放一颗鸡蛋,A专门往盘子里放鸡蛋,如果盘子里有鸡蛋,则一直等到盘子里没鸡蛋,B专门从盘子里拿鸡蛋,如果盘子里没鸡蛋,则等待直到盘子里有鸡蛋。其实盘子就是一个互斥区,每次往盘子放鸡蛋应该都是互斥的,A的等待其实就是主动放弃锁,B等待时还要提醒A放鸡蛋。

如何让线程主动释放锁?

很简单,调用锁的wait()方法就好。wait方法是从Object来的,所以任意对象都有这个方法。看这个代码片段:

Object lock=new Object();//声明了一个对象作为锁

synchronized (lock) {

balance = balance - num;

//这里放弃了同步锁,好不容易得到,又放弃了

lock.wait();

}

如果一个线程获得了锁lock,进入了同步块,执行lock.wait(),那么这个线程会进入到lock的阻塞队列。如果调用lock.notify()则会通知阻塞队列的某个线程进入就绪队列。

声明一个盘子,只能放一个鸡蛋:

import java.util.ArrayList;

import java.util.List;

public class Plate {

List<Object> eggs = new ArrayList<Object>();

public synchronized Object getEgg() {

while(eggs.size() == 0) {

try {

wait();

} catch (InterruptedException e) {

}

}

Object egg = eggs.get(0);

eggs.clear();// 清空盘子

notify();// 唤醒阻塞队列的某线程到就绪队列

System.out.println("拿到鸡蛋");

return egg;

}

public synchronized void putEgg(Object egg) {

while(eggs.size() > 0) {

try {

wait();

} catch (InterruptedException e) {

}

}

eggs.add(egg);// 往盘子里放鸡蛋

notify();// 唤醒阻塞队列的某线程到就绪队列

System.out.println("放入鸡蛋");

}

static class AddThread extends Thread{

private Plate plate;

private Object egg=new Object();

public AddThread(Plate plate){

this.plate=plate;

}

public void run(){

for(int i=0;i<5;i++){

plate.putEgg(egg);

}

}

}

static class GetThread extends Thread{

private Plate plate;

public GetThread(Plate plate){

this.plate=plate;

}

public void run(){

for(int i=0;i<5;i++){

plate.getEgg();

}

}

}

public static void main(String args[]){

try {

Plate plate=new Plate();

Thread add=new Thread(new AddThread(plate));

Thread get=new Thread(new GetThread(plate));

add.start();

get.start();

add.join();

get.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("测试结束");

}

}

执行结果:

放入鸡蛋

拿到鸡蛋

放入鸡蛋

拿到鸡蛋

放入鸡蛋

拿到鸡蛋

放入鸡蛋

拿到鸡蛋

放入鸡蛋

拿到鸡蛋

测试结束

声明一个Plate对象为plate,被线程A和线程B共享,A专门放鸡蛋,B专门拿鸡蛋。假设

1:开始,A调用plate.putEgg方法,此时eggs.size()为0,因此顺利将鸡蛋放到盘子,还执行了notify()方法,唤醒锁的阻塞队列的线程,此时阻塞队列还没有线程;

2:又有一个A线程对象调用plate.putEgg方法,此时eggs.size()不为0,调用wait()方法,自己进入了锁对象的阻塞队列;

3:此时,来了一个B线程对象,调用plate.getEgg方法,eggs.size()不为0,顺利的拿到了一个鸡蛋,还执行了notify()方法,唤
醒锁的阻塞队列的线程,此时阻塞队列有一个A线程对象,唤醒后,它进入到就绪队列,就绪队列也就它一个,因此马上得到锁,开始往盘子里放鸡蛋,此时盘子是 空的,因此放鸡蛋成功;

4:假设接着来了线程A,就重复2;假设来料线程B,就重复3;

整个过程都保证了放鸡蛋,拿鸡蛋,放鸡蛋,拿鸡蛋。

volatile关键字:

volatile是java提供的一种同步手段,只不过它是轻量级的同步,为什么这么说,因为volatile只能保证多线程的内存可见性,不能保证多线程的执行有序性。而最彻底的同步要保证有序性和可见性,例如synchronized。任何被volatile修饰的变量,都不拷贝副本到工作内存,任何修改都及时写在主存。因此对于Valatile修饰的变量的修改,所有线程马上就能看到,但是volatile不能保证对变量的修改是有序的。什么意思呢?假如有这样的代码:

public class VolatileTest{

public volatile int a;

public void add(int count){

a=a+count;

}

}

当一个VolatileTest对象被多个线程共享,a的值不一定是正确的,因为a=a+count包含了好几步操作,而此时多个线程的执行是无序的,因为没有任何机制来保证多个线程的执行有序性和原子性。volatile存在的意义是,任何线程对a的修改,都会马上被其他线程读取到,因为直接操作主存,没有线程对工作内存和主存的同步。所以,volatile的使用场景是有限的,在有限的一些情形下可以使用volatile变量替代锁。要使volatile变量提供理想的线程安全,必须同时满足下面两个条件:

1)对变量的写操作不依赖于当前值;

2)该变量没有包含在具有其他变量的不变式中;

volatile只保证了可见性,所以Volatile适合直接赋值的场景,如:

public class VolatileTest{

public volatile int a;

public void setA(int a){

this.a=a;

}

}

在没有volatile声明时,多线程环境下,a的最终值不一定是正确的,因为this.a=a;涉及到给a赋值和将a同步回主存的步骤,这个顺序可能被打乱。如果用volatile声明了,读取主存副本到工作内存和同步a到主存的步骤,相当于是一个原子操作。所以简单来说,volatile适合这种场景:一个变量被多个线程共享,线程直接给这个变量赋值。这是一种很简单的同步场景,这时候使用volatile的开销将会非常小。

原子类:AtomicInteger:

源码:

/*

* @(#)AtomicInteger.java 1.11 06/06/15

*

* Copyright 2006 Sun Microsystems, Inc. All rights reserved.

* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

*/

package java.util.concurrent.atomic;

import sun.misc.Unsafe;

/**

* An {@code int} value that may be updated atomically.  See the

* {@link java.util.concurrent.atomic} package specification for

* description of the properties of atomic variables. An

* {@code AtomicInteger} is used in applications such as atomically

* incremented counters, and cannot be used as a replacement for an

* {@link java.lang.Integer}. However, this class does extend

* {@code Number} to allow uniform access by tools and utilities that

* deal with numerically-based classes.

*

* @since 1.5

* @author Doug Lea

*/

public class AtomicInteger extends Number implements java.io.Serializable {

private static final long serialVersionUID = 6214790243416807050L;

// setup to use Unsafe.compareAndSwapInt for updates

private static final Unsafe unsafe = Unsafe.getUnsafe();

private static final long valueOffset;

static {

try {

valueOffset = unsafe.objectFieldOffset

(AtomicInteger.class.getDeclaredField("value"));

} catch (Exception ex) { throw new Error(ex); }

}

private volatile int value;

/**

* Creates a new AtomicInteger with the given initial value.

*

* @param initialValue the initial value

*/

public AtomicInteger(int initialValue) {

value = initialValue;

}

/**

* Creates a new AtomicInteger with initial value {@code 0}.

*/

public AtomicInteger() {

}

/**

* Gets the current value.

*

* @return the current value

*/

public final int get() {

return value;

}

/**

* Sets to the given value.

*

* @param newValue the new value

*/

public final void set(int newValue) {

value = newValue;

}

/**

* Eventually sets to the given value.

*

* @param newValue the new value

* @since 1.6

*/

public final void lazySet(int newValue) {

unsafe.putOrderedInt(this, valueOffset, newValue);

}

/**

* Atomically sets to the given value and returns the old value.

*

* @param newValue the new value

* @return the previous value

*/

public final int getAndSet(int newValue) {

for (;;) {

int current = get();

if (compareAndSet(current, newValue))

return current;

}

}

/**

* Atomically sets the value to the given updated value

* if the current value {@code ==} the expected value.

*

* @param expect the expected value

* @param update the new value

* @return true if successful. False return indicates that

* the actual value was not equal to the expected value.

*/

public final boolean compareAndSet(int expect, int update) {

return unsafe.compareAndSwapInt(this, valueOffset, expect, update);

}

/**

* Atomically sets the value to the given updated value

* if the current value {@code ==} the expected value.

*

* <p>May <a href="package-summary.html#Spurious">fail spuriously</a>

* and does not provide ordering guarantees, so is only rarely an

* appropriate alternative to {@code compareAndSet}.

*

* @param expect the expected value

* @param update the new value

* @return true if successful.

*/

public final boolean weakCompareAndSet(int expect, int update) {

return unsafe.compareAndSwapInt(this, valueOffset, expect, update);

}

/**

* Atomically increments by one the current value.

*

* @return the previous value

*/

public final int getAndIncrement() {

for (;;) {

int current = get();

int next = current + 1;

if (compareAndSet(current, next))

return current;

}

}

/**

* Atomically decrements by one the current value.

*

* @return the previous value

*/

public final int getAndDecrement() {

for (;;) {

int current = get();

int next = current - 1;

if (compareAndSet(current, next))

return current;

}

}

/**

* Atomically adds the given value to the current value.

*

* @param delta the value to add

* @return the previous value

*/

public final int getAndAdd(int delta) {

for (;;) {

int current = get();

int next = current + delta;

if (compareAndSet(current, next))

return current;

}

}

/**

* Atomically increments by one the current value.

*

* @return the updated value

*/

public final int incrementAndGet() {

for (;;) {

int current = get();

int next = current + 1;

if (compareAndSet(current, next))

return next;

}

}

/**

* Atomically decrements by one the current value.

*

* @return the updated value

*/

public final int decrementAndGet() {

for (;;) {

int current = get();

int next = current - 1;

if (compareAndSet(current, next))

return next;

}

}

/**

* Atomically adds the given value to the current value.

*

* @param delta the value to add

* @return the updated value

*/

public final int addAndGet(int delta) {

for (;;) {

int current = get();

int next = current + delta;

if (compareAndSet(current, next))

return next;

}

}

/**

* Returns the String representation of the current value.

* @return the String representation of the current value.

*/

public String toString() {

return Integer.toString(get());

}

public int intValue() {

return get();

}

public long longValue() {

return (long)get();

}

public float floatValue() {

return (float)get();

}

public double doubleValue() {

return (double)get();

}

}

compareAndSet调用Unsafe来实现:

private static final Unsafe unsafe = Unsafe.getUnsafe();

compareAndSet方法首先判断当前值是否等于current;

如果当前值 = current
,说明AtomicInteger的值没有被其他线程修改;

如果当前值 != current,说明AtomicInteger的值被其他线程修改了,这时会再次进入循环重新比较;

java提供的原子操作可以原子更新的基本类型有以下三个:

1,AtomicBoolean;

2,AtomicInteger;

3,AtomicLong;

java提供的原子操作,还可以原子更新以下类型的值:

1,原子更新数组,Atomic包提供了以下几个类:AtomicIntegerArray、AtomicLongArray、AtomicReferenceArray;

2,原子更新引用类型,也就是更新实体类的值,比如AtomicReference<User>:

AtomicReference:原子更新引用类型的值;

AtomicReferenceFieldUpdater:原子更新引用类型里的字段;

AtomicMarkableReference:原子更新带有标记位的引用类型;

3,原子更新字段值:

AtomicIntegerFieldUpdater:原子更新整形的字段的更新器;

AtomicLongFieldUpdater:原子更新长整形的字段的更新器;

AtomicStampedReference:原子更新带有版本号的引用类型的更新器;

未完待续,学到什么再加上~~~~~~

@Arya0624