wait & notify?

Hi,

i have a thread class that listens for incoming socket connections and registering them in an array via another class. BUT, my code is really hogging up cpu and i'm not sure how to make the code more efficient. any tips is greatly appreciated.

public class Acceptor extends Thread

public Acceptor(TestSocket cSocks)

{

testSocket = cSocks;

}

public void run()

{

String clientAddress = null;

Socket s1 = null;

ServerSocket ss= null;

try

{

ss = new ServerSocket(port#)

while (true)

{

s1 = ss.accept();

testSocket.addSocket(s1);

}

}

}

[675 byte] By [mbongcojr] at [2007-9-26 1:13:25]
# 1

The infinite loop there is what's causing most of your problems. Why don't you try a design like this?

public class Accepter

{

public static void main(String args[])

{

ServerSocket ss = new ServerSocket();

while(true)

{

new AccepterThread(ss.accept()).start();

}

}

}

class AccepterThread extends Thread

{

private Socket client;

public AccepterThread(Socket c)

{

client = c;

}

public void run()

{

testSocket.addSocket(client);

client.close();

}

}

Accepter class just sits there listening in on a port (forever, until it recieves a KILL signal), when a connection is made, it spawns a thread and passes that socket to it. That thread involkes your other class and then dies. I hope that is what you were looking for.

Abe.

atehrani_java at 2007-6-29 0:09:34 > top of Java-index,Archived Forums,Java Programming...
# 2
how would u implement your sample with the wait/notify methods?
mbongcojr at 2007-6-29 0:09:34 > top of Java-index,Archived Forums,Java Programming...
# 3
I don't see the need to use wait/notify....
atehrani_java at 2007-6-29 0:09:34 > top of Java-index,Archived Forums,Java Programming...

Archived Forums New Topic

Archived Forums Hot Topic