Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Mutithreading Framework?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mutithreading Framework?

    Is there a frame Work or library present for Multithreading scenario that can help the developer to ease the thread development by allowing the developer to just submit a task and execute it by Thread-per-request architecture? ie the developer does not need to extend Thread class or implement Runnable interface but he should submit the task to the framework and frame work handle the thread creation and destruction itself so that the developer can just concentrate on task coding


  2. #2
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multithreading frame works?

    Is there a frame Work or library present for Multithreading scenario that can help the developer to ease the thread development by allowing the developer to just submit a task and execute it by Thread-per-request architecture? ie the developer does not need to extend Thread class or implement Runnable interface but he should submit the task to the framework and frame work handle the thread creation and destruction itself so that the developer can just concentrate on task coding

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Multithreading frame works?

    Actually creating a thread and overwriting the run method is pretty simple, I dont think it can get any simpler then that no matter what kind of framework you use.

    If you want, for example, a Thread that is periodically writing the time to the standard output you could write code like this:
    		Thread t = new Thread() {
    			public void run() {
    				while (true) {
    					System.out.println(new Date());
    					try {
    						sleep(100);
    					} catch (InterruptedException e) {}
    				}
    			}
    		};
    		t.start();
    There is no black magic involved, its all pretty straight forward.
    You dont even have to care about any destruction or clean up, the garbage collector will do that for you once the thread has stopped execution. (in this case that wont happen since it runs forever)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 688 Times in 675 Posts

    Default Re: Mutithreading Framework?

    Thread moved.

    Honest question: In your mind, is there a difference between a developer and a programmer? Does the programmer get his/her hands dirty adjusting the nuts and bolts in the depths of the language at its interface with the hardware while the developer uses tools (frameworks?) built by programmers, enabling them to stay at arm's length from the details, unconcerned about what's happening under the hood?

    The multi-threading concept you describe would not be that hard to program and would be an interesting, confidence-building exercise for someone who may not be too sure of their threading skills. You can find some very good multi-threading tutorials and discussions by searching "java concurrency".

    --- Update ---

    Please do not post multiple threads on the same topic.

    Threads merged.

  5. #5

    Default Re: Mutithreading Framework?

    Executor framework is used when your application has some requirement where you have to execute the tasks by multiple threads concurrently, So If you use executor framework, then you don't need to manage the threads, you can just define the no. of threads to be in thread pool,and that's it.

    Fork and Join framework, It is basically used when you have some sort of task(or algorithm) that you can sub-divide the tasks and then later join them to get the final result. It basically works on Divide and conquer principle.

  6. #6
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Mutithreading Framework?

    The Java platform always has a strong support for concurrent programming and multithreading. But in earlier days the support was in the form of calling native constructs itself in the application layer. The biggest disadvantage of this approach was to handle those primitive construct calls efficiently. Otherwise the application will not run properly and unexpected results will be generated.

    The Java Executor Framework has been introduced in Java 1.5 and it is a part of java concurrency package. The Executor framework is an abstraction layer over the actual implementation of java multithreading. It is the first concurrent utility framework in java and used for standardizing invocation, scheduling, execution and control of asynchronous tasks in parallel threads. The execution rules are defined during the creation of the constructor. And then the executor runs the concurrent threads following the rules set earlier.

    Executor implementation in java uses thread pools which consists of worker threads. The entire management of worker threads is handled by the framework. So the overhead in memory management is much reduced compared to earlier multithreading approaches.

    Java Executor Service
    Figure 1: Java Executor Service

    Before java 1.5, multithreading applications were created using thread group, thread pool or custom thread pool. As a result the entire thread management was the responsibility of the programmer keeping in mind the following points.

    Thread synchronization
    Thread waiting
    Thread joining
    Thread locking
    Thread notification
    Handling dead lock
    And many more concurrent execution issues that arises out of the application requirement. In real project implementation some time it is very difficult to control multithreading applications. The behaviors of the threads are also dependent on the environment where the application is deployed and running. So the same application might behave in a different way on different deployment environment. For example, the processor speed, the RAM size, the band width all has a direct impact on the multithreading application. So you have to keep all these factors in mind before creating architecture for multithreading applications.

    The Java Executor framework provides multi-threading applications an easy abstraction layer. The executor abstraction layer hides the critical parts of concurrent execution and the programmer only concentrates on the business logic implementation. In java executor framework all parallel works are considered as tasks instead of simple threads. So the application now deals simply with instances of Runnable (which is basically collections of tasks or parallel works) and then it is passed to an Executor to process. The ExecutorService interface extends the simplistic Executor interface. The Java Executor framework has life cycle methods to manage the entire concurrent execution flow.

    The Java Executor framework creates tasks by using instances of Runnable or Callable. In case of Runnable, the run () method does not return a value or throw any checked exception. But Callable is a more functional version in that area. It defines a call () method that allows the return of some computed value which can be used in future processing and it also throws an exception if necessary.

    The FutureTask class is another important component which is used to get future information about the processing. An instance of this class can wrap either a Callable or a Runnable. You can get an instance of this as the return value of submit () method of an ExecutorService. You can also manually wrap your task in a FutureTask before calling execute () method.

    Following are the functional steps to implement the Java ThreadPoolExecutor.

    A pool of multiple threads is created.
    A queue is created holding all the tasks but these tasks are not yet assigned to threads from the pool.
    Rejection handler is used to handle the situation when one or more tasks are not able to assign in the queue. As per the default rejection policy, it will simply throw a RejectedExecutionException runtime exception, and the application can catch it or discard it.
    Thread Pool Executor
    Figure 2: Thread Pool Executor

    The following example will show the life cycle of an executor framework. The example will cover only the basis steps and the basic interfaces. There are many more properties and subclasses available to handle different kind of applicant needs.

    The following class implements Runnable and its instances will be used as a task in the next section of code.

    Listing 1: Class implementing Runnable

    public class TaskPrint implements Runnable {
    private final String name;
    private final int delay;

    public TaskPrint(String name, int delay) {
    this.name = name;
    this.delay = delay;
    }
    public void run() {
    System.out.println("Starting: " + name);
    try {
    Thread.sleep(delay);
    } catch (InterruptedException ignored) {
    }
    System.out.println("Done with: " + name);
    }
    }
    Listing 2: Class implementing Executor tasks

    import java.util.concurrent.*;
    import java.util.Random;
    public class ExecutorServiceExample {
    public static void main(String args[]) {
    Random random = new Random();

    // Create an executor of thread pool size 3
    ExecutorService executor = Executors.newFixedThreadPool(3);

    // Sum up wait times to know when to shutdown
    int waitTime = 600;
    for (int i=0; i<10; i++) {
    String name = "NamePrinter " + i;
    int time = random.nextInt(500);
    waitTime += time;
    Runnable runner = new TaskPrint(name, time);
    System.out.println("Adding: " + name + " / " + time);
    executor.execute(runner);
    }
    try {
    Thread.sleep(waitTime);
    executor.shutdown();
    executor.awaitTermination
    (waitTime, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignored) {
    }
    System.exit(0);
    }
    }

    Following will describe the steps to implement java executor framework in your application.

    Create an executor
    First you need to create an instance of an Executor or ExecutorService. The Executor class has a number of static factory methods to create an ExecutorService depending upon the requirement of the application. Following are two main methods to create executor service.

    The newFixedThreadPool () returns a ThreadPoolExecutor instance with an initialized and unbounded queue and a fixed number of threads.
    The newCachedThreadPool () returns a ThreadPoolExecutor instance initialized with an unbounded queue and unbounded number of threads.
    In the 1st case no extra thread is created during execution. So if there is no free thread available the task has to wait and then execute when one thread is free. In the 2nd case, existing threads are reused if available. But if no free thread is available, a new one is created and added to the pool to complete the new task. Threads that have been idle for longer than a timeout period will be removed automatically from the pool.

    This is a fixed pool of 10 threads.

    private static final Executor executor = Executors.newFixedThreadPool(10);
    This is a cached thread pool

    private static ExecutorService exec = Executors.newCachedThreadPool();
    Following is an example of customized thread pool executor. The parameter values depend upon the application need. Here the core pool is having 8 threads which can run concurrently and the maximum number is 12. The queue is capable of keeping 250 tasks. Here one point should be remembered that the pool size should be kept on a higher side to accommodate all tasks. The idle time limit is kept as 5 ms.

    private static final Executor executor = new ThreadPoolExecutor(5, 12, 50000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(250));

  7. #7
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Mutithreading Framework?

    The Java platform always has a strong support for concurrent programming and multithreading. But in earlier days the support was in the form of calling native constructs itself in the application layer. The biggest disadvantage of this approach was to handle those primitive construct calls efficiently. Otherwise the application will not run properly and unexpected results will be generated.

    The Java Executor Framework has been introduced in Java 1.5 and it is a part of java concurrency package. The Executor framework is an abstraction layer over the actual implementation of java multithreading. It is the first concurrent utility framework in java and used for standardizing invocation, scheduling, execution and control of asynchronous tasks in parallel threads. The execution rules are defined during the creation of the constructor. And then the executor runs the concurrent threads following the rules set earlier.

    Executor implementation in java uses thread pools which consists of worker threads. The entire management of worker threads is handled by the framework. So the overhead in memory management is much reduced compared to earlier multithreading approaches.

    Java Executor Service
    Figure 1: Java Executor Service

    Before java 1.5, multithreading applications were created using thread group, thread pool or custom thread pool. As a result the entire thread management was the responsibility of the programmer keeping in mind the following points.

    Thread synchronization
    Thread waiting
    Thread joining
    Thread locking
    Thread notification
    Handling dead lock
    And many more concurrent execution issues that arises out of the application requirement. In real project implementation some time it is very difficult to control multithreading applications. The behaviors of the threads are also dependent on the environment where the application is deployed and running. So the same application might behave in a different way on different deployment environment. For example, the processor speed, the RAM size, the band width all has a direct impact on the multithreading application. So you have to keep all these factors in mind before creating architecture for multithreading applications.

    The Java Executor framework provides multi-threading applications an easy abstraction layer. The executor abstraction layer hides the critical parts of concurrent execution and the programmer only concentrates on the business logic implementation. In java executor framework all parallel works are considered as tasks instead of simple threads. So the application now deals simply with instances of Runnable (which is basically collections of tasks or parallel works) and then it is passed to an Executor to process. The ExecutorService interface extends the simplistic Executor interface. The Java Executor framework has life cycle methods to manage the entire concurrent execution flow.

    The Java Executor framework creates tasks by using instances of Runnable or Callable. In case of Runnable, the run () method does not return a value or throw any checked exception. But Callable is a more functional version in that area. It defines a call () method that allows the return of some computed value which can be used in future processing and it also throws an exception if necessary.

    The FutureTask class is another important component which is used to get future information about the processing. An instance of this class can wrap either a Callable or a Runnable. You can get an instance of this as the return value of submit () method of an ExecutorService. You can also manually wrap your task in a FutureTask before calling execute () method.

    Following are the functional steps to implement the Java ThreadPoolExecutor.

    A pool of multiple threads is created.
    A queue is created holding all the tasks but these tasks are not yet assigned to threads from the pool.
    Rejection handler is used to handle the situation when one or more tasks are not able to assign in the queue. As per the default rejection policy, it will simply throw a RejectedExecutionException runtime exception, and the application can catch it or discard it.
    Thread Pool Executor
    Figure 2: Thread Pool Executor

    The following example will show the life cycle of an executor framework. The example will cover only the basis steps and the basic interfaces. There are many more properties and subclasses available to handle different kind of applicant needs.

    The following class implements Runnable and its instances will be used as a task in the next section of code.

    Listing 1: Class implementing Runnable

    public class TaskPrint implements Runnable {
    private final String name;
    private final int delay;

    public TaskPrint(String name, int delay) {
    this.name = name;
    this.delay = delay;
    }
    public void run() {
    System.out.println("Starting: " + name);
    try {
    Thread.sleep(delay);
    } catch (InterruptedException ignored) {
    }
    System.out.println("Done with: " + name);
    }
    }
    Listing 2: Class implementing Executor tasks

    import java.util.concurrent.*;
    import java.util.Random;
    public class ExecutorServiceExample {
    public static void main(String args[]) {
    Random random = new Random();

    // Create an executor of thread pool size 3
    ExecutorService executor = Executors.newFixedThreadPool(3);

    // Sum up wait times to know when to shutdown
    int waitTime = 600;
    for (int i=0; i<10; i++) {
    String name = "NamePrinter " + i;
    int time = random.nextInt(500);
    waitTime += time;
    Runnable runner = new TaskPrint(name, time);
    System.out.println("Adding: " + name + " / " + time);
    executor.execute(runner);
    }
    try {
    Thread.sleep(waitTime);
    executor.shutdown();
    executor.awaitTermination
    (waitTime, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignored) {
    }
    System.exit(0);
    }
    }

    --- Update ---

    The Java platform always has a strong support for concurrent programming and multithreading. But in earlier days the support was in the form of calling native constructs itself in the application layer. The biggest disadvantage of this approach was to handle those primitive construct calls efficiently. Otherwise the application will not run properly and unexpected results will be generated.

    The Java Executor Framework has been introduced in Java 1.5 and it is a part of java concurrency package. The Executor framework is an abstraction layer over the actual implementation of java multithreading. It is the first concurrent utility framework in java and used for standardizing invocation, scheduling, execution and control of asynchronous tasks in parallel threads. The execution rules are defined during the creation of the constructor. And then the executor runs the concurrent threads following the rules set earlier.

    Executor implementation in java uses thread pools which consists of worker threads. The entire management of worker threads is handled by the framework. So the overhead in memory management is much reduced compared to earlier multithreading approaches.

    Java Executor Service
    Figure 1: Java Executor Service

    Before java 1.5, multithreading applications were created using thread group, thread pool or custom thread pool. As a result the entire thread management was the responsibility of the programmer keeping in mind the following points.

    Thread synchronization
    Thread waiting
    Thread joining
    Thread locking
    Thread notification
    Handling dead lock
    And many more concurrent execution issues that arises out of the application requirement. In real project implementation some time it is very difficult to control multithreading applications. The behaviors of the threads are also dependent on the environment where the application is deployed and running. So the same application might behave in a different way on different deployment environment. For example, the processor speed, the RAM size, the band width all has a direct impact on the multithreading application. So you have to keep all these factors in mind before creating architecture for multithreading applications.

    The Java Executor framework provides multi-threading applications an easy abstraction layer. The executor abstraction layer hides the critical parts of concurrent execution and the programmer only concentrates on the business logic implementation. In java executor framework all parallel works are considered as tasks instead of simple threads. So the application now deals simply with instances of Runnable (which is basically collections of tasks or parallel works) and then it is passed to an Executor to process. The ExecutorService interface extends the simplistic Executor interface. The Java Executor framework has life cycle methods to manage the entire concurrent execution flow.

    The Java Executor framework creates tasks by using instances of Runnable or Callable. In case of Runnable, the run () method does not return a value or throw any checked exception. But Callable is a more functional version in that area. It defines a call () method that allows the return of some computed value which can be used in future processing and it also throws an exception if necessary.

    The FutureTask class is another important component which is used to get future information about the processing. An instance of this class can wrap either a Callable or a Runnable. You can get an instance of this as the return value of submit () method of an ExecutorService. You can also manually wrap your task in a FutureTask before calling execute () method.

    Following are the functional steps to implement the Java ThreadPoolExecutor.

    A pool of multiple threads is created.
    A queue is created holding all the tasks but these tasks are not yet assigned to threads from the pool.
    Rejection handler is used to handle the situation when one or more tasks are not able to assign in the queue. As per the default rejection policy, it will simply throw a RejectedExecutionException runtime exception, and the application can catch it or discard it.
    Thread Pool Executor
    Figure 2: Thread Pool Executor

    The following example will show the life cycle of an executor framework. The example will cover only the basis steps and the basic interfaces. There are many more properties and subclasses available to handle different kind of applicant needs.

    The following class implements Runnable and its instances will be used as a task in the next section of code.

    Listing 1: Class implementing Runnable

    public class TaskPrint implements Runnable {
    private final String name;
    private final int delay;

    public TaskPrint(String name, int delay) {
    this.name = name;
    this.delay = delay;
    }
    public void run() {
    System.out.println("Starting: " + name);
    try {
    Thread.sleep(delay);
    } catch (InterruptedException ignored) {
    }
    System.out.println("Done with: " + name);
    }
    }
    Listing 2: Class implementing Executor tasks

    import java.util.concurrent.*;
    import java.util.Random;
    public class ExecutorServiceExample {
    public static void main(String args[]) {
    Random random = new Random();

    // Create an executor of thread pool size 3
    ExecutorService executor = Executors.newFixedThreadPool(3);

    // Sum up wait times to know when to shutdown
    int waitTime = 600;
    for (int i=0; i<10; i++) {
    String name = "NamePrinter " + i;
    int time = random.nextInt(500);
    waitTime += time;
    Runnable runner = new TaskPrint(name, time);
    System.out.println("Adding: " + name + " / " + time);
    executor.execute(runner);
    }
    try {
    Thread.sleep(waitTime);
    executor.shutdown();
    executor.awaitTermination
    (waitTime, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignored) {
    }
    System.exit(0);
    }
    }

Similar Threads

  1. Java MutiThreading Stock
    By tanchiwoo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2014, 05:37 AM
  2. Spring Framework
    By Napsy in forum Web Frameworks
    Replies: 3
    Last Post: January 23rd, 2013, 07:04 AM
  3. Mutithreading problem
    By ashish12169 in forum Threads
    Replies: 1
    Last Post: January 8th, 2013, 07:30 AM
  4. Self Adaptive framework
    By dpunitha in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 5th, 2013, 12:49 AM
  5. Replies: 1
    Last Post: October 20th, 2009, 07:31 AM

Tags for this Thread