Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3

08 Apr, 2019
Prerequisite: Different Approaches to Concurrent Programming in Java
In this approach, the user physically puts the class definition of the class that implements Runnable inside the class definition of the main class.public class OuterClass{
    private class InnerClass implements Runnable{
        public void run(){
        }
    }
}
The run() method is then put inside the inner class and passed to execute method. Execute does not really mean execute. It means available for execution. For instance, if the user has a pool size of 5 and there are 10 tasks in the task queue, the 6th one does not start executing until one of the first five is finished.taskList.execute(new InnerClass());

In this approach, the user physically puts the class definition of the class that implements Runnable inside the class definition of the main class.public class OuterClass{
    private class InnerClass implements Runnable{
        public void run(){
        }
    }
}

public class OuterClass{
    private class InnerClass implements Runnable{
        public void run(){
        }
    }
}

The run() method is then put inside the inner class and passed to execute method. Execute does not really mean execute. It means available for execution. For instance, if the user has a pool size of 5 and there are 10 tasks in the task queue, the 6th one does not start executing until one of the first five is finished.taskList.execute(new InnerClass());

taskList.execute(new InnerClass());

Practical Implementation:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;  // Java Program to depict Concurrent// Programming in actionpublic class OuterClass {      // Driver method    public static void main(String[] args)    {        new OuterClass().startThreads();    }      // Starts the threads and calls run method    // method of the runnable interface    private void startThreads()    {        ExecutorService taskList            = Executors.newFixedThreadPool(2);          taskList.execute(new InnerClass(1));        taskList.execute(new InnerClass(2));        taskList.execute(new InnerClass(3));        taskList.execute(new InnerClass(4));        taskList.execute(new InnerClass(5));        taskList.shutdown();    }      // Pauses execution allowing time for    // system to switch back and forth    private void pause(double seconds)    {        try {            Thread.sleep(Math.round(1000.0 * seconds));        }        catch (InterruptedException e) {            e.printStackTrace();        }    }      // Inner Class    public class InnerClass implements Runnable {          private int loopLimit;          // Constructor to define        // different limits        InnerClass(int loopLimit)        {            this.loopLimit = loopLimit;        }          // Prints thread name and value        // of the counter variable        @Override        public void run()        {            for (int i = 0; i < loopLimit; i++) {                System.out.println(                    Thread.currentThread().getName()                    + " Counter: " + i);                pause(Math.random());            }        }    }}
Output:

pool-1-thread-1 Counter: 0
pool-1-thread-2 Counter: 0
pool-1-thread-1 Counter: 0
pool-1-thread-2 Counter: 1
pool-1-thread-1 Counter: 1
pool-1-thread-2 Counter: 0
pool-1-thread-2 Counter: 1
pool-1-thread-1 Counter: 2
pool-1-thread-1 Counter: 0
pool-1-thread-2 Counter: 2
pool-1-thread-1 Counter: 1
pool-1-thread-2 Counter: 3
pool-1-thread-1 Counter: 2
pool-1-thread-1 Counter: 3
pool-1-thread-1 Counter: 4


Advantages:
It is easy to access the main application because methods in inner classes can access any public or private methods or instance variables of the outer class.As with separate classes, the user can pass arguments to the constructor that stores them in instance variables that run uses.
It is easy to access the main application because methods in inner classes can access any public or private methods or instance variables of the outer class.
As with separate classes, the user can pass arguments to the constructor that stores them in instance variables that run uses.
Disadvantages:
There is a disadvantage of tight coupling as the run method is closely tied to this application. The run method cannot be reused elsewhere.There is a severe danger of race conditions. Inner classes are specifically used when the user wants to access the shared data (data in the main application).
There is a disadvantage of tight coupling as the run method is closely tied to this application. The run method cannot be reused elsewhere.
There is a severe danger of race conditions. Inner classes are specifically used when the user wants to access the shared data (data in the main application).
Anonymous Inner Classes: Inner classes are used so frequently in applications that developers often want to shorten the syntax by using Anonymous Inner Classes where the user gives the class definition and instantiate the class all in one fell swoop. Since the anonymous inner classes are inner classes, they have the same pros as inner classes but they are shorter and more succinct. The disadvantages that come with them are that they are a little more confusing to beginners, there are no constructors and that they cannot be reused elsewhere.
Practical Implementation:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;  // Concurrent programming using// Anonymous Inner Classpublic class MyClass {      // Driver method    public static void main(String[] args)    {        new MyClass().startThreads();    }      // Starting threads with pool size as 2    private void startThreads()    {        ExecutorService taskList            = Executors.newFixedThreadPool(2);          for (int i = 0; i < 5; i++) {            int finalI = i + 1;              // Giving the class definition            // and instantiating it all at once            taskList.execute(new Runnable() {                  // Prints thread name and value                // of the counter variable                @Override                public void run()                {                    for (int j = 0; j < finalI; j++) {                        System.out.println(                            Thread                                .currentThread()                                .getName()                            + " Counter:" + j);                        pause(Math.random());                    }                }            });        }        taskList.shutdown();    }      // Pauses execution allowing time for    // system to switch back and forth    private void pause(double seconds)    {        try {            Thread.sleep(                Math.round(1000.0 * seconds));        }        catch (InterruptedException e) {            e.printStackTrace();        }    }}
Output:
pool-1-thread-1 Counter:0
pool-1-thread-2 Counter:0
pool-1-thread-2 Counter:1
pool-1-thread-1 Counter:0
pool-1-thread-2 Counter:0
pool-1-thread-1 Counter:1
pool-1-thread-2 Counter:1
pool-1-thread-1 Counter:2
pool-1-thread-2 Counter:2
pool-1-thread-2 Counter:3
pool-1-thread-1 Counter:0
pool-1-thread-1 Counter:1
pool-1-thread-1 Counter:2
pool-1-thread-1 Counter:3
pool-1-thread-1 Counter:4

Java-Multithreading
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Queue Interface In Java
Object Oriented Programming (OOPs) Concept in Java
Stack Class in Java
ArrayList in Java
HashMap in Java with Examples
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Implementing a Linked List in Java using Class
Convert Double to Integer in Java