Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to do thread communication in java

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default How to do thread communication in java

    I have 2 threads and I need one variable get tu dhe second thread.
    For example: main Thread have int i=5;
    and i want this variable to second Thread
    How can i do this?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Thread variable

    Depends what exactly you want to do, perhaps yuo could show us your code so we know what you are doing and how we can work with it.

    Chris

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Thread variable

    Yes ok it is udp server
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package msnserver;
     
     
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Lolek
     */
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            vlakno ServerSocket2 = new vlakno();
            ServerSocket2.vlak.start();
              try {
     
                DatagramSocket server = new DatagramSocket(5000);//vytvoreni server soketu ktery naslouchat na portu 5000           
                byte[] poslana = new byte[256];//vytvoreni 256 bytoveho pole pro posilana data
                byte[] prijata = new byte[256];//vytvoreni 256 bytoveho pole pro prijata data             
                byte[] lolo = new byte[256];  
                //BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in));
                //String klavesnice;
                //byte ip[] = { Network.ip(127), Network.ip(0), Network.ip(0), Network.ip(1)};
                //String ukonceni = "/q";
                //String kOdeslani;           
                while (true)                
                {
                DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length);// zaobaleni pole bajtu k prijmu
                server.receive(prijatyPaket);//prijmuti paketu              
                int port1 = prijatyPaket.getPort();//Ziskani portu klienta a prirazeni do port
                InetAddress IPAdresa1 = prijatyPaket.getAddress();
                System.out.println("Port:"+ port1 +"Adresa:"+IPAdresa1);
                //InetAddress adresa = InetAddress.getByAddress(ip);           
                String prepos = new String(prijatyPaket.getData()); 
                System.out.println(prepos);
                lolo = prepos.getBytes();    
                DatagramPacket klient1 = new DatagramPacket(lolo,lolo.length,IPAdresa1,port1);
                server.send(klient1);        
                //klavesnice = cteni.readLine();
                //poslana = klavesnice.getBytes();
                //DatagramPacket klv = new DatagramPacket(poslana,poslana.length,IPAdresa1,port);
                //server.send(klv);
                }            
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }    
     
        }
     
    }
    class vlakno implements Runnable
    {
        Thread vlak;
        vlakno()
        {
        vlak = new Thread(this,"Vlakno 2");
        }
       public void run ()
        {   
     
            try 
            {
                DatagramSocket server2 = new DatagramSocket(4000);
                byte[] prijata = new byte[256];
                byte[] odeslana = new byte[256];
                byte[] ip ={Network.ip(192),Network.ip(168),Network.ip(1),Network.ip(101)};
                int port2; 
     
     
                while(true)
                {
                 DatagramPacket prijatyPaket2 = new DatagramPacket(prijata,prijata.length);
                 server2.receive(prijatyPaket2);
                 port2 = prijatyPaket2.getPort();
                 InetAddress IPAdresa2 = prijatyPaket2.getAddress();
                 String prijaty = new String(prijatyPaket2.getData());
                 odeslana = prijaty.getBytes();
                 DatagramPacket odesilanyPaket2 = new DatagramPacket(odeslana,odeslana.length,IPAdresa2,port2);
                 server2.send(prijatyPaket2);
                }            
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(vlakno.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
    }
    And i need change from first thread get IP address and get port, to second thread. Client send datagram to server and server send this datagram to another client.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Thread variable

    If you want to make the variable available outside of the main method then declare it as:

    public static int i=5;
    Just after

    public class Main {

    Then you can access it anywhere. For example,

    int newInt = i;

    I'm not sure if this is what you mean though?!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Thread variable

    No i need when one thread get these getPort and getAddress give to second thread. first client send gatagram to server, server getAddress and getPort and these give to second thread this thread recive datagram for another client and send this datagram first client becouse this thread know address and port.

Similar Threads

  1. Java Variable Conversions
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM
  2. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  3. UDP server sends thread application
    By Koren3 in forum Threads
    Replies: 2
    Last Post: April 20th, 2009, 11:46 AM
  4. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  5. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM