Changeset 38908 for main/trunk
- Timestamp:
- 2024-04-06T22:41:44+13:00 (2 months ago)
- Location:
- main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/CURL.java
r38871 r38908 135 135 public void readAll() { 136 136 int value; 137 while((value = read()) != -1) { 137 //while((value = read()) != -1) { 138 while(!Thread.currentThread().isInterrupted() && (value = read()) != -1) { 139 } 140 try { 141 input.close(); 142 } catch(IOException ioex) { 143 System.err.println("CURL tried to close the inputstream, but got exception " + ioex); 144 } finally { 145 if(Thread.currentThread().isInterrupted()) { 146 System.err.println("**** CURL.readAll() had been interrupted."); 147 } 138 148 } 139 149 } … … 159 169 else { 160 170 try { 161 value = input.read(); 171 value = input.read(); // what if this blocks?? How can this be made interruptible? 162 172 } catch (Exception e) { 163 173 e.printStackTrace(); -
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/Display.java
r38853 r38908 10 10 * @author Katrina Edgar */ 11 11 public class Display extends Thread { 12 12 13 13 GsdlCollageApplet app_ = null; 14 14 … … 16 16 super("Display"); 17 17 app_ = app; 18 19 System.err.println("@@@@ Someone called new Display()"); 18 20 19 21 } 20 22 21 23 // Do we even use this class? If so, maybe a separate stopRunning() method is better 24 // As I'm not sure if there needs to be synchronisation done around app_.isStopping()? 22 25 /** Runs display thread by repeatedly calling the next frame, 23 26 * waiting for a specified delay period between calls */ … … 29 32 Thread curr_thread = Thread.currentThread(); 30 33 31 while ( curr_thread == this) {34 while (!app_.isStopping() && curr_thread == this) { 32 35 app_.repaint(); 33 36 // can't make it zero or it hogs the whole processor … … 36 39 } 37 40 41 if(app_.isStopping() && app_.verbosity() >= 3) { 42 System.err.println("*** Display.run() thread has been told to stop."); 43 } 38 44 System.err.println("Stopped display thread."); 39 45 -
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DisplayImages.java
r38889 r38908 18 18 * to create movement when no new images have been downloaded. */ 19 19 public class DisplayImages extends Thread { 20 21 20 22 21 /** Will fire a removal operation when 4 images in the collage overlap a single area */ 23 22 static final int NO_IMAGES_OF_OVERLAP = 4; 23 24 private boolean stop_running = false; 24 25 25 26 /** To determine if there are several images overlapped, can remove the … … 141 142 Thread curr_thread = Thread.currentThread(); 142 143 143 while ( curr_thread == next_frame_) {144 while (!isStopping() && curr_thread == next_frame_) { 144 145 try { 145 146 Runtime rt = Runtime.getRuntime(); … … 158 159 } 159 160 160 161 if(isStopping() && DisplayImages.this.app_.verbosity() >= 3) { 162 System.err.println("*** DisplayImages inner thread next_frame has been told to stop."); 163 } 161 164 } 162 165 … … 342 345 343 346 } 347 348 // some other thread can call this method to tell this thread to stop running 349 public void stopRunning() { 350 stop_running = true; 351 if (app_.verbosity()>=3) { 352 System.err.println("**** DisplayImages.stopRunning() called"); 353 } 354 } 344 355 345 356 public boolean isStopping() { 357 return stop_running; 358 } 346 359 347 360 /** Paints the applet on the screen */ … … 421 434 Thread curr_thread = Thread.currentThread(); 422 435 423 while ( curr_thread == this) {436 while (!isStopping() && curr_thread == this) { 424 437 if (inuse_ready_.size() >= 1){ 425 438 Thread.sleep(1000); … … 438 451 System.out.println("Display images thread is interrupted"); 439 452 } 440 441 System.out.println("Display images thread stoped"); 453 454 if(isStopping() && app_.verbosity() >= 3) { 455 System.err.println("*** DisplayImages thread has been told to stop."); 456 } 457 System.out.println("DisplayImages thread stopped"); 442 458 443 459 } -
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DownloadUrls.java
r38885 r38908 8 8 import javax.swing.ImageIcon; //**** 9 9 10 //import org.apache.log4j.*; 10 11 11 12 /** … … 21 22 22 23 public class DownloadUrls extends Thread { 24 23 25 // for GS3 24 26 String baseURL = null; … … 35 37 String document_root_ = null; 36 38 37 39 /** When this thread is asked to stop running, this variable will be set to true */ 40 private boolean stop_running = false; 41 42 /** When this thread is asked to stop downloading, this variable will be set to true. 43 * For now the behaviour is the same as stop_running=true on this thread, 44 * but in case it changes in the future, we have a separate variable. 45 * Also, calling stopRunning() is not the same as setting stop_running = true, so 46 * to be careful, a separate variable for stop_downloading could be safer when coding. 47 */ 48 private boolean stop_downloading = false; 49 38 50 /** CHRIS - Holds the contents of the collection's assoc directory */ 39 51 // File[] assocDir_ = null; … … 181 193 } 182 194 183 184 195 // some other thread can call this method to tell this thread to stop running 196 public void stopRunning() { 197 if (verbosity_ >= 3) { 198 System.err.println("**** DownloadUrls.stopRunning() called"); 199 } 200 201 stop_running = true; 202 // Interrupt this thread, even if it's not the one running 203 // Just want to make sure the DownloadURls' thread the CURL object runs in 204 // gets interrupted if it's what's currently running 205 if(!this.isInterrupted()) { 206 this.interrupt(); 207 } 208 if(!Thread.currentThread().isInterrupted()) { 209 Thread.currentThread().interrupt(); 210 } 211 } 212 213 public boolean isStopping() { 214 return stop_running; 215 } 185 216 186 217 … … 390 421 try{ 391 422 // if have completed the maximum number of downloads for the 392 // application then stop 423 // application then stop downloading 393 424 if (size == app_.maxDownloads()) { 394 stop(); 425 // NOTE: the app can continue displaying images forever after download is 426 // finished, until interrupted/stopped. 427 // So don't set stop_running=false just because downloads have finished. 428 //stop_running = true; // Don't do this! 429 //thread_running = false; 430 //thread.currentThread().interrupt(); 431 432 stop_downloading = true; 433 //stop(); // TODO, remove this, replacing with above 434 395 435 } 396 436 … … 398 438 catch (Exception e) { 399 439 thread_running_ = false; 400 stop(); 440 //stop(); // TODO 441 stop_downloading = true; 401 442 e.printStackTrace(); 402 443 } … … 412 453 public void rec_add_images(String new_url, int depth) 413 454 { 414 415 455 if (verbosity_ >= 2) { 416 456 System.err.println("*** Inspecting url: " + new_url); … … 424 464 425 465 // connect to the url 466 // stopRunning would have set the interrupted flag, and 467 // CURL checks for that in its loop, *outside* its potentially-blocking read() call 426 468 CURL curl = (app_.gsdlversion == 3) ? new CURL(new_url, app_.baseURL) : new CURL(new_url); 427 469 … … 445 487 } 446 488 // process each of the image links according to the parameters given. 447 for (int i = 0; i < src_links.size() ; i++)489 for (int i = 0; i < src_links.size() && !stop_running && !stop_downloading; i++) 448 490 { 449 491 URL url = (URL)src_links.get(i); … … 491 533 } 492 534 535 if(stop_running && verbosity_ >= 3) { 536 System.err.println("*** DownloadUrls.rec_add_images() - Asked to stop running"); 537 return; 538 } 539 493 540 // get all the <code><a href=</code> links into a vector 494 541 Vector href_links = curl.getHrefLinks(); … … 500 547 501 548 // process each of the href links according to the parameters given. 502 for (int i = 0; i < href_links.size() ; i++)549 for (int i = 0; i < href_links.size() && !stop_running && !stop_downloading; i++) 503 550 { 504 551 … … 546 593 System.err.println("Unable able to download "+new_url); 547 594 } 595 596 if(stop_running && verbosity_ >= 3) { 597 System.err.println("*** DownloadUrls.rec_add_images() thread has been told to stop."); 598 } 548 599 } 549 600 … … 566 617 return; 567 618 } 568 619 569 620 // open a url to the file written 570 621 URL u = new URL(starting_url_ + "externallinks"); … … 576 627 String l = r.readLine(); 577 628 // split the line of the space, first part is the image, second part the link 578 while (l != null ) {629 while (l != null && !stop_running && !stop_downloading) { 579 630 580 631 String tmp1 = new String(); … … 600 651 r.close(); 601 652 653 if(stop_running && verbosity_ >= 3) { 654 System.err.println("*** DownloadUrls.externalLinks(): Asked to stop running"); 655 } 656 602 657 } catch (Exception e) { 603 658 e.printStackTrace(); … … 612 667 visited_url_ = new Hashtable(); 613 668 visited_images_ = new Hashtable(); 614 615 669 670 rec_add_images(starting_url_,1); 616 671 download_images_.stopDownload(); 617 System.err.println("Download thread finished.");672 System.err.println("DownloadUrls.run() - download thread finished."); 618 673 } 619 674 } -
main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/GsdlCollageApplet.java
r38890 r38908 48 48 int gsdlversion = 2; 49 49 String baseURL = null; 50 51 /** When asked to stop running, this variable will be set to true */ 52 private boolean stop_running = false; 50 53 51 54 /** Amount of error checking output produced <br> … … 222 225 this.X_DIM = (w <= 0) ? X_DIM : w; 223 226 this.Y_DIM = (h <= 0) ? Y_DIM : h; 227 228 // Attempting hack to circumvent division by zero error in MyAffineTransform line 39 229 //DisplayImages.app_x_dim_ = X_DIM; 230 //DisplayImages.app_y_dim_ = Y_DIM; 224 231 225 232 try { … … 459 466 } 460 467 468 // TODO: Do I need to follow 469 // https://stackoverflow.com/questions/5861894/how-to-synchronize-or-lock-upon-variables-in-java 470 public void stopRunning() { 471 if(verbosity_ >= 3) { 472 System.err.println("**** GsdlCollageApplet.stopRunning() called"); 473 } 474 475 stop_running = true; 476 if(download_thread_ != null) { 477 download_thread_.stopRunning(); 478 } 479 if(display_thread_ != null) { 480 display_thread_.stopRunning(); 481 } 482 483 // The Display Thread? 484 //if(!Thread.currentThread().isInterrupted()) { 485 // Thread.currentThread().interrupt(); 486 //} 487 } 488 489 public boolean isStopping() { 490 return stop_running; 491 } 492 461 493 protected class CollageMouseAdapter extends MouseAdapter { 462 494 … … 547 579 } 548 580 549 /** Stops threads for downloading and displaying images */ 581 /** Stops threads for downloading and displaying images */ 550 582 public void stop() 551 583 { 552 download_thread_.stop(); 553 display_thread_.stop(); 584 //download_thread_.stop(); 585 //display_thread_.stop(); 586 stopRunning(); 554 587 555 588 download_thread_ = null; … … 696 729 collageApp.showStatus("Stopping threads"); 697 730 System.err.println("\n\n*** Closing applet: stopping threads..."); 698 collageApp.stop(); 699 collageApp.destroy(); 731 collageApp.stopRunning(); 732 //collageApp.stop(); 733 //collageApp.destroy(); 700 734 } 701 735 });
Note:
See TracChangeset
for help on using the changeset viewer.