Results 1 to 3 of 3
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default First Venture into Multithreading

    Hello everyone I am having a little trouble with my first program using multi-threading I need to create a server and a client.The client uses a gui to send loan information to server. The server computes this information and sends back the results to the client.I have the networking working correctly but the gui does not show when the program is run.Like I said this is the first time I have used multi-threading I am sure this program is full of errors if some could just take a look and let me know what is keeping the gui from displaying.Thank you.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/29/12
     * Time: 8:38 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Server extends JFrame {
        private JPanel _panel = new JPanel();
        private JTextArea _textArea = new JTextArea();
    
        int portNum =8000;
    
        public Server()
        {
            JScrollPane _scrollPane = new JScrollPane(_textArea);
            this.setLayout(new BorderLayout());
            this.add(_panel, BorderLayout.CENTER);
            _panel.setLayout(new BorderLayout());
            _panel.add(_scrollPane, BorderLayout.CENTER);
    
            try{
                ServerSocket socket1 = new ServerSocket(portNum);
                while(true)
                {
                    Socket  socketListen = socket1.accept();
                    InetAddress address = socketListen.getInetAddress();
                    System.out.println("Connected to " + address.getHostAddress() + " (" + address.getHostName() + ")");
                    Task task1 = new Task(socketListen);
                    Thread thread1 = new Thread(task1);
                    thread1.start();
                }
            }
            catch (Exception ex){
    
            }
    
        }
    }
    import java.io.*;
    import java.net.Socket;
    import java.util.ArrayList;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/29/12
     * Time: 8:39 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Task  implements Runnable {
        Socket s;
        private static ArrayList<Socket> _clients = new ArrayList<Socket>();
        Account account = new Account();
    
        public Task(Socket s)
        {
            this.s = s;
            _clients.add(s);
    
        }
        @Override
        public void run()
        {
            try{
                DataInputStream dataInputStream = new DataInputStream(s.getInputStream());
    
                while(true)
                {
                    String message = dataInputStream.readUTF();
                    for(int i =0; i < _clients.size(); i++)
                    {
                        Socket c1 = _clients.get(i);
    
                            DataOutputStream dataOutputStream = new DataOutputStream(s.getOutputStream());
    
    
                            account.setIrate(dataInputStream.readDouble());
                            account.setYears(dataInputStream.readInt());
                            account.setAmount(dataInputStream.readInt());
                            System.out.println(account.getIrate() + " " + account.getYears() + " " + account.getAmount());
    
                            System.out.println("Data Received");
    
                    }
                }
            }
            catch(Exception ex){
    
            }
        }
    }
    import java.io.Serializable;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/29/12
     * Time: 8:36 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Account implements Serializable {
        private  int  _years;
        private  double _amount;
        private  double _iRate;
        private  double _monPay;
        private double _totalPay;
    
        public int getYears()
        {
            return _years;
        }
        public void setYears(int years)
        {
            this._years = years;
        }
        public double getAmount()
        {
            return _amount;
        }
        public void setAmount(double amount)
        {
            this._amount = amount;
        }
        public double getIrate()
        {
            return _iRate;
        }
        public void setIrate(double irate)
        {
            this._iRate = irate;
        }
        public double getMonthlyPayments()
        {
            _monPay = (_amount * _iRate / 1200) / ( 1 - Math.pow( 1 / ( 1 + _iRate / 1200), 12 * _years)) ;
            return _monPay;
        }
        public double getTotalPayment()
        {
            _totalPay = _monPay * 12 * _years;
            _totalPay = (int)(_totalPay * 100 + 0.5 ) / 100.0;
            return _totalPay;
        }
    
    }
    import javax.swing.*;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/29/12
     * Time: 8:37 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Application {
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    Server frame = new Server();
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);
                    frame.setTitle("Server");
                    frame.setSize(500,300);
                }
            });
        }
    
    }
    Last edited by aortell24; 08-31-2012 at 10:43 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default Re: First Venture into Multithreading

    what is keeping the gui from displaying
    Does the Server's constructor return so that the setVisible() method is called?

    After the constructor builds the GUI, it should create and start a thread to do its work and then return to the call to setVisible(), leaving the thread running.

    Also be sure to add calls to the printStackTrace() method to all of the catch blocks.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: First Venture into Multithreading

    Thank you very much.

Similar Threads

  1. Multithreading Gui
    By BUGSIE91 in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-13-2010, 02:20 PM
  2. Want to know about Multithreading.
    By Chetans in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2010, 07:50 AM
  3. Log 4j Multithreading
    By joe2010 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 03:48 AM
  4. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •