Server-GUI freezes when waiting for connections - java

I'm trying to create a multithreaded server, connecting to it through a client works fine, but the GUI is frozen. Probably since it is constantly listening for incoming connections it is stuck in a while loop. So, do i need to create a thread for the multithreaded server (which itself will be creating threads) or how would i go about fixing this?
MultiThreadedServer:
public class MultiThreadServer{
public static final int PORT = 2000;
//function below is called by the GUI.
public void startServer() throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server socket: " + s);
System.out.println("Server listening...");
try { while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
System.out.println("Connection accepted.");
System.out.println("The new socket: " + socket);
try {
ClientThread t = new ClientThread(socket);
System.out.println("New thread started.");
System.out.println("The new thread: " + t);
}
catch(IOException e) {
System.out.println("===Socket Closed===");
socket.close();
}
}
}
finally { System.out.println("===ServerSocket Closed==="); s.close(); }
}
}
}
ServerGUI:
public class ServerGUI extends Application implements Initializable{
#FXML private MenuItem startConn;
#Override
public void start(Stage stage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("ServerGUI.fxml"));
stage.setScene(new Scene(root));
stage.show();
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
startConn.setOnAction(e -> {
try {
MultiThreadServer mts = new MultiThreadServer();
mts.startServer();
System.out.println("Starting Server...");
} catch (IOException e1) {
e1.printStackTrace();
}
});
}
}

