Javaマルチスレッドの簡単なスレッド同期インスタンス


データクラス:
package Thread.MyCommon;

public class Data {

    public int num = 0;

    public synchronized int getEven() {
        ++num;
        Thread.yield();// , 
        ++num;
        return num;
    }

}

 
スレッドクラス:
package Thread.MyCommon;

public class myThread implements Runnable {

    private Data data;

    public myThread(Data d) {
        data = d;
    }

    @Override
    public void run() {
        while (true)
        {
            int val = data.getEven();
            if (val % 2 != 0) {
                System.out.println(val + " not even!");
            }
        }
    }

}

 
主スレッドクラス:
package Thread.MyCommon;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    
    
    public static void main(String[] args) {
        Data data=new Data();// 
        ExecutorService executorService=Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            executorService.execute(new myThread(data));
        }
        executorService.shutdown();// , 。
    }
}

 
------------------------------------------------------------------------------------------
同期コードをlock方式に変更できます.
package Thread.MyCommon;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Data {

    public int num = 0;

    Lock lock = new ReentrantLock();

    public int getEven() {
        lock.lock();
        try {
            ++num;
            Thread.yield();//  , 
            ++num;
            return num;//  unlock return, 
        } finally {
            lock.unlock();
        }
        // return num;// unlock return, 
    }
}

 
またはsynchronized(this)に変更します.
package Thread.MyCommon;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Data {

    public int num = 0;

    Lock lock = new ReentrantLock();

    public int getEven() {
        synchronized (this) {
        ++num;
        Thread.yield();//  , 
        ++num;
        return num;//  unlock return, 
        }
    }
}

 
 
 
--------------------------------------------------------------------------
以下のように、同期しているが、読み出し方法ではデータが同期の偶数であることは保証されない
package Thread.MyCommon;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AtomicityTest implements Runnable {

    private int i = 0;

    public int getValue() {
        return i;
    }

    private synchronized void evenIncrement() {
        i++;
        Thread.yield();
        i++;
    }

    @Override
    public void run() {
        while (true)
            evenIncrement();
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        AtomicityTest at = new AtomicityTest();
        exec.execute(at);
        while (true) {
            int val = at.getValue();
            if (val % 2 != 0) {
                System.out.println(val);
                System.exit(0);
            }
        }
    }
}