简体   繁体   中英

How do I get an identical value from two threads calling on the same variable?

I have a program that starts by creating a GUI to handle user input and display the output.

The first thing that happens is the window is created and then the Functions Class method initServer() is called to initialize some variables for the input and output portion

private JFrame frame;

public static Functions func = new Functions();

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            Thread.currentThread().setName("Console");
            System.out.println(Thread.currentThread().getName() + " [" + Thread.currentThread().getId() + "] Started");

            try {
                Console window = new Console();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }

            func.initServer();
        }

    });
}

With the Functions Class method initServer() being called, We start the process then follow by initializing the i/o variable that will handle all the streams being used to communicate with the process. Then we start the two threads - ConsoleInputWriter and ConsoleOutputReader - responsible for handling Input and Output to the process.

public class Functions {

    private ConsoleOutputReader cor = new ConsoleOutputReader();
    private ConsoleInputWriter ciw = new ConsoleInputWriter();

    private OutputStreamWriter osw;
    private InputStreamReader isr;

    private BufferedWriter bw;
    private BufferedReader br;

    private BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    private File serverJar;

    private String serverPath;

    private ProcessBuilder builder;
    private Process proc;

    private boolean init = false;

    public void initServer() 
    {
        updateConsole("Server Initiated Status: " + serverStatus());

        builder = new ProcessBuilder("/bin/bash");

        try {
            proc = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

        osw = new OutputStreamWriter(proc.getOutputStream());
        bw = new BufferedWriter(osw);

        isr = new InputStreamReader(proc.getInputStream());
        br = new BufferedReader(isr);

        serverStatus(true);
        updateConsole("Server Initiated Status: " + serverStatus());

        cor.start();
        ciw.start();
    }

    public String recieveInput() 
    {
        String s = null;
        try {
            s = input.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    public boolean serverStatus() 
    {
        return init;
    }

    public void serverStatus(boolean status) 
    {
        init = status;
    }

    public void exec(String cmd)
    {
        try {
            bw.write(cmd);
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            updateConsole("Cant run: [" + cmd + "] :::::::: " + e);
            e.printStackTrace();
        }
    }

    public void updateConsole()
    {
        //edit to print to textPane
        try {
            System.out.println(br.readLine());
        } catch (IOException e) {

        }
    }

    public void updateConsole(String s)
    {
        System.out.println(s);
    }

    public File getJar(/**String s**/) 
    {
        serverJar = new File(Functions.class.
                getResource("CraftBukkit.jar").getPath());
        return serverJar;
    }

    public void setPath(String s) 
    {
        serverPath = s;
    }


    public String getPath() 
    {
        return serverPath;
    }

}

Once called the Class ConsoleOutputReader starts and executes a command for the process to start a Jar File and confirms that the i/o streams have been initialized before it tries to get any output. If it continues to the while loop we should be getting output.

public class ConsoleOutputReader extends Thread{

    private static Functions func = new Functions();

    public void run()
    {
        currentThread().setName("cor");
        System.out.println(currentThread().getName() + " [" + Thread.currentThread().getId() + "] Started");

        func.exec("cd " + "~/Desktop/Bukkit" + " && java -Xmx1024M -jar " + func.getJar() + " -o true");

        while(func.serverStatus())
            func.updateConsole();
    }


}

and the Class ConsoleInputWriter follows right after ConsoleOutputReader also confirming that the serverInit() boolean is true, then to wait in a while loop for an input from the user.

public class ConsoleInputWriter extends Thread{

    public static Functions func = new Functions();

    public void run()
    {
        currentThread().setName("ciw");
        func.updateConsole(currentThread().getName() + " [" + Thread.currentThread().getId() + "] Started");

        while(func.serverStatus()) 
            func.exec(func.recieveInput());

    }
}

The main issue I have is that with minimal knowledge on threads I seemed to have made the serverStatus() boolean from the Functions class equal two different things. where the output from ConsoleOutputReader is true and the output from ConsoleInputWriter is false. How would I make sure that when I start both threads they're seeing the same value when they call the method?

I've gotten this code to work with two threads where the main thread ran the inputs and a second thread was used to run the outputs, but I wanted to try it setup like this.

Any tips to my style and or patterns I use are also very welcome.

Edit: I realized with all my frantic changes that whatever class calls initServer() is that class that get true when they call serverStatus().

Any methods that change an object for both threads need to be synchronized. When the method runs, if the object is being read by one or both of the threads while it changes, The threads could read different values.

ex:

public static synchronized void initServer(boolean bool) { init = bool; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM