Java Chat Multi-Client Receiving thread - java

In a lot of multclient java programs people use a separate thread which only receives the messages from the server. Is it really necessary? Why can't it be done in the main thread? What should be the problem?
For me a separate thread to receive the messages from the server is not that necessary, it could be done simply be the main.
Am I wrong?

No, if it was done in the main method, then the rest of your application would wait until a message is received. This may be fine if your application only received messages. However, if your application did other things like accepting user input in order to send messages then, the application would not be able to do the other tasks(such as user Input) until a message has been received.
EDIT:
It would also have a hard time with multiple clients

Yes you are. At least if you want something pratical.
If you get updates periodical, you need to wait every x ms/s/mins for the server input to be read, if you receive message from the server directly you need to wait everytime someone sends one, and could not even handle multiple messages at once.
Now add a Graphical Interface and youll run into a wall trying to handle that by 1 thread.
Not that it would make sense anyway.

After 6 years I would like to answer myself of the past.
Some more context: the multi-client chat is a GUI chat that used swing. It was a high school project.
The main method would go in a loop to read messages received from a server. Since every other client action is event-based, the event handling would be done on a separate thread anyway.
So yeah, it worked fabulously at the time!

Related

View the console output of a running Java program, from another Java app

I have a program made in Java, which you want to configure as an operating system service. The program reads data received by socket, which processes and stores it. It also logs errors and finally prints the information flow all the time in console to observe its operation.
After setting it up as a service, a need arose.
How do I view the print activity in console, if the application is in the background and therefore the console is not open?
Is there a way to view the console output of that application?
It occurred to me, if it is possible to create another Java application to read and print the console output of the created service. Would that solve the problem?
Thank you very much for your comments, after researching a little more thoroughly, according to your suggestions, i developed an IPC communication system via Sockets, in which, an swing application connects to the service and requests Every 2 seconds the required information and the sample in the app

Java Web/JSP program with code running in background 24/7

What is the best way to implement a notification system that keeps running in the background?
This system will generate these notifications when certain conditions are met. The server has to monitor the database constantly for these events and update a notification log when they occur. When a notification is generated, an SMS is sent to designated people.
With a traditional JSP system, the code only runs when a user visits the site. How do you program the server to have the code running 24/7? Also, how do you feed back these events asynchronously to the front end so the front end does not need to keep polling the database?
The question is very vague, but i'd say you need a task scheduler, some application servers like wildfly already have a task scheduler, or you can use something like quartz, and set it to run every as many times as you need.

How to manage queue like system over multiple user and start/stop instance?

(I'm sorry for the title, it was the best I could come up with)
I have a PlayFramework (2.3) app where my users can upload large csv files that will be processed.
Once the CSV is imported, I run a task that will pass over each new entry, and check a specific data with a request to an external API (for each entry).
Since this takes a long time, I do the checking asynchronously, but I'm facing a structural issue here :
User A upload a file with 100k lines
I add those 100k lines to my async code and start it
User B upload a file with 200k lines
I would add those new lines to the current async code
I stop the app (updating the code)
When restarting, it should start where it stopped.
I thought about a Queue system, but I would loose the interest when starting the app.
Any idea on how I can do this ?
Thank you for your help.
Since the question is structural, I am only going to focus on the high level implementation details :
I stop the app (updating the code)
When you decide to stop the application, you need a way to gradually stop all running threads. For this, every thread that you start in your application should be registered with some sort of a thread manager. This way, when you decide to stop the application by clicking on a stop button/bringing down the app server, your thread manager knows what threads are running and will give these threads a chance to save their state or finish their work without allowing any new threads to be spawned before finally bringing down the main thread itself.
When restarting, it should start where it stopped
To start from where you stopped, you need to save the state of the completed work somewhere. Assuming that you are using a Queue based system, you will have to serialize your queue before the app stops. This way, you won't lose the contents of the queue when you bring back the app.

I'm trying to write a VNC server and client in java. I have a few questions

I want to write a VNC client and server as i think i will get to learn a lot. I would want to do this in JAVA however I saw somewhere that this isn't possible. I'd want to use C++ for parts that aren't possible in JAVA. I'm a little confused about where to begin. I saw TightVNC,RealVNC etc but the code base is too large for me to make sense. I see that the RFB specifies the packet types So could someone help me get started? How do I go about writing code to make the packet formats of any use? Also could anyone explain if this is a VNC server?
I have another question:
If i ditch RFB can i write
A server application: which basically captures everything on the screen and sends it to the client every few milliseconds. (Robot screencapture is too slow) And waits for mouse movements and keystrokes.
A client application that sends mouse movements and keystokes to the server and displays the screen of the server.
However I'm worried about synchornization.

Performance and networking of Eclipse-run applet vs web applet

I have made a fairly simple turn-based multiplayer web game in an applet. My question is about the performance. I was noticing that there was a really long gap between a player taking his turn and the rest of the players seeing all the updates. It could be as long as 10 seconds for a single move/action.
The game runs on a dedicated server and all the players connect to the server as clients. As one player takes his turn, each move/action is sent to the server, and then from the server out to all the other clients. The server usually sends updates in the form of complete game-state objects, but also sends String messages. The client has a separate thread for listening for these updates. This is all done through a socket connection and persistent object input/output streams.
In my attempt to track down where the bottleneck is, I realized that if I run the applet from Eclipse (clicking "run as applet"), there is virtually no delay. So that means the client applets are sending out updates and the server is receiving them and then sending out it's updates perfectly. The bottleneck has to be in the applet's receiving/processing those updates.
I had two Chrome-applets and two Eclipse applets open on the same game. I would make a move on any of them and the two Eclipse applets would receive the server update instantly and the Chrome-applets would take as much as 10 seconds to get a single update.
Is there something import about different about how the applet runs in the browser vs in Eclipse? I know Eclipse is running the applet from local files, but doesn't a web-applet download all the appropriate files when it starts up? Thanks for your help. Let me know if posting some of my code would help.

Categories

Resources