1、什么是JUC
JUC指的是java.util三个并发编程工具包
- java.util.concurrent
- java.util.concurrent.atomic
- java.util.concurrent.locks
2、线程的几种状态
public enum State{
NEW,//新建
RUNNABLE,//准备就绪,等待资源
BLOCKED,//阻塞
WAITING,//一直等待
TIMED_WAITING,//超时等待,过时不候
TERMINATED;//终止,线程已经完成执行
}
3、wait和sleep的区别
1、来自不同的类
wait来自Object类,任何对象实例都能调用
sleep来自Thread类,是Thread的静态方法
2、锁的释放
wait会释放锁
sleep不需要占用锁,不会释放锁
3、使用范围、捕获异常不同
wait:必须在同步代码块中使用,不需要捕获异常
sleep:可以在任何地方使用,必须捕获异常
4、线程间通信
4.1 生产者和消费者
生产者与消费者是java并发环境下常见的设计模式,一个线程负责生产数据,一个线程负责消费数据,两个线程同时去操作这个变量,但是这是两个相互互斥的操作。
步骤:
(1)创建资源类,在资源类中创建属性和方法
(2)资源类的方法
判断等待-》业务逻辑-》通知唤醒
(3)创建多个线程,调用资源类的方法
(4)防止虚假唤醒
线程操作资源类-》判断干活-》通知,while防止虚假唤醒
虚假唤醒:线程可以被唤醒,而不会被通知,中断或超时
因此等待应该总是出现在循环中,如果条件不满足,则继续等待
实现方式1-synchronized
public class Mythread2 {
public static void main(String[] args) {
Resource resource = new Resource();//资源类
//生产者
new Thread(new Runnable() {
@Override
public void run() {
try{
resource.increment();
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"producer").start();
//消费者
new Thread(new Runnable() {
@Override
public void run() {
try{
resource.decrement();
}catch(InterruptedException e){
e.printStackTrace();
}
}
},"consumer").start();
}
}
class Resource{
int number=1;
public synchronized void increment() throws InterruptedException{
while(number!=0){//判断
this.wait();
}
number++;//干活
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException{
while (number==0){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
}
实现方式2-JUC-lock
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyThread3 {
}
class Resource1{
private int number=0;
Lock lock=new ReentrantLock();
Condition condition=lock.newCondition();
public void increment()throws InterruptedException{
lock.lock();
try{
while (number!=0){
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void decrement() throws InterruptedException{
lock.lock();
try{
while(number==0){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"=》"+number);
condition.signalAll();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
lock方式与synchronized的区别
与synchronized的区别:
lock中的condition可以实现精准定位
如下示例,实现了线程顺序按照A-B-C的顺序执行
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyThread3 {
public static void main(String[] args) {
Resource1 resource1=new Resource1();
new Thread(new Runnable() {
@Override
public void run() {
try{
for (int i=0;i<10;i++){
resource1.printA();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
},"A").start();
new Thread(new Runnable() {
@Override
public void run() {
try{
for (int i=0;i<10;i++){
resource1.printB();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
},"B").start();
new Thread(new Runnable() {
@Override
public void run() {
try{
for (int i=0;i<10;i++){
resource1.printC();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
},"C").start();
}
}
class Resource1{
private int number=1;
Lock lock=new ReentrantLock();
Condition condition1=lock.newCondition();
Condition condition2=lock.newCondition();
Condition condition3=lock.newCondition();
public void printA()throws InterruptedException{
// System.out.println(Thread.currentThread().getName()+"=>"+number);
lock.lock();
try{
while (number!=1){
condition1.await();
}
number=2;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition2.signalAll();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printB()throws InterruptedException{
// System.out.println(Thread.currentThread().getName()+"=>"+number);
lock.lock();
try{
while (number!=2){
condition2.await();
}
number=3;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition3.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printC()throws InterruptedException{
// System.out.println(Thread.currentThread().getName()+"=>"+number);
lock.lock();
try{
while (number!=3){
condition2.await();
}
number=1;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition1.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
5、常用的辅助类
(1)CountDownLatch-减法计数器
用给定的数字进行初始化,等待所有线程执行完(await),才进行下一步。使用countDown方法使当前计数器到0,之后所有等待线程被释放。一次性操作,无法重置计数,如果需要重置计数,可以使用CyclicBarrier。
import java.util.concurrent.CountDownLatch;
public class CountDownLanch1 {
public static void main(String[] args) throws InterruptedException{
// 倒计时 起始为6 必须要执行任务的时候,再使用!
CountDownLatch countDownLatch=new CountDownLatch(6);
for (int i=0;i<6;i++){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"->线程");
countDownLatch.countDown();//计数器-1
}
},String.valueOf(i)).start();
}
countDownLatch.await();//待计数器归零在向下执行
System.out.println("线程全部执行完毕");
}
}
2、CyclicBarrier(加法计数器)
线程加法,只有等到一定数量的线程都开启后,才进行下一步(执行run方法)
比如:集齐7颗龙珠,才能召唤神龙
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier barrier=new CyclicBarrier(7,new Thread(new Runnable() {
@Override
public void run() {
System.out.println("集齐龙珠,召唤神龙");
}
}) );
for(int i=1;i<=7;i++){
final int temp=i;
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "第" + temp + "颗龙珠");
try{
barrier.await();//等待
}catch (Exception e){
e.printStackTrace();
}
}
},String.valueOf(i)).start();
}
}
}
3、Semaphore(信号量)
计数信号量,允许优先的多线程去访问公共资源,如:6个车抢3个停车位
原理:
semaphore.acquire(); //获得,如果已经满了,等待,等待被释放为止!
semaphore.release(); // 释放,会将当前的信号量释放+1,然后唤醒等待的线程!
作用:多个共享资源互斥的使用!并发限流,控制最大的线程数!
import java.sql.Time;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class SemaphoreDemo {
public static void main(String[] args) {
//指定允许的线程数量:车位
Semaphore semaphore = new Semaphore(3);
for(int i=1;i<=6;i++){
new Thread(new Runnable() {
@Override
public void run() {
try{
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "抢到车位");
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"离开车位");
}catch(InterruptedException e){
e.printStackTrace();
}finally {
semaphore.release();
}
}
},String.valueOf(i)).start();
}
}
}
4.读写锁
写锁:独占锁
读锁:共享锁
读写锁:一个资源可以被多个读线程访问,或者被一个写线程访问,但是不能同时存在读写线程,读写互斥,读读是共享的
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCache myCache=new MyCache();
//写线程
for (int i=0;i<5;i++){
final int temp=i;
//写入
new Thread(new Runnable() {
@Override
public void run() {
myCache.put(temp+"",""+temp);
}
},String.valueOf(i)).start();
}
//读线程
for(int i=0;i<5;i++){
final int temp=i;
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("获取"+temp+"缓存数据=="+myCache.get(temp+""));
}
},String.valueOf(i)).start();
}
}
}
class MyCache{
private volatile Map<String,Object> map=new HashMap<>();
private ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
//写存
public void put(String key,Object value){
readWriteLock.writeLock().lock();//写锁
System.out.println(Thread.currentThread().getName()+"写入"+key);
try{
TimeUnit.MILLISECONDS.sleep(200);
map.put(key,value);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
readWriteLock.writeLock().unlock();//写完释放锁
}
System.out.println(Thread.currentThread().getName() + "写入OK");
}
//读取
public Object get(String key){
readWriteLock.readLock().lock();//加读锁
Object object=null;
try{
System.out.println(Thread.currentThread().getName() + "读取" + key);
object=map.get(key);
System.out.println(Thread.currentThread().getName() + "读取OK");
}catch (Exception e){
e.printStackTrace();
}finally {
readWriteLock.readLock().unlock();//释放读锁
}
return object;
}
}
参考文章:
线程的5个状态
JUC基础