再学JAVA基礎(六):マルチスレッドの同期

9708 ワード

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 

 * @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();

    }

    

}