Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Acess to Threaded Class  (Read 540 times)
0 Members and 1 Guest are viewing this topic.
Offline 50Cent

JGO n00b
*

Posts: 1



« on: 2010-08-23 19:58:26 »

I have the following code:

Server.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Server
{
   public Thread InNetwork[] = new Thread[10];
   
   public static void main(String Args[])
   {
      new Server();
   }
   public Server()
   {
      InNetwork[0] = new Thread(new InNetwork());
   }
}


InNetwork.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class InNetwork implements Runnable
{
   public int iClientH = -1;
   public InNetwork()
   {
     
   }
   public void run()
   {
      while(true)
      {
         
      }
   }
}


how can i access for example to variable iClientH from Server class ?
Offline teletubo
« League of Dukes »

Sr. Member
*****

Posts: 463
Medals: 17



« Reply #1 on: 2010-08-23 20:17:38 »

Wow what a mess you've got there, pal . Why are you naming your Thread array with the same name of you InNetwork class ??

1  
   public Thread InNetwork[] = new Thread[10];  // ???  


To answer your question I'll change the name of this array to 'threads'.
To access the variables you have to keep a reference from the instance you passed to the Thread:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class Server
{
   public Thread threads[] = new Thread[10];
   
   public static void main(String Args[])
   {
      new Server();
   }
   public Server()
   {
                InNetwork in = new InNetwork();
      threads[0] = new Thread(in);
                in.iClientH = 12345;
   }
}


Other than that, I suggest you looking into java basics first, before writing Client/Server applications .

Offline Eli Delventhal
« League of Dukes »

JGO Kernel
*****

Posts: 3573
Medals: 44


Game Engineer


« Reply #2 on: 2010-08-24 11:07:42 »

Yeeeaaaaah....

Definitely definitely do not work with multithreading and client/server stuff until you know how to write an accessor method... Man, you really need to start from the beginning. Ground zero. Write Hello World.

See my work:
OTC Software
<br />
Currently Working On:
Secret project...
Quote from: _Riven
I edit JGO in production, because I simply don't waste time writing bugs
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.06 seconds with 20 queries.