I have two streams (main and one side). When you click on the button, the side stream is executed, but when you press the error again. Below is the code. It is necessary that you can click on the button to wait until the thread is executed and click again and the stream is executed 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(); } } } }