Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3 Last Updated : 08 Apr, 2019 Comments Improve Suggest changes Like Article Like Report Prerequisite: Different Approaches to Concurrent Programming in Java Inner Class that implements Runnable 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()); Practical Implementation: Java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // Java Program to depict Concurrent // Programming in action public 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. 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). Anonymous Inner Class that implements Runnable 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: Java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // Concurrent programming using // Anonymous Inner Class public 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 Comment More infoAdvertise with us Next Article Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3 A AnureetKaur Follow Improve Article Tags : Java Java Programs Java-Multithreading Practice Tags : Java Similar Reads Main App Implements Runnable | Concurrent Programming Approach 2 Prerequisite: Different Approaches to Concurrent Programming in Java Let's look at the second approach in detail. The user has the main class that implements runnable which is a promise to the compiler that the class will have a run method. public class MyClass implements Runnable{ public void run() 3 min read How to Handle Concurrent Access and Modification of a PriorityQueue in Java? In Java, PriorityQueue is a class that implements a priority queue based on the priority heap. While PriorityQueue provides efficient operations for the adding and removing elements and it is not inherently thread-safe. Concurrent access and modification of the PriorityQueue can lead to unexpected b 3 min read Java Program to Implement ConcurrentLinkedQueue API The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts e 4 min read Java Multithreading - One Thread to Take Input, Another to Print on Console Multithreading is a concept in which our program can do two or more tasks at the same time. Thread is the execution unit of any process. Every process has at least one thread which is called main thread. In this article, we will create a Java program that will do printing on the console until the us 4 min read Difference Between Running and Runnable States of a Thread in Java Thread is the backbone of multithreading in java. Multithreading is a feature that allows concurrent execution of two or more parts of the program for the maximum utilization of CPU. Each part of such a program is called a thread. So threads are light-weighted processes within a process. A thread ca 5 min read Like