wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Thread Visibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Visibility" Watch "Thread Visibility" New topic
Author

Thread Visibility

M Mohile
Greenhorn

Joined: Sep 22, 2012
Posts: 17
Hi Ranchers,
This application is such that data will be coming from socket and will eventually get stored in a CopyOnWriteArrayList that is running in a separate thread called from the same class. This List will be accessed from servlet (servlet is in different package) via AJAX calls. But whenever I call the required method (getOFSOne() or getOFSTwo()) from servlet, this CopyOnWriteArrayList throws IndexOutOfBoundsException. I have tried printing the contents of CopyOnWriteArrayList when all the data coming from the socket has been received and it gets printed but when the AJAX call comes to the servlet, the methods (getOFSOne() or getOFSTwo()) called from servlet are not able to get the contents of CopyOnWriteArrayList. So I conclude that the Servlet is not able to view the contents of the list.

Please help what should I be doing in such a scenario. If my design should change, suggestions are most welcome.

The code for the class (that collects the data via socket) which has the thread in which CopyOnWriteArrayList runs, the listener() method that listens for the incoming data via socket, getOFSOne() and getOFSTwo() methods is as follows:



The Servlet class is as follows:


Thanks..


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

As far as the Trades work is concerned, your Servlet code amounts to this:

This is 'create a new instance of trades and call getOFSOne() on it. You do nothing in between the two. The problem here is that you only fill the OFSOne list inside the run() method, which is designed to be called in a Thread. You never start the thread so you never get the list filled.

Now, in your other class, you have a main method which does start a Thread to run the code which fills the lists. You also have a connection to a Socket, which looks like it feeds the data from the socket into the running thread via a Queue. But you only do that when the class is run as a standalone application. So it has nothing to do with the case where you create an instance of Trades via the constructor.

My guess is that you are making things a lot more complicated than they have to be - but I don't understand your design. But if you think that the Trades object used when the main method is called is the same Trades object when the Servlet creates a new one via the constructor, you are mistaken - they are not the same, they do not share state, and in all likelyhood would not even share the same static context (probably different JVMs if you run the main method from the command line).


Steve
M Mohile
Greenhorn

Joined: Sep 22, 2012
Posts: 17
Hi Steve...I understood where I'm going wrong.

Regarding the design, just think this Trades is some sort of middleware where a stream of incoming data will be received via socket (the listener() method). This data will be put in the PriorityBlockingQueue. PriorityBlockingQueue is chosen because if in the absense of incoming data coming via socket, the Queue will wait till its available. Then data processing takes place in the while loop and eventually, it gets placed in the CopyOnWriteArrayList. Now this data is to be accessed from the list via AJAX calls that will be calling the servlet every one second. So as soon as the data is received from socket, it will be immediately available on a web-page. That's it.

Please suggest how should I make the data in the list available to the servlet. I tried making the list and even the Trades instance global by putting it in a new class where it was declared as static but my effort was fruitless. Please help me in this scenario and if possible with an example.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

What is feeding the data to Trades via sockets (what is at the other end of the Socket Trades uses)? From this code:

we know it is on the local host, is it the same server your Servlet is running on?

Second, why do you need a thread that takes what the listener puts in a queue just to put that same data into one of two lists? Why not just use the listener to move the data directly into the lists?

Finally, if you want to use the data Trades holds, you will need to create and run the Trades code in your web application someplace. That means the code that you have in the main method should go someplace in your web application. It probably does not belong in a servlet, because it seems to be out of scope (it last longer than) a single request. Maybe use a ServletContextListener to start the task running when the Web App starts and put the copy of Trades into the ServletContext, then when the Servlet wants to use the data it would retrieve Trades out of the ServletContext to get the lists it needs.
M Mohile
Greenhorn

Joined: Sep 22, 2012
Posts: 17
No. The servlet and the socket connection from where the data is getting sent are on different servers.

As suggested, the thread and the BlockingQueue that is running is removed. So here's the fresh code:



Here, the Trades has been moved into the web-app space and now implements ServletContextListener interface. I did not get what you meant by
That means the code that you have in the main method should go someplace in your web application.
because how could I connect to the socket without a main method and therefore, I left it as it is and defined Trades trades in the instance variable space as static. Now the JVM does not throw an exception but it neither does output anything, not even null. I understand that declaring anything as static results in a unit of construction getting created in a unit of description(class) but that was the best I could think of.
Please correct me what necessary changes need to be made to get this code working..
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

M Mohile wrote:I did not get what you meant by
That means the code that you have in the main method should go someplace in your web application.
because how could I connect to the socket without a main method

The same you do with the main method. There is nothing special about the main method, other than that it is the launch point for stand alone applications. Your code needs to run in the Web App, it therefore is not a stand alone application and you need to use a different launch point. What makes a good launch point in your current situation?

and therefore, I left it as it is and defined Trades trades in the instance variable space as static.

Why? Just doing it because you know the static keyword isn't good enough. You are throwing things against the wall and hoping it sticks. You won't fix a problem by adding simple keywords. Trying won't help, but it may make things worse.
Now the JVM does not throw an exception but it neither does output anything, not even null. I understand that declaring anything as static results in a unit of construction getting created in a unit of description(class) but that was the best I could think of.
Please correct me what necessary changes need to be made to get this code working..

I think you need to step back from coding and start to research some more. I think you need to go back and learn some basics (object creation, references, static versus instance variables, data access) and, understanding that, I would guess you also need significant research on how web applications work as well. Have you, for example, registered you context listener? Why do you want to cause an exception when the application shuts down?

There is no quick fix to your problem - there are some not-too-difficult ways to get the code to run, but they aren't your problem, and I don't think you will understand them if I told you (since I told you and you didn't understand). So rather than just feed you lines to copy and paste I will rather suggest you take a step back, start a new simpler desktop-only project for educational purposes until you understand how Java works. Then add the Web Application layer on. Then, with that done, come back to this application, and fix it.
M Mohile
Greenhorn

Joined: Sep 22, 2012
Posts: 17
Its absolutely true for anyone to suggest me to step back from coding and learn some basics for the small things that I'm not able to get correctly which indeed made me realize that I should now get a good sleep and come back only when my mind is fresh. You had provided me the answer in your first blog itself. Thank-you Steve for your help and time. The issue has been resolved!!
 
 
subject: Thread Visibility
 
Similar Threads
Null pointer at request.getRequestDispatcher("foo.jsp").forward(request, response)
Uploading Image from Java Desktop App to Server
web service integration using Netbeans IDE
error in sending image from applet to servlet
HTTP method GET is not supported by this URL