I have an application that should be able to:
Start:
only by clicking "Start" button
Stop:
clicking "Stop" button
clicking "X" button
when all jobs are done (basically Application.stop() method invoked from non-javafx thread)
All of the thing should happen "gracefully".
Buttons code:
#FXML
private void start() {
Application.start();
}
#FXML
private void stop() {
Application.stop();
}
And the main Application start() and stop() functions (whole Application class):
public class Application {
private static final AtomicBoolean isStarted = new AtomicBoolean(false),
startPending = new AtomicBoolean(false),
stopPending = new AtomicBoolean(false);
private static final Object lock = new Object();
//------Functionals below
public static synchronized void start() {
if(isStarted.get()) {
Gui.showError("Application is already started");
return;
} else if(startPending.get() || stopPending.get()) return;
isStarted.set(true);
startPending.set(true);
new Thread(startInternals()).start();
}
private static Runnable startInternals() {
return () -> {
synchronized(lock) {
//do something
Logger.print("Started");
startPending.set(false);
}
};
}
public static synchronized void stop() {
if(!isStarted.get()) {
Gui.showError("Application is not started");
return;
} else if(stopPending.get()) return;
isStarted.set(false);
stopPending.set(true);
new Thread(stopInternals()).start();
}
private static Runnable stopInternals() {
return () -> {
synchronized(lock) {
//do something
Logger.print("Stopped");
stopPending.set(false);
}
};
}
//------Notes below
/*
===RULES===
--START:
STARTED -> NO
STOPPED -> YES
STARTING -> NO
STOPPING -> NO
--STOP:
STARTED -> YES
STOPPED -> NO
STARTING -> YES
STOPPING -> NO
*/
}
Is this approach fully correct? Is there any way I could do it better/easier?
PS. I've added "stop" ability when starting in case of "X" clicking (the app should terminate gracefully". Take a look at the bottom of the file for notes.
Related
I have a simple class which has a thread in it. Whenever user clicks on a button, I want to run the thread. Everything works great, except that whenever I click on the button while the thread is already running, my onTouch executes only after the thread has finished executing.
..
MyTouchAnimatorClass touchAnimatorClass = new MyTouchAnimatorClass();
view.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick( View view )
{
touchAnimatorClass.onTouch();
}
});
..
And my class with the thread:
Class MyTouchAnimatorClass
{
private boolean threadRunning = false;
public void onTouch() // <-- only executes AFTER the thread is complete
{
if( !threadRunning )
animate();
}
private void animate()
{
threadRunning = true;
Runnable r = new Runnable()
{
#Override
public void run()
{
activity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
int i=0;
boolean doBreak = false;
while( true )
{
i+=1;
if( i == 100 )
break;
}
}
}
}
}
new Thread( r ).start();
threadRunning = false;
}
}
I can't understand why this is happening, isn't the Runnable running in its own thread? Any suggestions please? Thank you!
EDIT:
I tried using
new Thread( r ).start();
Instead of:
r.run();
But unfortunately the same problem persists:
If I click the button the second time (while the thread is still running from the first click), it should execute onTouch, but shouldn't execute animate() since it is still running from the previous click.
What actually happens: The touch is non response until the thread from the first click has finished executing. Then onTouch triggers (even though I pressed it few seconds ago) and a new thread is started again.
If I press the button 10 times quickly, it will do 10 loops in a row, instead of doing one and blocking the others.
activity.runOnUiThread is asking the Android system to run this Runnable on the UI thread.
You can rather do new Thread(r).start();
Like this:
private void animate()
{
threadRunning = true;
//Define the work as a Runnable
Runnable r = new Runnable()
{
#Override
public void run()
{
int i=0;
//boolean doBreak = false; //Not used
while( true )
{
i+=1;
if( i == 100 )
break;
}
threadRunning = false; //Just before leaving
}
}
//Give that work to a new thread and start the thread
new Thread(r).run();
}
Runnable isn't a thread, is just a set of instructions to be executed in a thread
in order to achieve what you want you need to do
new Thread(r).start(); at end of your code
I have an application that has a TableView that has an attached listener so it refreshes as soon as it detects a change, but the thing is that I´m getting java.lang.IllegalStateException: Not on FX application thread; currentThread = Smack Listener Processor (0).
Here is my code:
/**
* This function resets the pagination pagecount
*/
public void resetPage() {
try {
System.out.println("RESET");
int tamRoster = this.loginManager.getRosterService().getRosterList().size();
paginationContactos.setPageCount((int)(Math.ceil(tamRoster*1.0/limit.get())));
int tamEnviados = this.loginManager.getRosterService().getEnviadasList().size();
paginationEnviadas.setPageCount((int)(Math.ceil(tamEnviados*1.0/limit.get())));
int tamRecibidas = this.loginManager.getRosterService().getRecibidasList().size();
paginationRecibidas.setPageCount((int)(Math.ceil(tamRecibidas*1.0/limit.get())));
} catch (Exception e) {
e.printStackTrace();
}
}
public void doSomething () {
this.loginManager.getRosterService().getRosterList().addListener(new ListChangeListener<RosterDTO>() {
#Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends RosterDTO> c) {
// TODO Auto-generated method stub
resetPage();
while (c.next()) {
if (c.wasPermutated()) {
System.out.println("PERM");
} else if (c.wasUpdated()) {
System.out.println("UPD");
} else {
System.out.println("ELSE");
}
}
}
});
}
Altough it enters the resetPage method, I get that exception.
Why is this happening?
How can I fix it?
The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater(), with the logic inside the Runnable object. For example:
Platform.runLater(new Runnable() {
#Override
public void run() {
// Update UI here.
}
});
As a lambda expression:
// Avoid throwing IllegalStateException by running from a non-JavaFX thread.
Platform.runLater(
() -> {
// Update UI here.
}
);
JavaFX code allows updating the UI from an JavaFX application thread. But from the above exception message it says that it is not using FX Application thread.
One way you can fix is to launch an FX Application thread from the resetPage method and do the modifications there.
I had a similar issue which I fixed without using the Platform.runLater():
"java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-6"
Button play = new Button("Play");
EventHandler<ActionEvent> playHandler = e1 -> {
Runnable runnable = () -> {
play.setText("Pause");
EventHandler<ActionEvent> timelineHandler e2 -> play();
timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
};
Thread t = new Thread(runnable);
t.start();
};
play.setOnAction(playHandler);
SOLVED! Move everything unrelated outside of the runnable lambda:
Button play = new Button("Play");
EventHandler<ActionEvent> playHandler = e1 -> {
play.setText("Pause");
Runnable runnable = () -> {
EventHandler<ActionEvent> timelineHandler e2 -> play();
timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
};
Thread t = new Thread(runnable);
t.start();
};
play.setOnAction(playHandler);
I am unclear on how to use wait() and notify() to pause a thread. I read talk about synchronizing, but I'm unsure how to do it in my instance. I have a music player with a progress bar where I want to pause the thread in control of syncing the progress bar with the music. Here is the thread I want to pause:
#FXML private void clickedButton(ActionEvent event){
shuffle.setOnAction(e -> {
artistPane.setText(model.getCurrentSong());
if(firstTime){
//Multithreading with JavaFX. Essentially this other thread will check the slider to make sure its on track.
sliderThread = new Task<Void>() {
#Override
protected Void call() throws Exception {
boolean fxApplicationThread = Platform.isFxApplicationThread();
System.out.println("Is call on FXApplicationThread: " + fxApplicationThread);
//this is an infinite loop because now I only need to make this thread once, pausing and starting it, as opposed to making many threads
for(;;){
Thread.sleep(100);
progressBar.setValue(controller.getPercentageDone());
}
}
};
new Thread(sliderThread).start();
firstTime = false;
}else if(!model.getIsPlaying()){
//I want to start the thread here
}
controller.shuffle(); //this will start the music on the next song
});
Here is the second half where I also want to pause and start the thread:
play.setOnAction(e -> {
controller.play(); //this will pause/start the music
if(!model.getIsPlaying()){
//where I want to pause the thread.
}else{
//I want to start the thread here
}
});
I will try to give you simple example and you try to apply it to your program...
public class TestClass extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private Thread playThread ;
TestClass() {
playThread = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("DO SOME THING HERE");
System.out.println("SONG WILL PLAY.....");
}
});
}
public void startMyPlayer() {
System.out.println("PLAYING NOW...");
playThread.start();
}
public void pauseMyPlayer() throws InterruptedException {
System.out.println("PAUSED NOW...");
playThread.wait();
}
public void resumeMyPlayer() {
System.out.println("RESUMING NOW...");
playThread.notify();
}
}
That's it.I hope this help you.
I want a progress indicator to stop only after completely updating the ui. But it stops after executing last line of code in ui method. The indicator should work as:
1> Start the progress
2> Run the progress until all nodes are added to the scene
3> Stop after completion
The way i update is, i pass BorderPane to the thread and set its center a gridpane, which is the last line of code after which the indicator stops.
And the loginTask, if i start it inside Application Thread the indicator does not spin !
borderpane.setCenter(gpane);
UI method
{
Loading loadBar = new Loading(stage);
Task<Boolean> loginTask= checkCredTask();
loginTask.setOnSucceeded(value -> {
loadBar.hideProgress(); });
loadBar.startTask(loginTask);
(new Thread(loginTask)).start();
}
Progress Bar
public class Loading{
private static Stage stage;
private static ProgressIndicator p;
private static Alert alert;
public Loading(Stage s){
stage=s;
p=new ProgressIndicator();
alert = new Alert(AlertType.NONE);
}
public void startTask(Task<Boolean> cm){
if(p != null){
p.setProgress(-1);
p.progressProperty().unbind();
p.progressProperty().bind(cm.progressProperty());
alert.initOwner(stage);
alert.getDialogPane().setStyle("-fx-background-color:rgba(0,0,0,0);");
alert.getDialogPane().getScene().setFill(Color.TRANSPARENT);
alert.initStyle(StageStyle.TRANSPARENT);
alert.getDialogPane().setContent(p);
alert.show();
}
}
public void hideProgress(){
alert.setResult(ButtonType.CLOSE);
}
}
Task
private Task<Boolean> checkCredTask() {
Task<Boolean> loginTask = new Task<Boolean>() {
#Override
public Boolean call() {
Boolean result = false;
int flag = verifyCredential();
if (flag == 1) {
loadUI();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result = true;
} else if (flag == 2) {
result = false;
}
return result;
}
};
return loginTask;
}
load UI method
ExecutorService execsrv = Executors.newSingleThreadExecutor();
execsrv.execute(new AdminUI(stage,pane,mb));
execsrv.shutdown();
Your code won't even compile. Task<Boolean> loginTask does not override abstract call() method. Even if you wrap
ExecutorService execsrv = Executors.newSingleThreadExecutor();
execsrv.execute(new AdminUI(stage,pane,mb));
execsrv.shutdown();
in call() method it makes no sense, because you are executing new task inside another task.
Assumig that new AdminUI(stage,pane,mb) is long running process and return boolean you should do it in this way:
Task<Boolean> loginTask = new Task<Boolean>() {
#Override
protected Boolean call() throws Exception {
// Start the UI
AdminUI ui = new AdminUI(stage, pane, mb);
return ui.getBooleanValue();
}
};
EDIT
As I said, you are executing AdminUI task inside loginTask what makes no sense. For now you are waiting only for login task but not for AdminUI task. Login task finishes faster than AdminUI that is why indicator stops to early. Extract AdminUI to another Task. This is pseudo code so you have to do it by your self.
Task<Boolean> loginTask= checkCredTask();
loginTask.setOnSucceeded(value -> {
//****** THIS IS PSEUDO CODE ***********
//start AdminUITask
//AdminUITask.setOnSucceeded(value -> {
//loadBar.hideProgress();
//});
});
I have an application that has a TableView that has an attached listener so it refreshes as soon as it detects a change, but the thing is that I´m getting java.lang.IllegalStateException: Not on FX application thread; currentThread = Smack Listener Processor (0).
Here is my code:
/**
* This function resets the pagination pagecount
*/
public void resetPage() {
try {
System.out.println("RESET");
int tamRoster = this.loginManager.getRosterService().getRosterList().size();
paginationContactos.setPageCount((int)(Math.ceil(tamRoster*1.0/limit.get())));
int tamEnviados = this.loginManager.getRosterService().getEnviadasList().size();
paginationEnviadas.setPageCount((int)(Math.ceil(tamEnviados*1.0/limit.get())));
int tamRecibidas = this.loginManager.getRosterService().getRecibidasList().size();
paginationRecibidas.setPageCount((int)(Math.ceil(tamRecibidas*1.0/limit.get())));
} catch (Exception e) {
e.printStackTrace();
}
}
public void doSomething () {
this.loginManager.getRosterService().getRosterList().addListener(new ListChangeListener<RosterDTO>() {
#Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends RosterDTO> c) {
// TODO Auto-generated method stub
resetPage();
while (c.next()) {
if (c.wasPermutated()) {
System.out.println("PERM");
} else if (c.wasUpdated()) {
System.out.println("UPD");
} else {
System.out.println("ELSE");
}
}
}
});
}
Altough it enters the resetPage method, I get that exception.
Why is this happening?
How can I fix it?
The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater(), with the logic inside the Runnable object. For example:
Platform.runLater(new Runnable() {
#Override
public void run() {
// Update UI here.
}
});
As a lambda expression:
// Avoid throwing IllegalStateException by running from a non-JavaFX thread.
Platform.runLater(
() -> {
// Update UI here.
}
);
JavaFX code allows updating the UI from an JavaFX application thread. But from the above exception message it says that it is not using FX Application thread.
One way you can fix is to launch an FX Application thread from the resetPage method and do the modifications there.
I had a similar issue which I fixed without using the Platform.runLater():
"java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-6"
Button play = new Button("Play");
EventHandler<ActionEvent> playHandler = e1 -> {
Runnable runnable = () -> {
play.setText("Pause");
EventHandler<ActionEvent> timelineHandler e2 -> play();
timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
};
Thread t = new Thread(runnable);
t.start();
};
play.setOnAction(playHandler);
SOLVED! Move everything unrelated outside of the runnable lambda:
Button play = new Button("Play");
EventHandler<ActionEvent> playHandler = e1 -> {
play.setText("Pause");
Runnable runnable = () -> {
EventHandler<ActionEvent> timelineHandler e2 -> play();
timeline = new Timeline(new KeyFrame(Duration.millis(2000), timelineHandler));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
};
Thread t = new Thread(runnable);
t.start();
};
play.setOnAction(playHandler);