chiark / gitweb /
thread fixes: names for threads, etc.
[jarrg-ian.git] / src / net / chiark / yarrg / MarketUploader.java
index 4e78282de05079d205058e1ba38d35879118aaf6..7df37f4a78c938e6da06797328aecc0bdd811559 100644 (file)
@@ -71,6 +71,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
   private AccessibleContext sidePanel;
   private HashMap<String,Integer> commodMap;
   public PrintStream dtxt = null;
+  private int uploadcounter = 0;
 
   /*
    * UTILITY METHODS AND SUBCLASSES
@@ -89,7 +90,8 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
   private void debuglog(String s) {
     if (dtxt == null) return;
     long now = new Date().getTime();
-    dtxt.println("progress "+(now - startTime)+"ms "+s);
+    dtxt.println("progress "+(now - startTime)+"ms "
+                +Thread.currentThread().getName()+": "+s);
   }
 
   private void debug_write_stringdata(String what, String data)
@@ -247,14 +249,14 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
   }
 
   private void progressNote(final String s_in) throws Exception {
-    new UIX() { public void body() {
+    new UIA() { public void body() {
       String arb = null;
       arb = arbitrageResult.getText();
       String s = s_in;
       if (arb != null && arb.length() != 0)
        s = "<html>" + arb + "<br>" + s;
       progmon.setNote(s);
-    }}.exec("progressNote");
+    }}.exec("progressNote "+s_in);
   }
        
   /*
@@ -351,15 +353,16 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     findMarket.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          on_ui_thread();
+         uploadcounter++;
          findMarket.setEnabled(false);
          resultSummary.setText("");
          arbitrageResult.setText("");
-         new Thread() {
+         new Thread("MarketUploader-uploader-"+uploadcounter) {
            public void run() {
              startTime = new Date().getTime();
              unknownPCTBcommods = 0;
              try {
-               runUpload();
+               runUpload(uploadcounter);
              } catch(Exception e) {
                error(e.toString());
                e.printStackTrace();
@@ -405,6 +408,20 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
    * error and error_html may be called from any thread
    */ 
 
+  private abstract class UIA implements Runnable {
+    private String what;
+    public abstract void body();
+    public void run() {
+      debuglog("UIA 2 "+what+" begin");
+      body();
+      debuglog("UIA 3 "+what+" done");
+    }
+    public void exec(String what_in) {
+      what = what_in;
+      debuglog("UIA 1 "+what+" request");
+      EventQueue.invokeLater(this);
+    }
+  };
   private abstract class UIXR<ReturnType> implements Runnable {
     public abstract ReturnType bodyr();
     public ReturnType return_value;
@@ -413,7 +430,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       debuglog("UIX 2 "+what+" begin");
       return_value = bodyr();
       debuglog("UIX 3 "+what+" done");
-    };
+    }
     public ReturnType exec(String what_in) throws Exception {
       what = what_in;
       if (EventQueue.isDispatchThread()) {
@@ -426,7 +443,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        debuglog("UIX 4 "+what+" (other thread) exit");
       }
       return return_value;
-    };
+    }
   };
   private abstract class UIX extends UIXR<Object> implements Runnable {
     public abstract void body();
@@ -469,9 +486,9 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
   }
 
   private void setProgress(final int nv) throws Exception {
-    new UIX() { public void body() {
+    new UIA() { public void body() {
       progmon.setProgress(nv);
-    }}.exec("setProgress");
+    }}.exec("setProgress "+nv);
   }
   private boolean isCanceled() throws Exception {
     return new UIXR<Boolean>() { public Boolean bodyr() {
@@ -574,6 +591,9 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
    *   @exception Exception if an error we didn't expect occured
    */
   private class YarrgTimestampFetcher extends Thread {
+    public YarrgTimestampFetcher(int counter) {
+      super("MarketUploader-YarrgTimestampFetcher-"+uploadcounter);
+    }
     public String ts = null;
     public void run() {
       try {
@@ -585,7 +605,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     }
   };
 
-  private void runUpload() throws Exception {
+  private void runUpload(int counter) throws Exception {
     on_our_thread();
 
     boolean doneyarrg = false, donepctb = false;
@@ -595,7 +615,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
 
     if (uploadToYarrg) {
       debuglog("(async) yarrg timestamp...");
-      yarrgts_thread = new YarrgTimestampFetcher();
+      yarrgts_thread = new YarrgTimestampFetcher(counter);
       yarrgts_thread.start();
     }