重學JAVA基礎(六):多線程的同步

來源:互聯網
上載者:User

標籤:

1.synchronized關鍵字

/** * 同步關鍵字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */public class SyncThreadTest {    private static final byte[] lock = new byte[1];        /**     * 同步方法     * @author tomsnail     * @date 2015年4月18日 下午12:15:30     */    public synchronized void test1(){            }        /**     * 同步塊     * @author tomsnail     * @date 2015年4月18日 下午12:15:17     */    public void test2(){        synchronized (lock) {                    }    }    }

2.volatile關鍵字

/** * volatile關鍵字 * @author tomsnail * @date 2015年4月18日 下午12:21:58 */public class VolatileThreadTest {    private volatile int count = 100;        public void add(int number){        count+=number;    }        public int getCount(){        return count;    }}

 

3.Lock鎖

/** * lock鎖 * @author tomsnail * @date 2015年4月18日 下午12:58:49 */public class LockThreadTest {    private Lock lock = new ReentrantLock();        private int count = 100;        public void test(){        lock.lock();        count++;        System.out.println(count);        lock.unlock();    }    }

4.Mutex訊號量

/** * 線程訊號量 * @author tomsnail * @date 2015年4月18日 下午1:14:47 */public class MutexThreadTest {    private CountDownLatch countDownLatch = new CountDownLatch(1);        private Semaphore s = new Semaphore(5);        public void a(){        try {            countDownLatch.await();        } catch (InterruptedException e) {            e.printStackTrace();        }    }        public void b(){        countDownLatch.countDown();    }        public void c(){        try {            System.out.println(" try acquire s");            s.acquire();            System.out.println(" acquire s");        } catch (InterruptedException e) {            e.printStackTrace();        }    }        public void d(){        s.release();        System.out.println(" release s");    }            public static void main(String[] args) {        MutexThreadTest mutexThreadTest = new MutexThreadTest();        for(int i=0;i<10;i++){            new Thread(new ThreadTest(mutexThreadTest)).start();        }        mutexThreadTest.a();        System.out.println("a...");        for(int i=0;i<10;i++){            new Thread(new ThreadTest2(mutexThreadTest)).start();        }            }    }class ThreadTest implements Runnable{    private MutexThreadTest mutexThreadTest;        public ThreadTest(MutexThreadTest mutexThreadTest){        this.mutexThreadTest = mutexThreadTest;    }        @Override    public void run() {        try {            Thread.currentThread().sleep(500);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("mutexThreadTest countDown");        mutexThreadTest.b();    }    }class ThreadTest2 implements Runnable{    private MutexThreadTest mutexThreadTest;        public ThreadTest2(MutexThreadTest mutexThreadTest){        this.mutexThreadTest = mutexThreadTest;    }        @Override    public void run() {        mutexThreadTest.c();        try {            Thread.currentThread().sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        mutexThreadTest.d();    }    }

 

重學JAVA基礎(六):多線程的同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.