Solved

Java GUI and MultiThreading

Posted on 2008-06-16
3
356 Views
Last Modified: 2013-11-23
Hello,

I feel really stupid at the mo but I havent done any Java in about 3 years and that was only in UNI.

So my problem is that at the moment I have an application with a FrameView GUI that I have created in NetBeans IDE 6.

On startup the program reads a text file in and based on the amount of seperate sensors are in the list it creates a new UDP Thread for each sensor. Inside each thread a UDP Client is created that access the port.

So the part I am stuck on is gettign it back to the screen or even into the main class. I dont want to create a new instance of the GUI in order to do this and am not sure where to go from here.

So I call a StartUDPReceive at start up.

In this method is this code :
public void startUDPReceiver() throws FileNotFoundException, IOException {
    String line = null;
       BufferedReader config = new BufferedReader(new FileReader("C:\\portSettings.ini"));
         
    int i = 0;
    while (( line = config.readLine()) != null){
          String threadName =line.substring(0,line.indexOf(","));
          int threadPort = Integer.parseInt(line.substring(line.indexOf(",")+1,(line.length())));
          new UDPThread(threadPort,threadName).start();
          i++;
    }
    System.out.println("Port Listeners" + i);
}
So for each line in my file it gets the Port Number and Name and makes a UDP Thread for each one.
Here I have a piece of code called decomposeXML which simply splits it into words and then I send the data out to seperate Interp scripts depending on the name of the message. These Interp Scripts are what I am having trouble with at the moment. I cannot get them to send the data back to the GUI Class as It is a not instantiated.

public class UDPThread extends Thread {
    private int Port;
    public UDPThread(int Port, String name) {
        super(name);
        this.Port = Port;
    }
    public void run() {
        UDPClient threadedClient = new UDPClient(Port);
       
        try {
            while (true) {
                decomposeXML(threadedClient.runClient());
             }
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}


public class UDPClient {
    private int portID;
    public UDPClient(int newPortID) {
        this.portID = newPortID;
    }
    public int getPortID()
    {
        return this.portID;
    }
   
    public String runClient() throws Exception {
        DatagramSocket socket = new DatagramSocket(this.getPortID());
       
        byte[] buf = new byte[256];
       
        DatagramPacket pack = new DatagramPacket(buf,buf.length,InetAddress.getByName("139.166.238.255"),this.getPortID());
 
        socket.receive(pack);      
        socket.close();
        return (new String(pack.getData(),0,pack.getLength()));
    }
    }

Any Ideas appreciated.

Im sure I had this working in the office before but Im now at sea on a research cruise for a month and am stuck for getting into my computer from here.

Cheers
0
Comment
Question by:02003163
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 125 total points
ID: 21793754
You could simply pass the Main class to your UDPThread and do a callback from there
new UDPThread(this, threadPort,threadName).start();
 
 
 
 
public class UDPThread extends Thread {
    private int Port;
    private MainClass main;
    public UDPThread(MainClass main, int Port, String name) {
        super(name);
        this.main = main;
        this.Port = Port;
    }
    public void run() {
        UDPClient threadedClient = new UDPClient(Port);
       
        try {
            while (true) {
                decomposeXML(threadedClient.runClient());
 
 
                mainClass.callBack(...);
 
 
             }
        }
        catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21794756
Can you post the code for decompose XML?
0
 

Author Closing Comment

by:02003163
ID: 31467596
Thanks for the help with that one. Worked straight away. I am only just looking at java again as a prototype for something at work so its good to be able to ask questions here. Thanks again for the help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question