Skip to content

Commit

Permalink
thread termination bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vit authored and Vit committed Mar 24, 2015
1 parent 9e15905 commit 5f4f415
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/dist/
/build/
dist/*
build/*
2 changes: 1 addition & 1 deletion build/built-jar.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tue, 24 Mar 2015 11:18:37 +0100
#Tue, 24 Mar 2015 12:58:19 +0100


C\:\\Users\\V\u00edt\\Documents\\NetBeansProjects\\tfetch2=
Binary file modified build/classes/zapoctak/Crawler.class
Binary file not shown.
Binary file modified build/classes/zapoctak/Fetcher.class
Binary file not shown.
Binary file modified build/classes/zapoctak/InvalidArgumentException.class
Binary file not shown.
Binary file modified build/classes/zapoctak/InvalidOperationException.class
Binary file not shown.
Binary file modified build/classes/zapoctak/MainOverviewController.class
Binary file not shown.
Binary file modified build/classes/zapoctak/TumblrFetchingService.class
Binary file not shown.
Binary file modified build/classes/zapoctak/Zapoctak.class
Binary file not shown.
4 changes: 2 additions & 2 deletions dist/Zapoctak.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function launchApplication(jnlpfile) {
dtjava.launch( {
url : 'Zapoctak.jnlp',
jnlp_content : 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjxqbmxwIHNwZWM9IjEuMCIgeG1sbnM6amZ4PSJodHRwOi8vamF2YWZ4LmNvbSIgaHJlZj0iWmFwb2N0YWsuam5scCI+DQogIDxpbmZvcm1hdGlvbj4NCiAgICA8dGl0bGU+WmFwb2N0YWs8L3RpdGxlPg0KICAgIDx2ZW5kb3I+Vu10PC92ZW5kb3I+DQogICAgPGRlc2NyaXB0aW9uPm51bGw8L2Rlc2NyaXB0aW9uPg0KICAgIDxvZmZsaW5lLWFsbG93ZW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ZXM+DQogIDxyZXNvdXJjZXM+DQogICAgPGoyc2UgdmVyc2lvbj0iMS42KyIgaHJlZj0iaHR0cDovL2phdmEuc3VuLmNvbS9wcm9kdWN0cy9hdXRvZGwvajJzZSIvPg0KICAgIDxqYXIgaHJlZj0iWmFwb2N0YWsuamFyIiBzaXplPSIxNDMyMiIgZG93bmxvYWQ9ImVhZ2VyIiAvPg0KICA8L3Jlc291cmNlcz4NCiAgPGFwcGxldC1kZXNjICB3aWR0aD0iODAwIiBoZWlnaHQ9IjYwMCIgbWFpbi1jbGFzcz0iY29tLmphdmFmeC5tYWluLk5vSmF2YUZYRmFsbGJhY2siICBuYW1lPSJaYXBvY3RhayIgPg0KICAgIDxwYXJhbSBuYW1lPSJyZXF1aXJlZEZYVmVyc2lvbiIgdmFsdWU9IjguMCsiLz4NCiAgPC9hcHBsZXQtZGVzYz4NCiAgPGpmeDpqYXZhZngtZGVzYyAgd2lkdGg9IjgwMCIgaGVpZ2h0PSI2MDAiIG1haW4tY2xhc3M9InphcG9jdGFrLlphcG9jdGFrIiAgbmFtZT0iWmFwb2N0YWsiIC8+DQogIDx1cGRhdGUgY2hlY2s9ImFsd2F5cyIvPg0KPC9qbmxwPg0K'
},
{
javafx : '8.0+'
Expand Down
Binary file modified dist/Zapoctak.jar
Binary file not shown.
8 changes: 5 additions & 3 deletions dist/Zapoctak.jnlp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
<jnlp spec="1.0" xmlns:jfx="http://javafx.com" href="Zapoctak.jnlp">
<information>
<title>Zapoctak</title>
<vendor>Vít</vendor>
<description>null</description>
<vendor>Vít Habada</vendor>
<description>simple tumblr downloader</description>
<icon href="tblogo.png" kind="splash"/>
<icon href="tblogo.ico" />
<offline-allowed/>
</information>
<resources>
<jfx:javafx-runtime version="8.0+" href="http://javadl.sun.com/webapps/download/GetFile/javafx-latest/windows-i586/javafx2.jnlp"/>
</resources>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="Zapoctak.jar" size="13521" download="eager" />
<jar href="Zapoctak.jar" size="14322" download="eager" />
</resources>
<applet-desc width="800" height="600" main-class="com.javafx.main.NoJavaFXFallback" name="Zapoctak" >
<param name="requiredFXVersion" value="8.0+"/>
Expand Down
1 change: 1 addition & 0 deletions nbproject/private/private.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ do.jar=true
endorsed.javafx.ant.classpath=.
javac.debug=true
javadoc.preview=true
javafx.run.as=standalone
javafx.run.inbrowser=<Default System Browser>
javafx.run.inbrowser.path=C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe
user.properties.file=C:\\Users\\V\u00edt\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
16 changes: 15 additions & 1 deletion nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.desc=simple tumblr downloader
application.title=Zapoctak
application.vendor=V\u00edt
application.vendor=V\u00edt Habada
auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
Expand Down Expand Up @@ -64,11 +66,19 @@ javafx.classpath.extension=\
${platforms.JDK_1.8.home}/jre/lib/javaws.jar:\
${platforms.JDK_1.8.home}/jre/lib/deploy.jar:\
${platforms.JDK_1.8.home}/jre/lib/plugin.jar
javafx.deploy.adddesktopshortcut=false
javafx.deploy.addstartmenushortcut=false
javafx.deploy.allowoffline=true
# If true, application update mode is set to 'background', if false, update mode is set to 'eager'
javafx.deploy.backgroundupdate=false
javafx.deploy.disable.proxy=false
javafx.deploy.embedJNLP=true
javafx.deploy.icon=tblogo.ico
javafx.deploy.icon.native=tblogo.ico
javafx.deploy.includeDT=true
javafx.deploy.installpermanently=false
javafx.deploy.permissionselevated=false
javafx.deploy.splash=tblogo.png
# Set true to prevent creation of temporary copy of deployment artifacts before each run (disables concurrent runs)
javafx.disable.concurrent.runs=false
# Set true to enable multiple concurrent runs of the same WebStart or Run-in-Browser project
Expand All @@ -89,6 +99,9 @@ javafx.preloader.type=none
javafx.rebase.libs=false
javafx.run.height=600
javafx.run.width=800
javafx.signing.blob=false
javafx.signing.enabled=false
javafx.signing.type=self
# Pre-JavaFX 2.0 WebStart is deactivated in JavaFX 2.0+ projects
jnlp.enabled=false
# Main class for Java launcher
Expand All @@ -100,6 +113,7 @@ manifest.custom.permissions=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
native.bundling.enabled=true
platform.active=JDK_1.8
run.classpath=\
${dist.jar}:\
Expand Down
2 changes: 1 addition & 1 deletion src/zapoctak/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void crawlCurrentPage() throws IOException, InvalidOperationException, I
while (matcher.find()) {
String f = matcher.group(1);
Matcher m = p.matcher(f);
if (m.find()) {
if (m.find() && TumblrFetchingService.addImageHash(f)) {
collectedLinks.add(f);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/zapoctak/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Consumer worker class.
Expand All @@ -15,6 +16,11 @@
*/
public class Fetcher extends AbstractWorker {

/**
* Number of finished fetchers.
*/
public static AtomicInteger deadCount = new AtomicInteger(0);

/**
* Shared queue reference.
*/
Expand All @@ -41,6 +47,7 @@ private boolean finishedCondition() {
public void run() {
while (true) {
if (cancelRequested || finishedCondition()) {
deadCount.incrementAndGet();
break;
}

Expand Down
26 changes: 25 additions & 1 deletion src/zapoctak/MainOverviewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public void cancelFetch(ActionEvent e) {
}
});
t.start();

}

/**
Expand Down Expand Up @@ -141,6 +140,31 @@ public void initFetch(ActionEvent e) {
private void startFetchingService() throws InvalidArgumentException {
fetchingService = new TumblrFetchingService(textField.getText());
fetchingService.start();

// natural stop - all worker threads finish
Runnable r = () -> {
while(Fetcher.deadCount.get() != TumblrFetchingService.FETCHER_COUNT && Crawler.deadCount.get() != TumblrFetchingService.CRAWLER_COUNT) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(TumblrFetchingService.class.getName()).log(Level.SEVERE, null, ex);
}
}

try {
fetchingService.stop();

cancelButton.setDisable(true);
fetchButton.setDisable(false);
textField.setDisable(false);
textField.setText("");
// TODO: throws exception footerLabel.setText("No job running");
} catch (InterruptedException ex) {
Logger.getLogger(MainOverviewController.class.getName()).log(Level.SEVERE, null, ex);
}
};

new Thread(r).start();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/zapoctak/TumblrFetchingService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zapoctak;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
Expand All @@ -25,6 +26,11 @@ public class TumblrFetchingService {
* Shared queue for the consumer-producer pattern.
*/
private final BlockingQueue<String> sharedQueue;

/**
* Set of downloaded images hash codes.
*/
private static final HashSet<Integer> imageSet = new HashSet<>();

/**
* List of producers.
Expand Down Expand Up @@ -98,12 +104,16 @@ public void start() throws InvalidArgumentException {

consumers.add(t);
}

// reset image hashset
imageSet.clear();

// reset cancel flag
Crawler.cancelRequested = false;

// reset deadCount
Crawler.deadCount.set(0);
Fetcher.deadCount.set(0);

// start all producers
producers.stream().forEach((t) -> {
Expand All @@ -115,5 +125,15 @@ public void start() throws InvalidArgumentException {
t.start();
});
}

/**
* Adds the passed string's hashcode to the set.
*
* @param s Image URL
* @return true if adding to the set was successful
*/
public static synchronized boolean addImageHash(String s) {
return imageSet.add(s.hashCode());
}

}
3 changes: 3 additions & 0 deletions src/zapoctak/Zapoctak.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
Expand All @@ -22,6 +23,8 @@ public void start(Stage stage) throws Exception {

Scene scene = new Scene(root);

stage.setTitle("tfetch by VITUSH");
stage.getIcons().add(new Image("file:tblogo.png"));
stage.setScene(scene);
stage.setResizable(false);
stage.show();
Expand Down
Binary file added tblogo.ico
Binary file not shown.
Binary file added tblogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5f4f415

Please sign in to comment.