ConsoleInterrupt.java
1package com.gams.examples.interrupt;
2 
3 import java.io.File;
4 import java.io.PrintStream;
5 
6 import com.gams.api.GAMSJob;
7 import com.gams.api.GAMSOptions;
10 
20 public class ConsoleInterrupt {
21 
22  public static void main(String[] args) {
23  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
24  boolean interactive = false;
25  if (args.length > 0) {
26  wsInfo.setSystemDirectory( args[0] );
27  if (args.length > 1) {
28  interactive = args[1].equals("interactive") ? true : false;
29  }
30  }
31 
32  File workingDirectory = new File(System.getProperty("user.dir"), "ConsoleInterrupt");
33  workingDirectory.mkdir();
34  wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
35  GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
36 
37  //Use a MIP that needs some time to solve
38  final GAMSJob job = ws.addJobFromGamsLib("circpack");
39  final GAMSOptions opt = ws.addOptions();
40  opt.setAllModelTypes("scip");
41 
42  // start thread asynchronously
43  final Worker w = new Worker(job, opt, System.out);
44  w.start();
45 
46  if (interactive) {
47  // register actions to be performed at termination, including user interrupt signal
48  Runtime.getRuntime().addShutdownHook(new Thread() {
49  @Override
50  public void run() {
51  // Send interrupt signal to running GAMSJob only when job is not yet terminated
52  boolean terminated = (w.getState()==Thread.State.TERMINATED);
53  boolean interrupted = false;
54  if (!terminated)
55  interrupted = job.interrupt();
56  System.out.println("*** job: " + job.getJobName() + " finished in interactive mode "
57  + (terminated ? "without interruption : " : " with interruption : " )
58  + interrupted
59  );
60  }
61  });
62  } else {
63  // interrupts the job after 2 seconds
64  try {
65  Thread.currentThread();
66  Thread.sleep(2000); }
67  catch ( Exception e ) {
68  e.printStackTrace();
69  System.exit(-1);
70  }
71  // Send interrupt signal to running GAMSJob only when job is not yet terminated
72  boolean terminated = (w.getState()==Thread.State.TERMINATED);
73  boolean interrupted = false;
74  if (!terminated)
75  interrupted = job.interrupt();
76  System.out.println("*** job: " + job.getJobName()+" finished in non-interactive mode "
77  + (interrupted ? "with " : "without ")
78  + "interrut signal sent."
79  );
80  }
81  }
82 
84  static class Worker extends Thread {
85  GAMSJob job;
86  GAMSOptions option;
87  PrintStream output;
88 
94  public Worker(GAMSJob jb, GAMSOptions opt, PrintStream out) { job = jb; option = opt; output = out; }
95 
97  @Override
98  public void run() {
99  try {
100  job.run(option, output);
101  } catch(Exception e) {
102  e.printStackTrace();
103  System.exit(-1);
104  }
105  }
106 
107  }
108 }
void setSystemDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
void setWorkingDirectory(String directory)
This example demonstrates how to interrupt a solve in a running job but then continues with the execu...
void setAllModelTypes(String value)