博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDK并发包
阅读量:5231 次
发布时间:2019-06-14

本文共 4553 字,大约阅读时间需要 15 分钟。

synchronized的功能扩展 重入锁
java.util.concurrent.locks.ReentrantLock
package com.longfor.dragonshard.service.cost.standard.impl;import java.util.concurrent.locks.ReentrantLock;public class ReenterLock implements Runnable {    public static ReentrantLock reentrantLock = new ReentrantLock();    public static int i=0;    @Override    public void run() {        for (int j=0;j<10000;j++){            reentrantLock.lock();            try {                i++;            }finally {                reentrantLock.unlock();            }        }    }    public static void main(String[] args) throws InterruptedException{        Thread t1 = new Thread(new ReenterLock());        Thread t2 = new Thread(new ReenterLock());        t1.start();        t2.start();        t1.join();        t2.join();        System.out.println(i);    }}
View Code

   使用读写锁  读读非阻塞  写写 写读 读写 阻塞

   当一个程序读大于写  那么读写锁效率最高 

package com.longfor.dragonshard.service.cost.standard.impl;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockDemo{    private static ReentrantLock reentrantLock = new ReentrantLock();    private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();    private static Lock readLock = readWriteLock.readLock();    private static Lock writeLock = readWriteLock.writeLock();    private int value;    public Object handleRead(Lock lock) throws InterruptedException{        try {            lock.lock();            Thread.sleep(1000);            System.out.println(Thread.currentThread().getName()+"read has done");            return value;        }finally {            lock.unlock();        }    }    public void handleWrite(Lock lock,int value)throws InterruptedException{        try {            lock.lock();            Thread.sleep(1000);            System.out.println(Thread.currentThread().getName()+"write has done");            this.value=value;        }finally {            lock.unlock();        }    }    public static void main(String[] args) throws InterruptedException{        ReadWriteLockDemo readWriteLockDemo = new ReadWriteLockDemo();        Runnable readRunnable = new Runnable(){            @Override            public void run() {                try {                    //readWriteLockDemo.handleRead(readLock);                    readWriteLockDemo.handleRead(reentrantLock);                }catch (InterruptedException e){                    e.printStackTrace();                }            }        };        Runnable writeRunnable = new Runnable(){            @Override            public void run() {                try {                    //readWriteLockDemo.handleWrite(writeLock,2);                    readWriteLockDemo.handleWrite(reentrantLock,2);                }catch(InterruptedException e ){                    e.printStackTrace();                }            }        };        long startTime = System.currentTimeMillis();        for(int i=0;i<18;i++){            new Thread(readRunnable).start();        }        for(int i=18;i<20;i++){            new Thread(writeRunnable).start();        }        long endTime = System.currentTimeMillis();        System.out.println("the program is running for :"+(endTime-startTime)+"mill seconds ...");    }}
View Code

   倒计时器 CountDownLatch  多线程控制工具类  意为:门闩  就是检查所有程序完最后执行   每次执行完程序 countDown()  最后要求所有程序检查完毕 await()

package com.longfor.dragonshard.service.cost.standard.impl;import java.util.Random;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CountDownLatchTest implements Runnable{    private static CountDownLatch countDownLatch = new CountDownLatch(10);    private static CountDownLatchTest countDownLatchTest = new CountDownLatchTest();    @Override    public void run() {        try {            Thread.sleep(new Random().nextInt(10)*1000);            System.out.println("check complete");            countDownLatch.countDown();        }catch (InterruptedException e){            e.printStackTrace();        }    }    public static void main(String[] args) throws InterruptedException{        ExecutorService executorService = Executors.newFixedThreadPool(10);        for(int i=0;i<10;i++){            executorService.submit(countDownLatchTest);        }        countDownLatch.await();        System.out.println("Fire!");        executorService.shutdown();    }}
View Code

 

 
 

转载于:https://www.cnblogs.com/bockpecehhe/p/9275267.html

你可能感兴趣的文章
SpringBoot-静态资源映射
查看>>
SpringBoot-webjars
查看>>
SpringBoot-thymeleaf
查看>>
IDEA 调试 JAVA ConcurrentLinkedQueue
查看>>
P1908-逆序对
查看>>
P1192-台阶问题
查看>>
ACM模板——康托展开
查看>>
P1025-数的划分
查看>>
P1305-新二叉树
查看>>
LGTB 与大数
查看>>
[POI2009]KAM-Pebbles
查看>>
JavaScript对象
查看>>
bzoj 3696: 化合物
查看>>
LeetCode 28. Implement strStr()
查看>>
LeetCode 15. 3Sum
查看>>
SignalR示例demo
查看>>
实验七——函数定义及调用总结
查看>>
apple-touch-startup-image 制作iphone web应用程序的启动画面
查看>>
Dp Hdu1421 搬寝室
查看>>
C/C++中的可变参函数
查看>>