Thread Safety Between Subsequent Calls Executors.newSingleThreadExecutor

I have a question regarding the use of a single threaded artist. Since it reuses the same thread, does this mean that if I change the state of an object in one send call, can I assume that another modification of the state of this object in subsequent submit calls is thread safe? Let me give an example with a toy ...

public class Main {

    public static void main(String[] args) throws Exception {

        final A a = new Main().new A();
        ExecutorService executor = Executors.newSingleThreadExecutor();

        Callable<Integer> callable = new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                return a.modifyState();
            }
        };

        /* first call */
        Future<Integer> result = executor.submit(callable);
        result.get();

        /* second call */
        result = executor.submit(callable);
        int fin = result.get();
        System.out.println(fin);

        executor.shutdown();
    }

    class A {

        private int state;

        public int modifyState() {
            return ++state;
        }

        public int getState() {
            return state;
        }

    }
}

So, I use object A. I send the called and first change its state (see/* first call /). Then I make another call, change state again. (/ second call * /). Now my big question

, , , , A.state 1? , 0 ?

, , , ?,

? ?

+3
2

Executors.newSingleThreadExecutor() , . , , .

+1

, . javadoc ExecutorService :

: Runnable Callable ExecutorService - , , , , , Future.get().

+3

All Articles