From db39f985c06b90d54a16f985e8ee3acbc8172f75 Mon Sep 17 00:00:00 2001 From: esoe Date: Tue, 27 Sep 2022 14:26:00 +0300 Subject: [PATCH] multithread-try --- src/main/java/ru/molokoin/App.java | 3 + src/main/java/ru/molokoin/ExchangeServer.java | 69 +++++++++++------- .../java/ru/molokoin/MainFrameController.java | 4 +- target/classes/ru/molokoin/App.class | Bin 2002 -> 2248 bytes .../classes/ru/molokoin/ExchangeServer.class | Bin 4242 -> 4442 bytes .../ru/molokoin/MainFrameController.class | Bin 1558 -> 1528 bytes 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/main/java/ru/molokoin/App.java b/src/main/java/ru/molokoin/App.java index a061a18..3ada3e9 100644 --- a/src/main/java/ru/molokoin/App.java +++ b/src/main/java/ru/molokoin/App.java @@ -15,6 +15,7 @@ public class App extends Application { @Override public void start(Stage stage){ + System.out.printf("%s started... \n", Thread.currentThread().getName()); try { String linkFXML = "/face/mainframe.fxml"; String linkCSS = "/face/styles.css"; @@ -31,7 +32,9 @@ public class App extends Application { } public static void main(String[] args) { + System.out.printf("%s started... \n", Thread.currentThread().getName()); launch(); + } } diff --git a/src/main/java/ru/molokoin/ExchangeServer.java b/src/main/java/ru/molokoin/ExchangeServer.java index 1a8c505..64d33a5 100644 --- a/src/main/java/ru/molokoin/ExchangeServer.java +++ b/src/main/java/ru/molokoin/ExchangeServer.java @@ -9,41 +9,54 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; +/** + * + */ +public class ExchangeServer extends Thread{ + private int port = 8081; + private int num = 0; + public static boolean isActive; + public ServerSocket serverSocket; + public Socket socket; -public class ExchangeServer{ - private static int port = 8081; - private static String link = "localhost"; - private static int num = 0; - ServerSocket serverSocket; - Socket socket; - - - //подготовка компонентов - public void init(){ - + ExchangeServer(){ + isActive = true; } - - //остановка серера - public void stop(){ - try { - serverSocket.close(); - //socket.close(); - } catch (Exception e) { - System.out.println("Остановить сервер не удалось. Ошибка: " + e); - } + ExchangeServer (int port){ + isActive = true; + setPort(port); + } + /** + * @return the port + */ + public int getPort() { + return port; + } + /** + * @param port the port to set + */ + public void setPort(int port) { + this.port = port; } + //осановка сервера + public void finish(){ + isActive = false; + } + //запуск сервера - public void start(){ + @Override + public void run(){ + System.out.printf("%s started... \n", Thread.currentThread().getName()); try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("Server started at port: " + port + " ..."); - - while (true) { + + while (isActive) { // ожидаем подключения Socket socket = serverSocket.accept(); System.out.println("Client connected ..."); num++; - // для подключившегося клиента открываем потоки + // для подключившегося клиента открываем потоки // чтения и записи try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); PrintWriter output = new PrintWriter(socket.getOutputStream())) { @@ -87,17 +100,17 @@ public class ExchangeServer{ } output.flush(); } - catch (IOException e) { e.printStackTrace(); } - - // по окончанию выполнения блока try-with-resources потоки, + + // по окончанию выполнения блока try-with-resources потоки, // а вместе с ними и соединение будут закрыты System.out.println("Client disconnected!"); System.out.println("num: " + num); } } + System.out.printf("%s finished... \n", Thread.currentThread().getName()); } catch (IOException ex) { ex.printStackTrace(); } @@ -106,5 +119,5 @@ public class ExchangeServer{ public static void main(String[] args) { new ExchangeServer().start(); } - + } diff --git a/src/main/java/ru/molokoin/MainFrameController.java b/src/main/java/ru/molokoin/MainFrameController.java index f10b37d..67a5a8e 100644 --- a/src/main/java/ru/molokoin/MainFrameController.java +++ b/src/main/java/ru/molokoin/MainFrameController.java @@ -18,21 +18,19 @@ public class MainFrameController implements Initializable{ @Override public void initialize(URL location, ResourceBundle resources) { - // TODO Auto-generated method stub System.out.println("Инициализация компонентов интерфейса ..."); server = new ExchangeServer(); } @FXML public void buttonClicked(Event e){ - System.out.println("Button clicked"); if (btn.getText().equals("START")){ btn.setText("STOP"); server.start(); }else { btn.setText("START"); - server.stop(); + server.finish(); } } diff --git a/target/classes/ru/molokoin/App.class b/target/classes/ru/molokoin/App.class index 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Bc_P>i=~@2jC0yzV*N#fd4{v=|F_00CKYLYC7PCBb56gC z3@3A32z?kxkYmQ?BA4``=um5pD>hd{LGhsU{?G*1nYC~gsr5Ihq)k3t-C7kvL7`~j z>3h-F>PP*1EFHUPQ^D7N#O~a-h!B%2F`KL#PbMdJSE=7qrTH;!|Ij%Yc|!M0Ch~~g zgVEk(Ulpe}6Rom84Bv!UX*E~*_D_x(TORfm56M0uHtR;C_(l=^Bi;j9Sb!QbQSEm_q&o nl0&(1^C`h&)*eW0Y>G}uh7RG}xv9r~kK-0UNni2vCi0hexTUg2 diff --git a/target/classes/ru/molokoin/ExchangeServer.class b/target/classes/ru/molokoin/ExchangeServer.class index f1b35b0ebbc7718b47c8d2bfdedcbe0ca70131c4..5240a0cf5552997fab0164de60fa63ddcc0db613 100644 GIT binary patch literal 4442 zcma)9Yj{)T9sj+jNlucUrZiknK`qjX5@9Iwl*dY+Ng(j4EXpi@Ld$ZSH>gqF#%6HW!VB^g+NI%+a=(SXBwk+e3!}1O#)iRVQ)!AcbKG`+~kNzns&V_*F9(u z@MZEO6-6Y@mONY#PsZ*0sH1x31}a^WikU%_Agm#T@&E)TXz;=(XOlFj?re$%KLP^f zP4T3;I@{H5TCI`x1d)ZCQqf3aL&S>9b$-)hcg8aU)0#fo3xwjSUNLh+ka}*@P=#9r z)QoAbah#xTE6fO!4)Jvmb*7W3t!y%Y8K_oq$0#qh?9SL`mxh_BAyq0%4^4E)Atd4ZiMWaaxBc9ww(s?jc%oiB%#BnrA4Gma8v(plI zhrojBB4v3An~SzwybXCsqh&RyV7MoanJqyq#3C8_TU0D&PK{)xt7LIDo`{*2ANLDP zanr2ex2lL;B{SBj3ZfCqq>$xQGX_u@y zF*uU9S@NzCm_3&CkCY)f*J)^x(eT?TSAq2VdJP*SSk-ZFA}PJ_poTWE%6yS%)J!vb z#uozYRG3>nemul*E=k0h2vyNkGRY({V-lr`&#_(#kzAbZ=rAoawvL4@3;tmNy>PZ7 zna<{O(LD~L9WfPAX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0!)A$}lWv&K|-Z-KQyeJbP^HKingaeM0vOJurx;`}bt;D6auxL&DOb6osqP9j^;)3^g&uMvey3Zj z9m4lztaXD3(~d^eR%t%joY3kRShrq(b?hISFOPS%x+{1u zyo!Jp(u0Gj*MeH;I7e*q_>o)=YQa3t%RqkYpzl`9u#>3iOq81=&36SerLFhrjqa60 zpEK0FBbsvM`YMUY$-(RQ>eZT8nyc2m(wtYTmgd+l&}Zw>Ub8+Q!Al;cHyXtr|z7Ea>xR;Af?!#Irmwx;P{bv0Wxkh!eQLcSk||m4&)p z{8Rj!?ZckAp1Ev8o>osQ+bU1g6J<*l^Yl>OJqqvg7XtXO3t>JMv%eIL(AjIl-Pn$M zkis&)EtcaslEpr(!~w3pgccm;OZo`z$17NcH~1QO8?87=tuuHKzvi*|8|D6vNAWL2 z@jt#{1=fq{=n%`WQAE)xI?yFH^9{O<{REy6DZVOC@ZIP0VQq;5U^^)lKI z5Tut?_$_-5U5EzEzAk@yopS!?jS(Kd0#Tk&V3hh>72b1nVw!@3{Qoaz@mqThOU95! z_!dg8eCd0%V!wmHH3YfwscWcdV^do@Ms5p!{LQ4R7plit3Ac1x21FB{sAJ-bgFyrMyeX9HM&&f5_v*@rxng(DP2_E?IFo R)L;~Uq>5k`<+b(a{{s{!A;ACu diff --git a/target/classes/ru/molokoin/MainFrameController.class b/target/classes/ru/molokoin/MainFrameController.class index 7cf00c0332aed7eba1214d11c1b4723f85ff3d37..666da0b8f8a842826b5ba7f5b7f7364014fd997e 100644 GIT binary patch delta 335 zcmXYtyG{aO5QV>=1sB#$AmD-tUcrb;K_V*N5%1y!WcLNOK*X3WU}LnhwlpLrK7lpS zY++$%;}cmKf6$y{X3m++oXkAw--FuM&-({Z!0c+%iV;GvpraDhSYlbFAV@ws$$-uZ ztDQR~J-aRlm{zG~X>78kQWQk4>>E3GZ99#e>9(3qL#M=c=Tk|Sb_IUZs#JUH_H_=( z2-J4nwd%L7#tcU~Wg>EA+qGMLQ0HP`rP@Cjt4n zH}W2f(KU~aXW~5?-SOCyMb_i+g;-yb?SJXGE5{UuWE_)MLXt2e3^U5OM2ZQrOft_D fhU~`wY-CPICJi~Gf+46%;>VW?%YU8A+DGjdkU}@? delta 365 zcmXYt+b#nE5Qe|)ZnL#ENJ}f~P#Ue)g$svLhf?QbtGgbcVIxIa(IxIYgESe