CodeLockTest

8302 단어 code
package Thread;



import junit.framework.Assert;

import org.junit.Test;



import java.util.concurrent.Executor;

import java.util.concurrent.Executors;

import java.util.concurrent.locks.ReentrantLock;



/**

 * Created by csophys on 15/7/14.

 */

public class CodeLockTest {

    static int i = 1;



    public static void getTargtValueWithoutSynchronized() throws InterruptedException {

        //output current value

        //System.out.println(i);

        Assert.assertTrue(i == 1);

        i++;

        //          

        Thread.sleep(1);

        i--;

    }



    public static void getTargtValue() throws InterruptedException {

        //output current value

        System.out.println(i);

        Assert.assertTrue(i == 1);

        i++;

        //          

        Thread.sleep(1);

        i--;

    }



    //     

    /*{

        synchronized (this) {



        }

        synchronized (SynchronizedCodeTest.class){



        }

    }*/



    /**

     *        ,       ,          

     */

    static ReentrantLock reentrantLock = new ReentrantLock();



    public static void lockWithoutUnlock() {

        reentrantLock.lock();

        System.out.println(i);

    }



    public static void lockWithUnlock(){

        reentrantLock.lock();

        try {

            System.out.println(i);

        } catch (Exception e) {

            e.printStackTrace();

        }finally {

            reentrantLock.unlock();

        }



    }



    @Test

    public void testMultipleThreadInvokeWithoutSynchornized() {

        Executor executor = Executors.newFixedThreadPool(100);

        for (int i = 0; i < 100; i++) {

            executor.execute(new Runnable() {

                public void run() {

                    try {

                        getTargtValueWithoutSynchronized();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            });

        }

    }





    @Test

    public void testMultipleThreadInvoke() {

        Executor executor = Executors.newFixedThreadPool(100);

        for (int i = 0; i < 100; i++) {

            executor.execute(new Runnable() {

                public void run() {

                    try {

                        getTargtValue();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }

            });

        }

    }



    @Test

    public void testTryLockWithoutUnlock() {

        Executor executor = Executors.newFixedThreadPool(100);

        for (int i = 0; i < 100; i++) {

            executor.execute(new Runnable() {

                public void run() {

                    lockWithoutUnlock();

                }

            });

        }

    }



    @Test

    public void testTryLockWithUnlock() {

        Executor executor = Executors.newFixedThreadPool(100);

        for (int i = 0; i < 100; i++) {

            executor.execute(new Runnable() {

                public void run() {

                    lockWithUnlock();

                }

            });

        }

    }



    /*volatile         ,          ,            。          。*/

}

좋은 웹페이지 즐겨찾기