1. 程式人生 > >JDK並發包

JDK並發包

每次 return down img 倒計時器 handler demo turn main

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

 


JDK並發包