java - How to queue tasks in JavaFX? -
java - How to queue tasks in JavaFX? -
i have made gui using javafx, , there 3 radio buttons , 1 time user clicks submit , thread created , depending on radiobutton checked, thread runs required output , outputs result console.
but while thread running (it takes around 30 seconds 1 process complete) , able check on radiobutton. creates thread , outputs long other ongoing thread. output box jumble-wumble! looking @ asynchronous task not sure if related it.
here need: if task running, , click on submit button while running, wait previous task end , task.
here psuedo code of code
class testmain{ main class{ launch(args); } declaring new textfield name m_status update here 1 time submit button clicked { create new thread run } } class threadblahblah implements runnable{ if(first checkbox selected){ //do these fancy stuff platform.runlater(new runnable() { @override public void run() { testmain.m_status_update.settext("test completed!") ; } else(second checkbox selected){ //do these other fancy stuff platform.runlater(new runnable() { @override public void run() { testmain.m_status_update.settext("test completed!") ; } }
please not recommend me disable radio buttons while task running cause want queue tasks linked list.
use single-threaded executor run tasks:
import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.atomic.atomicinteger; import javafx.application.application; import javafx.beans.binding.bindings; import javafx.beans.property.integerproperty; import javafx.beans.property.simpleintegerproperty; import javafx.concurrent.task; import javafx.geometry.insets; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.textarea; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.stage.stage; public class queuedtaskexample extends application { private atomicinteger taskcount = new atomicinteger(0); private executorservice exec = executors.newsinglethreadexecutor(r -> { thread t = new thread(r); t.setdaemon(true); // allows app exit if tasks running homecoming t ; }); // utilize next if want tasks run concurrently, instead of consecutively: // private executorservice exec = executors.newcachedthreadpool(r -> { // thread t = new thread(r); // t.setdaemon(true); // homecoming t ; // }); @override public void start(stage primarystage) { // maintain track of number of tasks pending/running status label: integerproperty pendingtasks = new simpleintegerproperty(0); button startbutton = new button("start"); textarea textarea = new textarea(); textarea.seteditable(true); startbutton.setonaction(event -> { task<void> task = createtask(); // add together text text area if task's message changes: task.messageproperty().addlistener((obs, oldmessage, newmessage) -> { textarea.appendtext(newmessage); textarea.appendtext("\n"); }); // maintaining status label: pendingtasks.set(pendingtasks.get()+1); task.setonsucceeded(taskevent -> pendingtasks.set(pendingtasks.get()-1)); // run task in single-thread executor (will queue if task running): exec.submit(task); }); // layout etc hbox controls = new hbox(startbutton); controls.setalignment(pos.center); controls.setpadding(new insets(10)); label statuslabel = new label(); statuslabel.textproperty().bind(bindings.format("pending/running tasks: %s", pendingtasks)); borderpane root = new borderpane(textarea, statuslabel, null, controls, null); scene scene = new scene(root, 600, 400); primarystage.setscene(scene); primarystage.show(); } @override public void stop() { exec.shutdownnow(); } // trivial task counts 5, updating message goes: private task<void> createtask() { final int tasknumber = taskcount.incrementandget(); homecoming new task<void>() { @override public void call() throws exception { (int count=1; count<=5; count++) { thread.sleep(1000); updatemessage("task "+tasknumber+": count "+count); } homecoming null ; } }; } public static void main(string[] args) { launch(args); } }
java multithreading javafx
Comments
Post a Comment