Monday, December 03, 2007

Horizontal Thoughts

I'm writing tonight to see how much I can think to tell my audience as I lay in bed with a wireless keyboard and gyroscopic mouse, staring at my beautiful TV/monitor. Looks like I got it all worked out, but I can barely read the screen from across the room and there's a significant delay between the keystroke and the appearance of the letter on-screen.
I'm really just up late because I can't figure out Java multithreading (or multithreading in general) enough to complete a project for school due Monday at midnight CST. So if anyone knows how to write a multithreaded chat server in Java, please contact me soon. I have several questions for you, including these:

1. How can more than one client connect on the same port number?
Never mind. I just figured that one out. The server can listen on a port and has exclusive rights to that port. The number of connections on that port matters only to the program listening.

2. How do I create a thread pool with an ArrayList and then pass a Runnable object to it? I don't see a method to pass the object after instantiation.
Update: I got that one figured out, too. You don't pass a Runnable object - just extend the Thread class and fill your ArrayList with those. Worked for me. To stop the thread, set it to null. To start it again, pass it what it needs and then call .start().

If you want to see that code, here is the Client, the Server, and the ServerThread. It's not pretty and there are some errors when you close a Client, but the multithreading works.

I realize these are not questions for my typical audience, but hey - what else should I do in bed at this hour?

No comments: