The Artima Developer Community
Sponsored Link

Java Buzz Forum
Hazelcast 1.2: Distributed ExecutorService and Cancellations

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Talip Ozturk

Posts: 103
Nickname: talip
Registered: May, 2003

Talip Ozturk is founder of Hazelcast, distributed queue, set, map and lock implementation.
Hazelcast 1.2: Distributed ExecutorService and Cancellations Posted: Sep 16, 2008 7:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Talip Ozturk.
Original Post: Hazelcast 1.2: Distributed ExecutorService and Cancellations
Feed Title: Shared Memory
Feed URL: http://www.jroller.com/talipozturk/feed/entries/rss
Feed Description: about java, jcache, jini, javaspaces, distributed data structures and a little bit of me and life.
Latest Java Buzz Posts
Latest Java Buzz Posts by Talip Ozturk
Latest Posts From Shared Memory

Advertisement
Hazelcast 1.2 is out. Distributed executor service is so cool that Hazelcast itself is using it internally. It changed the way I do things. It simplifies distributed computing with its easy and standard interfaces. In the previous blog entry, you have seen the examples of executing code in cluster. What if the code you execute in cluster takes longer than acceptable. If you cannot stop/cancel that task it will keep eating your resources. Standard Java executor framework solves this problem by introducing cancel() api and 'encouraging' us to code and design for cancellations, which is highly ignored part of software development.

public class Fibonacci implements Callable, Serializable {
	int input = 0; 

	public Fibonacci() { 
	} 

	public Fibonacci(int input) { 
		this.input = input;
	} 

	public Long call() {
		return calculate (input);
	}

	private long calculate (int n) {
		if (Thread.currentThread().isInterrupted()) return 0;
		if (n <= 1) return n;
		else return calculate(n-1) + calculate(n-2);
    	}
}

The callable class above calculates the fibonacci number for a given number. In the calculate method, we are checking to see if the current thread is interrupted so that code can be responsive to cancellations once the execution started.

Following fib() method submits the Fibonacci calculation task for number 'n' and waits maximum 3 seconds for result. If the execution doesn't complete in 3 seconds, future.get() will throw TimeoutException and upon catching it we interruptibly cancel the execution for saving some CPU cycles.

long fib(int n) throws Exception {
	ExecutorService es = Hazelcast.getExecutorService();
	Future future = es.submit(new Fibonacci(n));  
	try {
		return future.get(3, TimeUnit.SECONDS);
	} catch (TimeoutException e) {
		future.cancel(true);			
	}
	return -1;
}

fib(20) will probably will take less than 3 seconds but fib(50) will take way longer. (This is not the example for writing better fibonacci calculation code but for showing how to cancel a running execution that takes too long.)

future.cancel(false) can only cancel execution before it is running (executing) but future.cancel(true) can interrupt running executions if your code is able to handle the interruption. So if you are willing to be able to cancel already running task then your task has to be designed to handle interruption.

If calculate (int n) method didn't have if (Thread.currentThread().isInterrupted()) line, then you wouldn't be able to cancel the execution after it started.

So coming up next. Transactions...

Read: Hazelcast 1.2: Distributed ExecutorService and Cancellations

Topic: Revisiting why the Open Web matters; Gratuitous technology politics analogy Previous Topic   Next Topic Topic: Panayotis Katsaloulis has posted JavaPlot 0.4.0, a library for interfacing with gnuplot through...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use