As i suspected, a new thread for the server was required.
MultiThreadServer:
public Runnable startServer() throws IOException {
After making the function Runnable i added this:
ServerGUI:
startConn.setOnAction(e -> {
Thread t1 = new Thread(new Runnable() {
public void run()
{
MultiThreadServer mts = new MultiThreadServer();
try {
mts.startServer();
} catch (IOException e) {
e.printStackTrace();
}
}});
t1.start();
Thanks to user BackSlash for providing reference-link.

Related

Handle Java Socket connection between 2 JFrames

I'm trying to create a client/server application using sockets, I have 2 JFrames (2 seperate classes) , a user will initially open up the one frame and there's a button to go to the other JFrame, when clicked it disposes the previous frame and opens the new frame.
I'd like to know how I could switch back and forth between these 2 JFrames without my program crashing and needing to forcefully close, I am establishing the connection in the constructor of each JFrame.
try {
server = new Socket("localhost", PORT);
// creates & instantiates objectInput and output streams
getStreams();
} catch (IOException ex) {
System.out.println("error creating socket: " + ex.getMessage());
}
I have these in the constructor of both JFrames
EDIT
Server
public class Testserver {
private ServerSocket serverSocket;
private Socket client;
private final int PORT = 5432;
private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
public Testserver() {
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
System.out.println("error creating server socket: " + ex.getMessage());
}
}
private void listenForClient() {
try {
System.out.println("Server is running and is waiting/listening for a connection to be established.");
////JOptionPane.showMessageDialog(null, "Server is running and is waiting/listening for a connection to be established." );
client = serverSocket.accept();
System.out.println("A client has connected");
objectOutputStream = new ObjectOutputStream(client.getOutputStream());
objectInputStream = new ObjectInputStream(client.getInputStream());
processClient();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}
}
private void processClient() {
do {
try {
String messageFromClient = (String) objectInputStream.readObject();
// check for clients requests and handle them (database etc)
System.out.println("[CLIENT] " + messageFromClient);
} catch (IOException ex) {
Logger.getLogger(Testserver.class.getName()).log(Level.SEVERE, null, ex);
break;
} catch (ClassNotFoundException ex) {
Logger.getLogger(Testserver.class.getName()).log(Level.SEVERE, null, ex);
break;
}
} while (true);
closeConnections();
}
private void closeConnections() {
try {
objectInputStream.close();
objectInputStream.close();
client.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
new Testserver().listenForClient();
}
}
CustomerGUI
public class CustomerGUI extends JFrame implements ActionListener{
private Socket server;
private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
private JPanel panel1;
private JButton btnAdminGUI;
private final int PORT = 5432;
public CustomerGUI() {
btnAdminGUI = new JButton("Go to Admin GUI");
try {
server = new Socket("localhost", PORT);
System.out.println("Connected to server");
objectOutputStream = new ObjectOutputStream(server.getOutputStream());
objectInputStream = new ObjectInputStream(server.getInputStream());
objectOutputStream.writeObject("from Customer");
objectOutputStream.flush();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage()););
}
btnAdminGUI.addActionListener(this);
}
public void setGUI() {
add(btnAdminGUI);
setSize(300, 400);
setVisible(true);
}
public static void main(String[] args) {
new CustomerGUI().setGUI();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnAdminGUI) {
new AdminGUI().setGUI();
dispose();
}
}
}
AdminGUI
public class AdminGUI extends JFrame implements ActionListener{
private Socket server;
private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
private JPanel panel1;
private JButton btnCustomerGUI;
private final int PORT = 5432;
public AdminGUI() {
btnCustomerGUI = new JButton("Go to Customer GUI");
try {
server = new Socket("localhost", PORT);
System.out.println("Connected to server");
objectOutputStream = new ObjectOutputStream(server.getOutputStream());
objectInputStream = new ObjectInputStream(server.getInputStream());
objectOutputStream.writeObject("from Admin");
objectOutputStream.flush();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}
btnCustomerGUI.addActionListener(this);
}
public void setGUI() {
add(btnCustomerGUI);
setSize(300, 400);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnCustomerGUI) {
new CustomerGUI().setGUI();
dispose();
}
}
}
I'll post a solution this because I have done something similar recently, but I don't have a UI for it yet.
The idea is MVC, with two models, a Server and Client and there are two Views, JPanels, which can be displayed in a window/jframe/dialog, whichever is appropriate. The Controller is essentially an api for interacting with the model, for this simple example the controllers will be mixed with the models. I think this example has a lot of flaws, but I think it gives a good idea of what needs to be done.
A button starts a course of action,
serverControls.addActionListener( evt ->{ } );
The button does one of two actions, either it starts the server or it stops the server.
serverLoop.submit( ()->{
server.listen();
//should notify a listener that the server has stopped.
});
serverLoop is another thread of execution, server.listen() is a long running task. It shouldn't return until we want the server to stop listening.
The other button is on the client. It has a similar structure..
clientControls.addActionListener( evt->{
clientLoop.submit( () -> client.connect( server ) );
clientLoop.submit(
() -> SwingUtilities.invokeLater(
() -> response.setText( client.communicate() )
)
);
});
First the client is going to connect, then it communicates.
import javax.swing.*;
import java.util.concurrent.*;
public class ClientServerApp{
static class Server{
volatile boolean available = false;
public void listen(){
try{
available = true;
synchronized( this ){
wait(5000);
}
} catch(Exception e){
e.printStackTrace();
//if the server terminates unexpectedly.
}finally{
available = false;
}
}
public void stopListening(){
}
}
static class Client{
Server connected;
public void connect(Server host){
connected = host;
}
public String communicate(){
if(connected != null){
if(connected.available){
return "connected";
} else{
return "cannot connect";
}
}
return "no host";
}
}
public static void main(String[] args){
Server server = new Server();
Client client = new Client();
ExecutorService serverLoop = Executors.newSingleThreadExecutor();
JPanel serverView = new JPanel();
JButton serverControls = new JButton("start");
serverView.add( serverControls );
serverControls.addActionListener( evt ->{
if(serverControls.getText().equals("start") ){
serverControls.setText("stop");
serverLoop.submit( ()->{
server.listen();
//should notify a listener that the server has stopped.
});
} else{
server.stopListening();
serverLoop.submit( ()->{
//will be run after the listen loop has completed.
SwingUtilities.invokeLater( () - >
serverControls.setText("start")
);
});
}
} );
JPanel clientView = new JPanel();
JButton clientControls = new JButton("connect");
JTextField response = new JTextField(40);
clientView.add( clientControls );
clientView.add(response);
ExecutorService clientLoop = Executors.newSingleThreadExecutor();
System.out.println("creating client action listener");
clientControls.addActionListener( evt->{
clientLoop.submit( () -> client.connect( server ) );
clientLoop.submit( () ->
SwingUtilities.invokeLater(
() -> response.setText( client.communicate() )
)
);
});
JFrame mainWindow = new JFrame();
mainWindow.setContentPane( serverView);
mainWindow.pack();
mainWindow.setVisible(true);
JDialog clientWindow = new JDialog( mainWindow, "Client Window");
clientWindow.setContentPane(clientView);
clientWindow.pack();
clientWindow.setVisible(true);
}
}
In a more complete example, you would probably have a Listener interface, so that your swing gui can response to changes in state of the server or the client, and a controller that manages the threads.

