File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Synchronisation Of Arrays, Please Explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Synchronisation Of Arrays, Please Explain" Watch "Synchronisation Of Arrays, Please Explain" New topic
Author

Synchronisation Of Arrays, Please Explain

Alex Mackey
Greenhorn

Joined: Feb 04, 2005
Posts: 5
Hi, could someone explain to me how a synchronised array works, or point me to a page that explains them well.

I have been given some code for a order processing system and I am very stuck on my current task to do on it, we are on a break at the moment, but I want to get this done.
I will break down what I have to do in points.
1. Cashier Client GUI (Client Package) creates a new order, which is a private brought Arraylist, uses the newOrder method to do this. (already done)
2. Pick Client GUI (Client Pagage) returns the list of orders waiting to be picked from the warehouse. IE an instance of the boughtlist. (need to do, uses most code from cashier)
3. They both use Order (OrderProcessing Package) to do this. (Need to do the getOrderToPick()) SEE BELOW

I have had real trouble accessing the BoughtList from another class, I tried setting up inheritance but this just seemed to give all sorts of errors from the code the tutor gave us to start with, so I dont think the answer lies here. Should it be handled here in the order Class. If someone could explain why these arrays are synchronised that would help, is that something to do with it.
Thanks alot.
I wanted to put the code on a different page, but having trouble with that so I will have to put it here.
Order


Cashier


Pick is almost exactly the same as cashierw, just no buy buttons, and an extra button to see waiting orders.
[ February 04, 2005: Message edited by: Alex Mackey ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Alex Mackey:
I have had real trouble accessing the BoughtList from another class, I tried setting up inheritance but this just seemed to give all sorts of errors from the code the tutor gave us to start with, so I dont think the answer lies here. Should it be handled here in the order Class. If someone could explain why these arrays are synchronised that would help, is that something to do with it.
First, it's the Order class itself that is synchronized, not "these arrays" (though the BoughtList class may also be synchronized, but without the source I can't tell). Order is synchronized because it manages an ArrayList of BLs waiting to be processed and is probably accessed by multiple threads. I recommend going to Sun's site and doing the Threads tutorial as it will explain synchronization.

Briefly, synchronization tells the JVM to only allow one thread to operate on the synchronized object at a time. When you call a synchronized object, you have to wait until you can acquire an exclusive lock on the object. When you exit the method, you release the lock. Calling a non-synchronized method on the object is not subject to these rules.

In Order's case, this ensures that one Thread cannot add a new BL to the waiting tray while some other Thread is picking a BL to process.

I assume that you're supposed to modify the getOrderToPick() method to choose one BL from the waiting tray, remove it from the tray, and return it to the caller. If so, synchronization doesn't erally change how you should write that method. You should have been given some requirements (or are free to make up your own) on how to choose a BL, possibly as simple as "the first in the tray." Don't worry about synchronization here; the method is already declared synchronized so you're good to go.

I don't quite know what you mean by "trouble accessing the BoughtList from another class." This Order class clearly passes around and stores BoughtLists, so why should getOrderToPick() be any different? And what other class are you refering to? Can you clarify exactly what part of the problem you're trying to solve now and what errors you are getting?
Alex Mackey
Greenhorn

Joined: Feb 04, 2005
Posts: 5
Thanks very much for reply. I will try to explain.

I was thinking that I could put something like this in the getOrderToPick class and it would read the first item in the waiting tray, as thats all we have to display in the window.

But I just get a Null pointer exeption. I thought the information in the waiting tray would carry over from the new order method?? Does it not work as the Pick and Cashier Clients are seperate classes that run seperatly?

I thought that making bought in the parameter would help but this didnt work as the BoughtList is private, so I tried setting up an inheritence with an accessor, thats where I was getting errors so I assumed I was on the wrong track.

I am propably missing something obvious, but as someone who doesnt find java that intuitive (I am in the second year of a java module that has moved pretty quickly and I had done no programming before) I often miss these things. Plus arraylists I find quite hard.
FYI the BoughtList class is not synchronised.

