8 Replies Latest reply on Nov 30, 2007 5:49 PM by 807603

    Global Variables and Threads

    807603
      Hello,

      I have a GUI and a "client server " pair. I'm running the GUI and the client on a thread that way I can run them at the same time. Here is the problem.

      * I have a global variable that works fine within the GUI portion of the thread, but it is not recognized inside the client.

      * I'm not really an expert on threads I'm not sure how to pass a string I'm trying to pass from the GUI to the client. any suggestions? or maybe a link I can read?

      Thank you.
        • 1. Re: Global Variables and Threads
          807603
          Well robertplant22,

          you could create an implementation of the Runnable class and pass any parameters you need t it.

          If you need more help show us your code.

          MeTitus
          • 2. Re: Global Variables and Threads
            807603
            Here is the code for the global variable :
             package gui;
            
            public class Global {
               
                public static String c = "simon";    
            Here is the code for the GUI. I'm tyring to pass a string to the server using the jBUpActionPerfomerd function. I'm only pasting the part where it interacts with the global variable because the code is quite long.

            package gui;    //package gui includes all other files
            import java.lang.String;
            
            public class ManualMode extends javax.swing.JFrame {
                
                public ManualMode() {
                    initComponents();
                }
             private void jBUpActionPerformed(java.awt.event.ActionEvent evt) {                                     
                    String moveUpString = "u";
                   
                    //c.setString(moveUpString);
                    //Global.c="simon";
                    
                  System.out.println(Global.c);
                }                                    
             public static void main(String args[]) {
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            new ManualMode().setVisible(true);
                        }
                    });
                }
            The code for the client is below, I would like to place the string in the part that has the line System.out.println("Client: " + fromUser );
            import java.io.*;
            import java.net.*;
            
            public class KnockKnockClient implements Runnable {
                Socket kkSocket;
            
                
               
                   public KnockKnockClient (){
                       kkSocket = null;
                   }
                   
            public void run(){
                
                PrintWriter out = null;
                BufferedReader  in = null;
                
                    try {
                        kkSocket = new Socket("NANO_BEHAL.RESEARCH.UCF.EDU", 4444);
                        out = new PrintWriter(kkSocket.getOutputStream(), true);
                        in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
                    } catch (UnknownHostException e) {
                        System.err.println("Don't know about host: taranis.");
                        System.exit(1);
                    } catch (IOException e) {
                        System.err.println("Couldn't get I/O for the connection to: taranis.");
                        System.exit(1);
                    }
            
                    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                    String fromServer;
                    String fromUser;
                    try {
            
                        while ((fromServer = in.readLine()) != null) {
                            System.out.println("Server: " + fromServer);
                            if (fromServer.equals("Bye."))
                                break;
                          
                            fromUser = stdIn.readLine();
                     if (fromUser != null) {
                                System.out.println("Client: " + fromUser );
                                out.println(fromUser);
                           
                     }
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
            
                    out.close();
                    try {
                        in.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        stdIn.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        kkSocket.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            • 3. Re: Global Variables and Threads
              807603
              What, exactly, do you mean by "Thread", because, because code doesn't "belong" to any particular thread and so access to variables has nothing to do with threading.

              It sounds like you're talking not about threads but about separate JVMs.
              • 4. Re: Global Variables and Threads
                807603
                By thread I mean that the GUI and server are running on a "thread". This is a GUI and the code is very extensive and has about 8 other classes; therefore I did not post all the code because it is not necessary. As for the code not being part of a thread; yes it is part of a thread. The class ManualMode is called from another class MainMenu and that is where the "Thread" is located.
                • 5. Re: Global Variables and Threads
                  807603
                  Is the problem just accessing a class in another package? (nothing to do with threads).

                  You need only put gui.Globals.fieldName (I forgot what you called the field). Or put an import for for gui.Globals at the head of the class.
                  • 6. Re: Global Variables and Threads
                    807603
                    Perhaps it's just a matter of accesing a field in another package in which case it's just

                    gui.Globals.c

                    Or you could put

                    import gui.Globals at the head of your file
                    • 7. Re: Global Variables and Threads
                      807603
                      Perhaps it's just a matter of accesing a field in another package in which case it's just

                      gui.Globals.c

                      Or you could put

                      import gui.Globals at the head of your file
                      • 8. Re: Global Variables and Threads
                        807603
                        What you guys are saying makes sense. I already did that because everything is inside a "package gui" The only place where is not recognized is in the "client"