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?
Printable View
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?
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
Yes ok it is udp serverAnd 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.Code :/* * 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); } } }
If you want to make the variable available outside of the main method then declare it as:
Just afterCode :public static int i=5;
Code :public class Main {
Then you can access it anywhere. For example,
Code :int newInt = i;
I'm not sure if this is what you mean though?!
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.