DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. IoT
  4. Run tasks in parallel and set your own time out

Run tasks in parallel and set your own time out

Andrew Salvadore user avatar by
Andrew Salvadore
·
Sep. 09, 11 · Interview
Like (0)
Save
Tweet
Share
8.43K Views

Join the DZone community and get the full member experience.

Join For Free

 Many times blocking method calls might cause problems in your applications.
If the code is waiting for some resource availablity and you cannot specify a timeout option...well you're stuck!

You might have seen this with javax.jms.Connection, you try to open a connection when the network is down and your application freezes. No exceptions or anything telling you what's wrong.

Using the the java.util.concurrent api you can run the problematic code in a separate thread and wait for the completion or timeout of this execution.

Here is some code calling a jms connection factory in a separate thread.

private Connection getConnection() throws NamingException, JMSException {
 FutureTask<Connection> createConnectionTask = null;
 try {
  createConnectionTask = new FutureTask<Connection>(new Callable<Connection>() {
   @Override
   public Connection call() throws Exception {
    logger.debug("Calling connection on the connection factory");
    Connection connection = (Connection) connectionFactory.createConnection();
    return connection;
   }
  });
  // start task in a new thread
  new Thread(createConnectionTask).start();
 
  logger.debug(String.format("Wait for the execution of createConnection to finish, timeout after %d ms", CONNECTION_TIME_OUT));
  connection = createConnectionTask.get(CONNECTION_TIME_OUT, TimeUnit.MILLISECONDS);
 } catch (TimeoutException e) {
  JMSException jmsException = new JMSException(e.getMessage());
  jmsException.setLinkedException(e);
  throw jmsException;
 }
 return connection;
}



The main thread will create a java.util.concurrent.FutureTask that returns the connection you need.
It'll wait a maximum of CONNECTION_TIME_OUT milliseconds before throwing a TimeoutException. This way you can have complete control of your code execution! 

From http://www.devinprogress.info/2011/03/run-tasks-in-parallel-and-set-your-own.html

Task (computing) application Connection (dance) Execution (computing) Timeout (computing) Factory (object-oriented programming) Blocking (computing) Network

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • How To Build an Effective CI/CD Pipeline
  • 3 Main Pillars in ReactJS
  • Choosing the Right Framework for Your Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: