Can I do this by creating multiple threads?
What is a really simple way, a program does more than one at a time, even if a computer doesn’t necessarily have several “cores”. Can I do this by creating a few theme?
If you have 1 single-core processor, then only the “official” only one task can be completed. Because your computer s processor is so fast and executes so many instructions per second, it creates the illusion that your computer performs several tasks at the same time, while each small unit performs only one task. You can create this illusion in Java by creating threads that are planned by your operating system in a short period of time.
My advice is to take a look at the java.util.concurrent package because it contains many useful tools for playing with streams much easier (in those days when this package was not there, it was much more complicated). For example, I like to use
ExecutorService es = Executors.newCachedThreadPool();
to create a thread pool that I can submit jobs at once. Then, when I have a task that I like to run, I call
es.execute(runnable);
where runnable is as follows:
Runnable runnable = new Runnable() { public void run() {
For example, let's say you run the following code:
package mytests; import java.util.Date; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String[] args) throws Exception {
The result will look like
run: Fri Apr 02 03:34:14 CEST 2010 Fri Apr 02 03:34:14 CEST 2010 total time: 1055 BUILD SUCCESSFUL (total time: 1 second)
But I had 2 tasks, each of which completed at least 1 second (due to sleep 1 second). If I completed these 2 tasks sequentially, it would take at least 2 seconds, but since I used threads, it took only 1 second. This is what you wanted and it is easy to accomplish with the java.util.concurrent package.
I want the server to potentially handle more than one client at a time.
My goal is to have two computers connected to the network (via Sockets) respond to each other's requests, while my program will at the same time be able to control the user interface. I want a server that can potentially handle more than one client at a time well.
I would advise you to take a look at the Netty framework (MINA, which is also developed by the creator of MINA, but Netty is better (more development), in my opinion) .:
The Netty project is an attempt to provide asynchronous event management network application infrastructure and tools for rapid development supported by high performance and high scalability protocol servers and amp; customers.
He will do all the hard work for you. When I read the user manual, I was completely amazed at netty. Netty uses nio , which for highly competitive servers is a new way to do IOs that scale much better. As I said before this frame does all the heavy lifting for you
My problem is that I want the server to wait for several readLine () requests and also do other things
I understand that communication is done using BufferedReader.readLine () and PrintWriter.println (). My problem is that I want the server to wait on several readLine () requests and also do other things. How do I handle this?
Again, when you look at the netty + user manual examples, you will find out that it does all the heavy lifting for you in an efficient way. You will only need to specify some callbacks to receive data from clients.
Hope this answered all your questions. Otherwise, I would advise you to leave a comment so that I try to explain it better.