Flow, JavaFX and processor



  • I have two flows. When applied to the button, the lateral flow shall be carried out but the error is repeated. The code is below. It must be possible to press the button to wait until the flow is met and pressed again and the flow is met again.

     package application;
     import javax.sound.midi.MidiChannel;
     import javax.sound.midi.MidiSystem;
     import javax.sound.midi.Synthesizer;
     import javafx.application.Application;
     import javafx.scene.Scene;
     import javafx.scene.control.Button;
     import javafx.scene.layout.AnchorPane;
     import javafx.stage.Stage;
     public class Main extends Application {
         static AffableThread mSecondThread; //побочный поток
         public void start(Stage stage) {
             AnchorPane r1 = new AnchorPane(); //создание pane
             Scene sc1 = new Scene(r1, 600, 400); //создание сцены
             mSecondThread = new AffableThread(); //Создание потока
             Button b1 = new Button(); //та самая кнопка
             b1.setLayoutX(200);
             b1.setLayoutY(100);
             b1.setPrefSize(100, 50);
             r1.getChildren().add(b1);
             b1.setOnAction(b - > { //ActionListener
                 mSecondThread.start(); //начало потока
             });
             stage.setScene(sc1);
             stage.show();
         }
         public static void main(String[] args) {
             launch(args);
             System.out.println("Главный поток завершён...");
         }
         class AffableThread extends Thread //вложенный класс отвечающий за то что происходит в побочном потоке
         {@
             Override
             public void run() //Этот метод будет выполнен в побочном потоке
             {
                 System.out.println("Привет из побочного потока!");
                 System.out.println("Привет из побочного потока!");
                 try {
                     Synthesizer s1 = MidiSystem.getSynthesizer();
                     s1.open();
                     MidiChannel[] channels = s1.getChannels();
                     channels[0].programChange(41);
                     channels[0].noteOn(36, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(36);
                     channels[0].noteOn(38, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(38);
                     channels[0].noteOn(40, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(40);
                     channels[0].noteOn(41, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(41);
                     channels[0].noteOn(43, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(43);
                     channels[0].noteOn(45, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(45);
                     channels[0].noteOn(47, 80);
                     Thread.sleep(1000);
                     channels[0].noteOff(47);
                     s1.close();
                 } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
         }
     }
    


  • If it's simple, you can create a flow every time you press the buttons:

    b1.setOnAction(b -> { //ActionListener
        if ( mSecondThread == null || !mSecondThread.isAlive() ) {
            mSecondThread = new AffableThread(); //Создание потока
            mSecondThread.start(); //начало потока
        }
    });
    

    You could say Yura Ivanov:

    import javax.sound.midi.MidiChannel;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Synthesizer;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    

    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledFuture;
    import java.util.concurrent.TimeUnit;

    public class Main extends Application {

    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    
    private ScheduledFuture future = null;
    
    public void start(Stage stage) {
        AnchorPane r1 = new AnchorPane();
        Scene sc1 = new Scene(r1, 600, 400);
        Button b1 = new Button();
        b1.setLayoutX(200);
        b1.setLayoutY(100);
        b1.setPrefSize(100, 50);
        r1.getChildren().add(b1);
        b1.setOnAction(b -> {
        if ( future == null || future.isDone() ) {
            try {
                future = executor.schedule( runnable, 0, TimeUnit.MILLISECONDS );
            } catch( Exception e ) {
                e.printStackTrace();
            }
        });
        stage.setScene(sc1);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
        System.out.println("Главный поток завершён...");
    }
    
    private Runnable runnable = () -> {
        System.out.println("Привет из побочного потока!");
        System.out.println("Привет из побочного потока!");
        try {
            Synthesizer s1 = MidiSystem.getSynthesizer();
            s1.open();
            MidiChannel[] channels = s1.getChannels();
            channels[0].programChange(41);
            channels[0].noteOn(36, 80);
            Thread.sleep(1000);
            channels[0].noteOff(36);
            channels[0].noteOn(38, 80);
            Thread.sleep(1000);
            channels[0].noteOff(38);
            channels[0].noteOn(40, 80);
            Thread.sleep(1000);
            channels[0].noteOff(40);
            channels[0].noteOn(41, 80);
            Thread.sleep(1000);
            channels[0].noteOff(41);
            channels[0].noteOn(43, 80);
            Thread.sleep(1000);
            channels[0].noteOff(43);
            channels[0].noteOn(45, 80);
            Thread.sleep(1000);
            channels[0].noteOff(45);
            channels[0].noteOn(47, 80);
            Thread.sleep(1000);
            channels[0].noteOff(47);
            s1.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };
    

    }

    It's not perfect, but the direction is correct.
    It is also possible to look towards javafx.concurrent, although this will be unnecessary.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2