I tried to look around for some time, but I was not able to find an answer to my problem.
I am currently writing a Java program that will be used to read and update the firmware of an HID device. I am using the pureHID java libraries to communicate with my device and everything is working fine.
The only problem I am currently experiencing, and I was not able to fix, concerns the synchronization of events.
I will give you an example.
When copying the firmware currently installed on the device, I am using the following method
dev.setOutputReport((byte) 0, packet_to_send, BUFFER_SIZE);
The answer from the unit will come in the form of an event received by the listener attached to the device manager, namely the following listener will be invoked
onInputReport() /* Something will happen */
My problem is that SOMETIMES, probably because of concurrent writing/listening invocation, the program completely freeze, without any exception nor any error signaled.
To fix the problem, I tried to implement a simple Lock mechanism, with the Lock being released only when a reply is received; but unfortunately it may happen that the reply packet sometimes is lost thus blocking me in a deadlock.
One possible solution I though would be to use a timer which will release the lock automatically after x milliseconds if it is not released (the reply will arrive within 10ms or will not arrive), but I do not know how to create an independent thread to manage the timer.
Can you please help me fixing this problem, or suggest me another concurrency mechanism which I can use to synchronize the two blocks OR to re-transmit the message, if a reply is lost?
Thank you very much to anyone helping.
Lorenzo
Related
We are running a Java server app that is using ScheduledThreadPoolExecutor to manage some work. There are multiple instances running, for different types of work, but each instance only uses one thread. It's not important why this is as there's really no way around it. What we noticed on the production server is that one of these instances stopped working at some point, completely and silently. Restarting the server brought it back again, but the problem isn't solved.
I know that using scheduleAtFixedRate will stop if the task throws an exception at some point, but this isn't the case here. We had a recurrent task that simply stopped executing, and new tasks that used the schedule() method and still didn't execute. I presume that the thread it was using died and didn't start again.
My question is, are there any circumstances under which this could happen? Is there anything I should look out for?
It looks like the simplest explanation is the answer: all threads hang.
In my case the cause of this seems to be HTTP requests that never timeout. This can happen in certain situations and I am yet to find a good solution for the problem. I think the best option is to implement a timeout on the scheduled task itself to make sure we avoid any issues.
I am writing a HTTP THREAD-POOLED WEB SERVER code. I made my code at my best. It works fine also.
But there is one problem, whenever I want to shut down my server, I have to use CTRL + C. But that is a bad way to shut down.
I think a lot, to add shutdown feature in my code. But I am unable to do that.
Please help me to add this feature, give any suggestion to do that, I will definitely code that.
EDIT NO. 1
One method that I think is to make one thread that is only listening STDIN input given by keyboard. Whenever it gets "SHUTDOWN". It calls ThreadPol.shutdown(). This way I can achieve this goal.
Is this a right method ? If yes, please help me to implement this.
You can do
ThreadPoolExecuto.shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
I have scenario where I am scheduling task at fixed duration repetitively.Fixed delay is generated by calling start method of another class that implements runnable interface using Thread.sleep(long ms ) method.
But when I test this application in my local pc it is working.But when I run this application in ibm blade server(64 bit) having OS(Windows server 2008 R2) it do not work as desired. It do not come out of sleep method.
Kindly suggest the solution?
Thank You in Advance.
There is not much information in your question to see what the problem is. Thread.sleep should either return or throw an exception. Maybe something different is happening. For example, an exception has occurred, caught and forgotten, or you have a deadlock somewhere. Anyway, different versions of Java sometimes have subtle differences causing bugs. You will have to investigate the problem yourself.
Try debugging the application. When it hangs, press Pause and examine all threads to find the hanging one.
If you can't install a debugger on the server, add System.out.println in every reasonable place of the code; reading the output in the console, you will probably be able to track down the issue.
If you can't launch the application with console, create a text file and write the messages to it. Don't forget to flush it every time.
I'm seeing strange behavior and I don't know how to gain any further insight into and am hoping someone can help.
Background: I have a query that takes a long time to return results so instead of making the user wait for the data directly upon request I execute this query via a Timer object at regular intervals and store the results in a static variable. Therefore, when the user requests the data I always just pull from the static variable, therefore making the response virtually instant. So far so good.
Issue: The behavior I'm seeing, however, is that if I make a request for the data just as the background (Timer) request has begun to query the data, my user's request waits for the data to come back before responding -- forcing the user to wait. It's as if tomcat is behaving synchronously with the threads (I know it's not -- it just looks that way).
This is in a Production environment and, for the most part, everything works great but for users there are times when the site just hangs for them and they feel it's unreliable (well, in a sense it is).
What I've done: Being that the requests for the data were in a static method I thought "A ha! The threads are syncronized which is causing the delay!" so i pulled all of my static methods out, removed the syncronization and forced each call to instantiate it's own object to retrieve the data (to keep it thread safe). There isn't any syncronization on a semaphore to the static variable either.
I've also installed javamelody to try and gain some insight into what's going on but nothing new thus far. I have noticed a lot (majority) of threads are in "WAITING" state but they also have 0ms for User and CPU time so don't think that is pointing to anything(?).
Running Tomcat 5.5 (no apache layer), struts 2, Java 1.5
If anyone has any idea why a simple request to a static variable hangs for longer background processes I would really appreciate it! Or if you know how I can gain insight that would be great too.
Thanks!
One possible explanation is that the threads are actually blocking at the database level due to database locking (or something) caused by the long-running query.
The way to figure out what is going on is to find out exactly where the blocked threads are blocking. A thread dump can be produced by sending a SIGQUIT (or equivalent) to the JVM, and included stack traces for all Java thread stacks. Alternatively, you can get the same information (and more) by attaching a debugger, etcetera. Either way, the class name and line number of the top frame of each stack should allow you to look at the source code and figure out (at least) what kind of locking or blocking is going on.
For those who would like to know I eventually found VisualVM (http://visualvm.java.net/download.html). It's perfect. I run Tomcat from eclipse like I normally do and it appears within the VisualVM client. Right-mouse click the tomcat icon, choose Thread Dump and, boom, I've got it all.
Thanks, all, for the help and pointers towards the right direction!
I'm developping a communication library based on NIO's non-blocking SocketChannels so I can use select to keep my thread low on CPU usage (and getting faster reaction time to other events).
SocketChannel are created externally to my thread and added to the list it handles, marking them as non-blocking and adding them to a Selector for READ operations (and WRITE when needed, but that does not happen in my problem).
I have a little Swing application for tests, running locally, that can be either a client or server: the client one connects to the server one and they can send each other messages. Pretty simple and works fine, excepts for the CPU which tops 100% (50% for each jvm) as soon as the connection is established between client and server.
Running jvisualvm shows me that sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run() uses 98% of the application time, counting only 3 method calls!
A forced stack trace shows it's blocking on the read operation on a FilteredInputStream, on a Socket.
I'm a little puzzled as I don't use RMI (though I can understand NIO and RMI can share the "transport" code part). I have seen a few similar questions but each were specifically using RMI, which I'm not. The answers I've seen is that this ConnectionHandler.run() method is responsible for marshalling/unmarshalling things, when here I get 100% CPU without any network traffic. I can only infer an active wait on the sockets but that sounds odd, especially with non-blocking SocketChannel...
Any idea would be greatly appreciated!
I tracked CPU use down to select(int timeout) which returns 0 immediately, regardless of the timeout value. My understanding of this function was it would block until a selected operation pops up, or timeout is reached (as said in the Javadoc).
However, I found out this other StackOverflow post showing the same problem: OP_CONNECT operation has to be cancelled once connection is accepted.
Many thanks to #Alexander, and #EJP for clarification about the OP_WRITE/OP_CONNECT similarities.
Regarding tge RMI part, it was probably due to Eclipse run configurations.