0

I encountered a problem:Will inner class method cannot synchronized the object in outer class? A answer from stackoverflow told me : No. But I really want to know the reason.

For example: I designed a thread pool with at most 5 threads to do work.

public class ThreadPool{

     // Ignore the Task class 
     private LinkedList<Task> tasks;

     Executor[] executors = new Executor[5];

     private static ThreadPool pool = null;

     private ThreadPool() {
        tasks = new LinkedList<Task>();
        for (int i = 0; i < 5; i++) {
            executors[i] = new Executor(i);

        }
    }

    public static ThreadPool getInstance() {
        if (pool == null) {
            pool = new ThreadPool();
        }
        return pool;
    }

    public void addTask(Task task) {
        /** igore code here*/
    }

    private class Executor extends Thread {

        private int i;
        public Executor(int i) {
            this.i = i;
            start();
        }

                public void run() {
                Task task = null;
                synchronized (tasks) {

                    if (tasks.size() == 0) {
                        System.out.println("tasks's size is : " + tasks.size());
                        try {

                            while (tasks.size() == 0) {
                                tasks.wait();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (tasks.size() > 0)
                        task = tasks.removeFirst();
                }
                if (task != null) {
                    /** ignore code here */
                }
        }

    }
}

Unfortunately, the synchronized doesn't work. But when I put the synchronized block into

synchronized (ThreadPool.this) {
 ...
} 

it works.

So, i want to know why inner class method cannot synchronized the object in outer class.

Hope for a detailed answer.

chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
  • You have only `synchronized (tasks)`. What code doesn't work? – talex Sep 05 '14 at 06:57
  • when I called getInstance() ,and then its constructor will be called. And the 5 five threads will all call the run method! if the synchronized works,only one thread can obtain the monitor and execute the code. – chenzhongpu Sep 05 '14 at 07:02
  • "So, i want to know why inner class method cannot synchronize the object in outer class.". It can. You did it, when you added `synchronized (ThreadPool.this)`. – user207421 Sep 05 '14 at 07:03
  • if only synchronized (tasks), System.out.println("tasks's size is : " + tasks.size()); this line will be executed 5 times (one thread one once) – chenzhongpu Sep 05 '14 at 07:04
  • 1
    So? Are you perhaps really asking why you don't get some kind of free extra synchronization other than the one you're actually writing? Because you don't. You get what you write, nothing more. – user207421 Sep 05 '14 at 07:05
  • i want to know what exactly synchronized (ThreadPool.this) do ? why cannot synchronize without it ? – chenzhongpu Sep 05 '14 at 07:07
  • @ChenZhongPu you can sinchronize without it. But first explain what is wrong with this way. – talex Sep 05 '14 at 07:12
  • without it, the five threads will execute the run() method. However, the Java Doc says, once a thread get the monitor of an object, other thread cannot execute the synchronized code (and will wait until being released).@talex – chenzhongpu Sep 05 '14 at 07:16
  • You can see it http://stackoverflow.com/questions/18910773/locking-and-synchronization-between-outer-and-inner-class-methods – chenzhongpu Sep 05 '14 at 07:19

0 Answers0