java Thread深入了解(三)

volatile

如下代碼

static byte value=0;
static boolean finish=false;

public static void testVolatile() throws InterruptedException {
    value=0;
    finish=false;
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(value==0&&!finish);
            console.info("value is "+value+" finish is "+finish);
        }
    }).start();
    Thread.sleep(1000);
    new Thread(new Runnable() {
        @Override
        public void run() {
            value=10;
            finish=true;
            console.info("has set the finish");
        }
    }).start();
}

第一個(gè)線程的死循環(huán) 并不會(huì)因?yàn)榈诙€(gè)線程的設(shè)置變量結(jié)束,這是因?yàn)閏pu緩存的問(wèn)題

volatile static byte value=0;

如果將value的改為volatile變量那么第一個(gè)線程就會(huì)如期結(jié)束。因?yàn)槊看巫x取value并不去使用緩存

concurrent的BlockingQueue

BlockingQueue是一個(gè)線程安全的隊(duì)列接口 ,提供了 put take poll等方法
實(shí)現(xiàn)類(lèi)有 ArrayBlockingQueue LinkedBlockingQueue PriorityBlockingQueue

public static class Message implements Comparable<Message>{

    public int value=0;

    public Message(int value) {
        this.value = value;
    }

    @Override
    public int compareTo(Message o) {
        return value-o.value;
    }
}

public static void blockingQueueTest(){
    PriorityBlockingQueue <Message> blockingDeque=new PriorityBlockingQueue <Message>();
    new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i=0;i<100;i++) {
                try {
                    console.info(blockingDeque.take().value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    },"消費(fèi)者").start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            blockingDeque.put(new Message(100));
            blockingDeque.put(new Message(0));
            blockingDeque.put(new Message(0));
            blockingDeque.put(new Message(100));
        }
    },"生產(chǎn)者").start();
}

看到消費(fèi)者拿到的消息是排序過(guò)的

concurren包的線程池

Executors.newScheduledThreadPool 創(chuàng)建固定大小的線程池
Executors.newFixedThreadPool 創(chuàng)建固定大小的線程池
Executors.newCachedThreadPool 創(chuàng)建緩存線程池 從0 開(kāi)始 每個(gè)線程存貨一分鐘
Executors.newSingleThreadExecutor 創(chuàng)建只有一個(gè)線程的線程池

實(shí)際上都是創(chuàng)建了一個(gè)ThreadPoolExecutor 控制了不同參數(shù)

 /**
  *
  *  corePoolSize 線程池最小保持線程數(shù) 即是他們處于空閑狀態(tài)
  *  maximumPoolSize 線程池最大線程數(shù)
  *  keepAliveTime 如果線程數(shù)超出最小保持 那么線程最大空閑存活實(shí)際
  *  unit 存活時(shí)間單位單位
  *  初始工作的緩沖區(qū)
  */
 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
                              }

ReentrantReadWriteLock

讀寫(xiě)鎖 把讀和寫(xiě)分別變成鎖 比synchronized更輕量控制更方便
讀鎖排斥寫(xiě)鎖 不排斥讀鎖
寫(xiě)鎖排斥其他鎖

public static void ReadWriteLock(){
    ReadWriteLock reentrantLock=new ReentrantReadWriteLock();
    Lock readLock=reentrantLock.readLock();
    Lock writeLock=reentrantLock.writeLock();
    for(int i=0;i<2;i++)
    new Thread(new Runnable() {
        @Override
        public void run() {
            readLock.lock();
            console.info("獲取到了讀鎖");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            writeLock.lock();
            console.info("獲取到了寫(xiě)鎖");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容