Conflicts between JavaFX and Socket

I'm trying to set up a Server with a simple GUI where I print when a client make a connection and for its standard operations. The problem happens when I try to execute it, because the GUI doesn't start (even if NetBeans says that the program is executed) and I can't understand if there is a sort of conflict between the threads or if I called it in a wing way. This is the code where the program starts:
public class Server extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader sLoader = new FXMLLoader(getClass().getResource("server.fxml"));
BorderPane root = new BorderPane(sLoader.load());
ServerController sController = sLoader.getController();
sController.initModel();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
This is the controller of the Server, I put here, in the init function, the part where the Socket waits for the client:
public class ServerController {
#FXML
private TextArea textarea;
public void initModel() {
try {
int i = 1;
ServerSocket s = new ServerSocket(5000);
while (true) {
Socket incoming = s.accept(); // si mette in attesa di richiesta di connessione e la apre
textarea.setText("In attesa di connessioni...");
Runnable r = new ThreadedEchoHandler(incoming, i);
new Thread(r).start();
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
class ThreadedEchoHandler implements Runnable {
private Socket incoming;
private int counter;
/**
* Constructs a handler.
*
* #param i the incoming socket
* #param c the counter for the handlers (used in prompts)
*/
public ThreadedEchoHandler(Socket in, int c) {
incoming = in;
counter = c;
}
//Con questo gestisco le stringhe
public void run() {
try {
try {
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);
//out.println("Hello! Enter BYE to exit.");
// echo client input
boolean done = false;
while (!done && in.hasNextLine()) {
String line = in.nextLine();
//out.println("Echo: " + line);
//System.out.println("ECHO: " + line);
if (line.trim().equals("BYE")) {
done = true;
}
}
} finally {
incoming.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Java Multiple threads using a client socket

I'm creating a server based chat program that has multiple worker threads each handling a client on a socket. It has a single server socket that passes off the client to the worker thread.
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4001);
System.out.println("Listening server");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Connected");
Random rand= new Random();
int port=4001+rand.nextInt(5);
Worker worker = new Worker(port);
executor.execute(worker);
System.out.println("Thread started");
new PrintWriter(socket.getOutputStream(), true).println(port);
socket.close();
System.out.println("Closed");
// break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public class Worker implements Runnable {
private int port;
public Worker(int i) {
port=i;
}
#Override
public void run() {
worker();
}
private static Socket socket;
private static PrintWriter out;
private static BufferedReader in;
private void worker() {
try {
ServerSocket serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("Received: " + line);
switch (line) {
case ("Button Press"):
System.out.println("Handled button");
out.println("Button acknowledged");
break;
case ("Give me some data"):
System.out.println("Handled data");
out.println("Have some data");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This works fine, however the issue is when I have an automated request by the client to check for messages and the user provides some input at the same type. This causes conflict as the actual methods take a couple of seconds to run and if more input is received then the request won't be handled because its in the middle of the method. For example:
private static BufferedReader in;
private static PrintWriter out;
public static void main(String[] args) {
Main main=new Main();
main.createWindow();
try {
Socket init = new Socket(InetAddress.getLocalHost(), 4001);
int port
=Integer.parseInt
(new BufferedReader
(new InputStreamReader(init.getInputStream())).readLine());
init.close();
Socket socket=new Socket(InetAddress.getLocalHost(), port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
try {
Thread.sleep(5000);
out.println("Give me some data");
if(in.readLine().equals("Have some data")){
System.out.println("Data recieved");
}else{
System.out.println("Data not recieved");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void createWindow(){
JFrame frame =new JFrame("This is a button");
Container pane=getContentPane();
JButton button=new JButton("This is a button");
button.addActionListener(this);
pane.add(button);
frame.setTitle("Messaging");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(pane);
frame.setVisible(true);
frame.setSize(400, 350);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button press");
try {
out.println("Button Press");
if(in.readLine().equals("Button acknowledged")){
System.out.println("Button complete");
}else{
System.out.println("Button fail");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
If the button is pressed whilst the server is fetching data it conflicts and the wrong response is sent. So how would I deal with this? If I create a separate socket to just handle automated checks that's double the amount of threads the server has to cope with. Is there a better solution?
And please can you try to explain as this is a new area for me.
Your problem is that you have two threads interacting with the socket, your main thread and the Swing event handling thread. This means that the two threads can queue two different things, and the responses may be picked up by the wrong thread. The simplest way to get where you want to be is to put all the socket interaction in one thread.
There are two ways to do this. One would be for the main thread to queue the periodic automated checks to the Swing event handling thread. However, that's a bit complicated and also possibly buggy as the actual Swing threading model is different from what's documented.
The other way to do it would be for the Swing event handling thread to queue button presses to the main thread. In this case, you would queue the button press to the main thread using proper synchronization. The main loop of the main thread would check for button presses and send them through the socket and wait for the proper response.

Update TextFlow from another class JavaFx

I'm here with my JavaFx app.
I have two class:
I start these class in two different Thread. Because server is blockable.
My UI class ( Called Main ( I know i need to change this)).
A Server class ( Called Serveur )).
In my Server Class when i receive bytes with a ServerSocket.
I need to update a TextFlow (called flowMessafe) in my UI class :
with flowMessage.getChildren().add(); I can update this TextFlow without problem in my UI class.
But i my Server class i can't.
EDIT : I try all the things, but i think i found a big problem. I update the wrong instance of my javafx app. I change the code with current code
He is a part of my server code
My Server Constructor
public Serveur(Mediator med){
this.med=med;
try {
lancer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Loop to catch message or file.
for(;;){
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Attente de communication ...");
try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
byte[] bytes = new byte[16*1024];
while ((count = in.read(bytes)) > 0) {}
String mode = new String(bytes);
String[] split = mode.split(";");
if(split[0].compareTo("Message")==0){
recevoirMessage();
} else if(split[0].compareTo("Fichier")==0){
recevoirFichier(split[2]);
}
in.close();
socket.close();
}
When i receive a message i go to this function :
public void recevoirMessage() {
output = new ByteArrayOutputStream();
try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
byte[] bytes = new byte[16*1024];
try {
while ((count = in.read(bytes)) > 0) {}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Message reçus");
String recu = "Message : "+new String(bytes);
System.out.println(recu);
Platform.runLater(() -> {
Label mes = new Label(new String(bytes));
med.cli.flowMessage.getChildren().add(mes);
});
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And in my main i have only empty constructor like
public Main(){}
In my UI class i have this to create my App :
'#Override
public void start(Stage primaryStage) {
try {
this.pStage = primaryStage;
this.pStage = new Stage();
idPane = new BorderPane();
Parent page;
page = FXMLLoader.load(getClass().getResource("/application/application.fxml"));
this.pStage.setTitle("Messagerie");
Scene scene = new Scene(page);
flowMessage = new TextFlow();
idContenuMessage= new ChoiceBox<String>();
scrollPane= new ScrollPane();
//flowMessage = new TextFlow();
String css = this.getClass().getResource("application.css").
toExternalForm();
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
this.pStage.setScene(scene);
this.pStage.setResizable(false);
this.pStage.show();
this.pStage.setOnCloseRequest(event -> {
Serveur.close();
});
} catch (IOException e) {
e.printStackTrace();
}
}'
And i don't know how to update my UI TextFlow in my server Class.
I saw different things like the Mediator Pattern, i try this but it didn't work ( maybe i do something wrong ).
I start my app with this class :
package communication;
import application.Main;
import javafx.application.Application;
import javafx.stage.Stage;
public class Mediator extends Application implements Runnable {
private Serveur serv;
public Main cli;
public Thread thread;
private Stage primaryStage;
public static void main(String args[]){
launch(args);
}
public Mediator(){
cli = new Main();
thread = new Thread(this,"serv");
thread.start();
}
#Override
public void run() {
setServ(new Serveur(this));
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
cli.start(primaryStage);
}
public Serveur getServ() {
return serv;
}
public void setServ(Serveur serv) {
this.serv = serv;
}
}
Thanks for Helping.
I would use Platform.runLater() to put your GUI changes on the Javafx application thread, e.g.
public void recevoirMessage() {
output = new ByteArrayOutputStream();
try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
byte[] bytes = new byte[16*1024];
try {
while ((count = in.read(bytes)) > 0) {}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Message reçus");
String recu = "Message : "+new String(bytes);
System.out.println(recu);
//inserted here
Platform.runLater(new Runnable() {
#Override public void run() {
//HERE I WANT TO UPDATE MY TEXTFLOW
}
});
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Firstly TextFlow does not have text property so you can't set that value to it. Add Text object to text flow:
Text text = new Text();
flowMessage = new TextFlow(text);
Then create StringProperty, bind it to Text component textProperty and update this value in Server class.
Application class:
public class Mediator extends Application implements Runnable {
private Serveur serv;
public Main cli;
private StringProperty textProperty = new SimpleStringProperty("text");
public Thread thread;
private Stage primaryStage;
public static void main(String args[]){
launch(args);
}
public Mediator(){
cli = new Main(this,textProperty);
thread = new Thread(this,"serv");
thread.start();
}
#Override
public void run() {
serv = new Serveur(this,textProperty);
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
cli.start(primaryStage);
}
}
Pass textProperty to Main and Serveur classes.
cli = new Main(this, textProperty);
serv = new Serveur(this, textProperty);
Bind text property to Text component:
text.textProperty().bind(textProperty);
Finally update textProperty in JavaFX Application Thread in your Serveur class:
public void recevoirMessage() {
output = new ByteArrayOutputStream();
try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
byte[] bytes = new byte[16*1024];
try {
while ((count = in.read(bytes)) > 0) {}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Message reçus");
String recu = "Message : "+new String(bytes);
System.out.println(recu);
//inserted here
Platform.runLater(new Runnable() {
#Override public void run() {
//HERE I WANT TO UPDATE MY TEXTFLOW
textProperty.setValue(new String(bytes));
}
});
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

JavaFX : Use a Thread more than once

I'm new with JavaFX and I've a little problem with a thread: I can execute it twice and I can't find why.
Here is a sum-upt of my code:
Task<Void> task = new Task<Void>() {
#Override public Void call() throws ImageLoadedException, HomographyException, IOException {
try{
System.out.println("GO !");
return null;
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void succeeded() {
super.succeeded();
System.out.println("SUCCEEDED");
}
};
#FXML protected void launch(ActionEvent event){
new Thread(task).start();
}
When I click a first time the button who start my thread, my task run without any problem (my console display "GO !" and "SUCCEEDED").
But if I click a second time, nothing append. Am I doing something wrong ? Can't we use a thread more than once ?
From the Thread.start() documentation : No
It is never legal to start a thread more than once. In particular, a
thread may not be restarted once it has completed execution.
From the Concurrency in JavaFX tutorial :
The Task class defines a one-time object that cannot be reused. If you
need a reusable Worker object, use the Service class.
So, you have to consider the Service class rather than Task.
Edit: this should work for you:
Service service = new Service<>(task);
//Updated use this to create a new Service object instead
Service service = new Service() {
#Override
protected Task createTask() {
return new Task() {
#Override
protected Void call() throws Exception {
//Your codes here
return null;
}
};
}
};
#FXML protected void launch(ActionEvent event){
if (!service.isRunning()) {
service.reset();
service.start();
}
}
With a button can fire new tasks
Button btn = new Button();
btn.setText("New task");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Executor ex=new Executor("Task"+count);
ex.start();
count++;
System.out.println("Task Starting...");
}
});
Do it with a wraper class
import java.io.IOException;
import javafx.concurrent.Task;
public class Executor {
private String name;
private Task<Void> task;
public Executor(final String name) {
this.name=name;
task = new Task<Void>() {
#Override
public Void call() throws IOException, InterruptedException {
try {
int i=0;
while(i<20){
System.out.println(name);
Thread.sleep(2000);
i++;
}
return null;
} catch (IllegalThreadStateException e) {
System.out.println(e);
}
return null;
}
#Override
protected void succeeded() {
super.succeeded();
try {
System.out.println(name+" finish");
} catch (Exception ex) {
System.out.println(ex);
}
}
};
}
public void start() {
try {
Thread th = new Thread(task);
th.start();
} catch (Exception ex) {
System.out.println(ex);
}
}
}

Categories