public static void main(String[] args) {
final ThreadGate trx1Gate = new ThreadGate();
final ThreadGate trx2Gate = new ThreadGate();
+ final FutureTask<Void>[] tasks = new FutureTask[2];
logger.info("Starting application");
- final FutureTask<Void> taskFirst = new FutureTask<Void>
+ tasks[0] = new FutureTask<Void>
(
new Runnable(){
},
null
);
- final FutureTask<Void> taskSecond = new FutureTask<Void>
+ tasks[1] = new FutureTask<Void>
(
new Runnable(){
null
);
- new Thread(taskFirst).start();
- new Thread(taskSecond).start();
+ for (final FutureTask<Void> task : tasks) {
+ new Thread(task).start();
+ }
// Wait for end.
- try {
- taskFirst.get();
- taskSecond.get();
- } catch (final InterruptedException e) {
- logger.error("Error", e);
- } catch (final ExecutionException e) {
- logger.error("Error", e);
+ for (final FutureTask<Void> task : tasks) {
+ try {
+ task.get();
+ } catch (final InterruptedException e) {
+ logger.error("Error", e);
+ } catch (final ExecutionException e) {
+ logger.error("Error", e);
+ } finally {
+ task.cancel(true);
+ }
}
-
+ SpringContextLocator.getInstance().close();
logger.info("End application");
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Spring Context
private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml";
// Spring ApplicationContext
- private final ApplicationContext context;
+ private final ClassPathXmlApplicationContext context;
private SpringContextLocator() {
public Object getBean(final String name) {
return context.getBean(name);
}
+
+ public void close() {
+ context.close();
+ }
}
}
}
- public void doFirstStepWithGate() {
+ private void doFirstStepWithGate() {
logger.info("Start doFirstStepWithGate");
logger.info("doFirstStepWithGate UPDATING");
logger.info("End doFirstStepWithGate");
}
- public void doFirstStepWithoutGate() {
+ private void doFirstStepWithoutGate() {
logger.info("Start doFirstStepWithoutGate");
logger.info("doFirstStepWithoutGate UPDATING");
final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("UPDATE children SET name='Bob', parent_id='1' WHERE id='2'");
- // trx2 continues (fourth step)
-
logger.info("End doThirdStep");
}