Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing a system core (server) that is to handle multiple connections and requests. Lets suppose my server has a client connected and is ready to communicate. My question here is, how would i make the server know that client wants to send some data or reversal. Since read blocks until it receives data... so how to manage between the read and write operations to sockets if we don't know what the remote system at other end expects?
I can see a way around it using multithreading. Any way around please???
Multithreading is the only sensible way around this (in fact, it's one of the absolute classic cases where threading is required) - you'll have to create a new thread for each connection.
You may wish to do this directly, or you may wish to use the constructs available in java.util.concurrent (which I'd recommend) - thread pools for instance. One sensible approach might be to use a fixed thread pool to make sure the number of threads doesn't grow too ridiculously large, and then spawn threads off there as required.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to send a Java thread instance in an HTTP request?
No. A Java thread cannot be serialized, and therefore it cannot be sent outside of the JVM in which it was originally created.
(For a moment, just imagine what would be involved in moving a thread from one JVM to another. What about the static variables it might refer to? What about the other threads that it might to interact with? The problem is intractable.)
Actually I am working on clustered servers and want to execute my some threads(defined Jobs and other heavy task) on one server(called it back office node). So is there any way to do that.
OK, so that's a different problem. You actually need to execute tasks remotely, not pass threads. ('Cos you can't pass threads.)
Yes, there are many ways to send tasks to be executed elsewhere ... in the general sense. What you need to do is to express the task or task description in a form that can be serialized in some way (Java Object serialization, JSON, XML, etcetera) and then pass the serial form to the server that is going to execute it. The server would be responsible for managing the thread that does the execution.
Caveat: This is not going to work well if the task is needs to refer to a whole lot of data / objects on the machine where you formulated the task. That is going to involve serializing / passing all of the data OR figuring out some way for the backend to "call back" to get the specific data items that a task needs.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Can you please explain how singleton is use full for creating database connection?
How it will work in multithreaded environment while creating db connection and closing the connection? Once connection is closed will it gets disconnect from database?
You should normally use a "connection pool" when managing database connections in your Java applications. Creating connections is expensive / heavy so you really don't want to be creating them time and time again, especially for a busy site (the performance drop will kill it).
The manner in which you acquire the pool reference depends on the type of application (managed like servlet/JSP or a standalone application) but search around for "datasource". Also have a look at this answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I want to develop a program, not language specific, that can redirect ANY connection to another one, like a proxy server, but for all connections. The language doesn't really matter, but is there a way to do this without modifying raw windows apis and such? I'm going to attempt this in java, but I can import C and C++ code with JNI. Also, what about mac/linux?
You should certainly consider/evaluate using TCP/UDP splice. It is a well-known mechanism for building proxies and is fairly efficient in terms of copying data form one connection to another.
URL: http://linux.die.net/man/2/splice
Java is the wrong language to attempt to do this in. Even if it is possible (e.g. on Windows), the real work would need to be done either using external utilities, or using native library calls.
Attempting to do this for multiple operating systems makes this even harder. The implementation mechanisms are bound to be different.
I don't know how you would do this in Windows, but on a modern Linux system you might do this using "iptables" to enable and configure the network packet processing in the OS kernel. This requires root privilege, a good understanding of the way that network protocols, and care ... since it is easy to "brick" your networking to the degree that you need console-level access to recover.
(And if you are using OS-level virtualization, it can get particularly complicated ...)
FWIW - It is possible to install and use a port of "iptables" on Mac OS/X ... thought it is apparently not supported by Apple.
On the other hand, if you are simply trying to have a Java application direct all of its own outgoing network connections via some kind of proxy, then you should be able to do this by configuring a custom SocketFactory implementation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm using ServerSocket for creating a server.
I must create a new thread for each client or is it possible&safe to use only the server's thread?
If you just use the server's main thread, then only one client will ever be able to connect, becuase the thread is busy with the only client connected. After that client disconnects, the server can take another client, but you want to be able to accept clients constantly. Thus you need to spawn a thread for each client.
If you use only the server thread you will be able to manage only one client for time. I suggest you to use Executors class in order to create a pool of thread. The pool thread can manage multiple connection like the ExecutorService javadoc shows.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I m working in java. I have an application which will read & write one sample.txt file.
And one more application which will also read & write to same file.
And first application will run for every 1 minute as windows service.
My doubt is now how, can make sure that both applications should r/w at the same time.
Any suggestions plz.
As you want to lock access from different application if i understand it right. You can use FileLock here an example
You need to guard the file access code through synchronized block or methods. Learn more about synchronization here:
http://javarevisited.blogspot.in/2011/04/synchronization-in-java-synchronized.html
Simples solution is to use the synchronized keyword.
public synchronized int i = 1;
in this way i can only be accessed from one thread at the time. But this is expansive! There are many concepts of synchronization, e.g. the monitor
Read this for more information: http://www.artima.com/insidejvm/ed2/threadsynch.html