Thanks again.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
No worries about not understanding; programmnig is pretty darn complex. My first recommendation is to get familiar with the JavaDocs for ArrayList. If you're using a JDK prior to 1.5, just find ArrayList in those installed on your computer (they come with the JDK). Also, unzip the source files if you haven't already so you can dig into the Java internals when you need more details.

ArrayList is a list of elements, each with an index (0, 1, 2, ...). The list grows as you add() elements, and you can access individual elements using get(i) and replace them using set(i, element). Finally, you can remove() elements, causing elements with higher indices to shift down one index.

For a metaphor, imagine taking a normal piece of lined note paper and numbering each line starting at 0. You write one element on each line, and you cannot skip lines (though you can put a "blank" aka null element on a line, but that line still has an element).

By "put something like this in the getOrderToPick class" are you talking about the getOrderToPick() method of the Order class? Or are you subclassing Order with your own class? Is Order your class or one given to you that you cannot modify?

Look again at the code you gave this last post. It loops from 0 to one less than the size of the list (this covers the full range of element indices). For each index, you get the BoughtList from the waiting tray and print it. However, the method, I assume, is supposed to choose one BoughtList from the tray and return it. What are you returning from your method?

But I just get a Null pointer exeption. I thought the information in the waiting tray would carry over from the new order method?? Does it not work as the Pick and Cashier Clients are seperate classes that run seperatly?
Are you running each of the clients in its own JVM by typing "java <someclass>" in different command windows/shells? If so, then they don't see each other and won't affect each other. These need to be run using a single JVM, probably by writing a class with a main method that creates each of the GUI windows and opening them all.

However, notice that waitingTray is initialized to a new ArrayList, and the reference is never changed. This tells me that it is unlikely to be the source of your NPE. If you catch the exceptiin and print its stack trace, it will tell you the class, method, file, and line number where the exception occurred. If you are unfamiliar with exceptions and try-catch blocks, go through the Sun tutorial on them. This is very key to becoming proficient in Java and will allow you to debug your programs more quickly.
Alex Mackey
Greenhorn

Joined: Feb 04, 2005
Posts: 5
I thought it would be easyer to just give you the whole file for the catalouge shop system. It is run by using the main method.
The current task I am on is creating a PickGUI which will display the next waiting order in the BoughtList, the Picker will then be able to press a button to signify the picking is complete.
I realised when I said I had a null pointer exeption that was actually just null being printed out in the terminal window, that was becuase I was trying to call the getOrderToPick method against theOrder in PickGUI, which is null in the constructor.
We use a compiler called BlueJ which is very user friendly and will point out where and errors are in the code.
Most of the code was given to us to start of with so I dont expect to many big changes need to be made to the actual structure of the classes. We just had to perform a number of tasks on the code (such as making a Back Door Client) and this is the first one I have become stuck on, and I cant do any of the next tasks without doing this one, and I dont want to waist the holiday time.
I dont want it to seem like I am trying to get the answer out of you, I would just like a base to start from, then I should be away, at the moment I am just confused.

(Right Click Save target as)
http://www.angelfire.com/extreme4/motorsports/jd3.zip
[ February 06, 2005: Message edited by: Alex Mackey ]
Alex Mackey
Greenhorn

Joined: Feb 04, 2005
Posts: 5
UPDATE:
I dont need the in depth help I was asking for in the last post now.

I have managed to get it to display the next order to pick, by having this in the getOrderToPick().

The reason I was getting the null was because I was calling it against theOrder in Pick, which is null. So I made a test button which called the method against theOrder from the cashier class, low and behold, it worked. Now I just need to work out what I need to call my getOrderToPick() against in the PickGUI class, as it contains no reference to theOrder from the Cashier GUI, im sure I will work it out.
Thanks for the help
Alex Mackey
Greenhorn

Joined: Feb 04, 2005
Posts: 5
OMG I found the reason I was getting null in the PICK was becuase I left out ONE line. (a pretty vital one though)


I could swear and kiss somebody at the same time, such is the way of java.
 
Consider Paul's rocket mass heater.
 
subject: Synchronisation Of Arrays, Please Explain
 
Similar Threads
JTable(my model is changed how do i update my view)?
update a JList based on an Array
close window when new one opens
The serializable class does not declare a static final...
Layout Managers and preferred size