2016-04-19 7 views
0

をので、次のコードでは、私は、オブジェクト「リーダー」と「作家」は私の方法のいくつかによって認識させしようとしていますので、私は後ででそれらを実行することができます同じ時間(私はスレッドを使用するように思っている)。それは基本的に私の質問です、私は "リーダー"と "ライター"オブジェクトを他の方法で認識させる方法がありますか?方法渡っ変数/オブジェクトの使用 - Javaの

package com.Kaelinator; 
 

 
import java.io.BufferedReader; 
 
import java.io.BufferedWriter; 
 
import java.io.IOException; 
 
import java.io.InputStreamReader; 
 
import java.io.OutputStreamWriter; 
 
import java.time.LocalDateTime; 
 
import java.time.temporal.ChronoField; 
 

 
public class ServerManager2 { 
 

 
    static boolean Running = false; 
 
    //static String command = "C:/Users/Kael/Desktop/minecraftserver/RUN.bat"; 
 
    static String command = "C:/Users/Owner/Desktop/rekt/Run.bat"; 
 
    static String shutDown = "C:/Windows/System32/shutdown.exe -r -t 0"; 
 

 

 

 
    public static void main(String[] args) throws IOException { 
 
    runServer(); 
 
    while (Running) { 
 
     chatChecker(); 
 
    } 
 

 
    Commands(); 
 

 
    } 
 

 

 

 

 
    public static void chatChecker() { 
 
    LocalDateTime now = LocalDateTime.now(); 
 
    int hour = now.get(ChronoField.HOUR_OF_DAY); 
 
    int minute = now.get(ChronoField.MINUTE_OF_HOUR); 
 
    int second = now.get(ChronoField.SECOND_OF_MINUTE); 
 

 
    String hourSyntax = Integer.toString(hour); 
 
    String minuteSyntax = Integer.toString(minute); 
 
    String secondSyntax = Integer.toString(second); 
 

 
    String chatChecker = "[" + hourSyntax + ":" + minuteSyntax + ":" + secondSyntax + 
 
     "] [Server thread/INFO]: "; 
 
    System.out.println(chatChecker); 
 
    } 
 

 
    public static void sleepThread(long time) { 
 

 
    try { 
 
     Thread.sleep(time * 1000); 
 
    } catch (InterruptedException e) { 
 
     e.printStackTrace(); 
 
    } 
 

 
    } 
 

 

 
    public static void runServer() throws IOException { 
 
    Process process = Runtime.getRuntime().exec(command); 
 
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 
 
    process.getInputStream(); 
 

 
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
 
    } 
 

 
    public static void readOutput() { 
 
    String line; 
 
    while ((line = reader.readLine()) != null) { 
 
     System.out.println(line); 
 
    } 
 
    } 
 

 
    public static void Commands() throws IOException { 
 

 
    Running = true; 
 

 
    try { 
 

 
     sleepThread(3); 
 
     writer.append("say SERVER STARTED"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(14100); 
 
     writer.append("say restarting in 5 minutes"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(60); 
 
     writer.append("say restarting in 4 minutes"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(60); 
 
     writer.append("say restarting in 3 minutes"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(60); 
 
     writer.append("say restarting in 2 minutes"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(60); 
 
     writer.append("say restarting in 1 minutes"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(30); 
 
     writer.append("say restarting in 30 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(20); 
 
     writer.append("say restarting in 10 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(5); 
 
     writer.append("say restarting in 5 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(1); 
 
     writer.append("say restarting in 4 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(1); 
 
     writer.append("say restarting in 3 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(1); 
 
     writer.append("say restarting in 2 seconds"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     sleepThread(1); 
 
     writer.append("say restarting in 1 second"); 
 
     writer.newLine(); 
 
     writer.flush(); 
 
     writer.append("stop"); 
 
     writer.close(); 
 

 

 
    } catch (IOException e) { 
 
     e.printStackTrace(); 
 
    } 
 
    } 
 
}

+0

それらのグローバル変数作りますか? – brso05

+0

xDどうすればいいですか?それらをメソッドに入れないでください。 brso05 @ @ brso05 – Kaelinator

+0

、*グローバル変数*は何を意味するのでしょうか? – Andrew

答えて

0

これは練習指向推奨オブジェクトではありませんが、あなたが欲しいものを行うための一つの方法は自分のRunningフィールドと同様に、staticフィールドとしてreaderwriterを設定することです。

編集:

次のコードをコンパイルして実行します。 2つの静的フィールドを追加し、メソッドにthrowsが不足しています。

package com.Kaelinator; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.time.LocalDateTime; 
import java.time.temporal.ChronoField; 

public class ServerManager2 { 

    static BufferedWriter writer; 
    static BufferedReader reader; 
    static boolean Running = false; 
    //static String command = "C:/Users/Kael/Desktop/minecraftserver/RUN.bat"; 
    static String command = "C:/Users/Owner/Desktop/rekt/Run.bat"; 
    static String shutDown = "C:/Windows/System32/shutdown.exe -r -t 0"; 



    public static void main(String[] args) throws IOException { 
    runServer(); 
    while (Running) { 
     chatChecker(); 
    } 

    Commands(); 

    } 




    public static void chatChecker() { 
    LocalDateTime now = LocalDateTime.now(); 
    int hour = now.get(ChronoField.HOUR_OF_DAY); 
    int minute = now.get(ChronoField.MINUTE_OF_HOUR); 
    int second = now.get(ChronoField.SECOND_OF_MINUTE); 

    String hourSyntax = Integer.toString(hour); 
    String minuteSyntax = Integer.toString(minute); 
    String secondSyntax = Integer.toString(second); 

    String chatChecker = "[" + hourSyntax + ":" + minuteSyntax + ":" + secondSyntax + 
      "] [Server thread/INFO]: "; 
    System.out.println(chatChecker); 
    } 

    public static void sleepThread(long time) { 

    try { 
     Thread.sleep(time * 1000); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

    } 


    public static void runServer() throws IOException { 
    Process process = Runtime.getRuntime().exec(command); 
    writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 
    process.getInputStream(); 

    reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
    } 

    public static void readOutput() throws IOException { 
    String line; 
    while ((line = reader.readLine()) != null) { 
     System.out.println(line); 
    } 
    } 

    public static void Commands() throws IOException { 

    Running = true; 

    try { 

     sleepThread(3); 
     writer.append("say SERVER STARTED"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(14100); 
     writer.append("say restarting in 5 minutes"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(60); 
     writer.append("say restarting in 4 minutes"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(60); 
     writer.append("say restarting in 3 minutes"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(60); 
     writer.append("say restarting in 2 minutes"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(60); 
     writer.append("say restarting in 1 minutes"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(30); 
     writer.append("say restarting in 30 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(20); 
     writer.append("say restarting in 10 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(5); 
     writer.append("say restarting in 5 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(1); 
     writer.append("say restarting in 4 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(1); 
     writer.append("say restarting in 3 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(1); 
     writer.append("say restarting in 2 seconds"); 
     writer.newLine(); 
     writer.flush(); 
     sleepThread(1); 
     writer.append("say restarting in 1 second"); 
     writer.newLine(); 
     writer.flush(); 
     writer.append("stop"); 
     writer.close(); 


    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

これは私にエラーを投げます:「無効な修飾子を削除する」:/それは単にメソッド内にあるかどうかわかりません。具体的な例なし – Kaelinator

+0

@Kaelinator私はあなたが間違って何をしたかを伝えることはできません。私は答えに完全な例を加えました。このコードはコンパイルされ実行されます。 – GuiSim

+0

ありがとう、これは働いた! @GuiSim – Kaelinator

関連する問題