M
Mr B
Hi,
I'm developing an application that uses a Collection class to store an
array of objects which specifies several operations, such as
addtoCollection, deletefromCollection, displayCollection etc. The
problem is, I want several instances of these collections where items
can be inserted and deleted much similar to the producer consumer
problem. Each collection has its own array and operations can be
performed on these arrays concurrently by concurrently executing
threads of producers and consumers for example. This is fine, only
until a display thread is called, where it can be interupted midway
through outputting the contents of a collection which I don't want. I
know that by making it syncronized this would work but I have no idea
what this entails, could synchronized be used for this method only, I
don't want all the other methods to have to be synchronized if
possible.
example of code is shown below:
Best Regards,
Daniel
public class Collection {
//code omitted
private int TraderID;
private Trade [] body;
public Semaphore mutex = new Semaphore(1);
public Semaphore numAvail = new Semaphore(0);
public Semaphore numFree;
public Collection(int traderId, int size){
//code omitted }
public int findMatch(Trade item)
{
//code omitted
}
public void displayCollection(){
try {
mutex.acquire();
} catch (InterruptedException ex2) {
System.out.println("mutex exception on aquire by
producer");
}
System.out.println("Collection
----------------------------------");
for (int i=0; i<body.length; i++) {
if (body!=null){
System.out.println(body.getTradeID());
}
else {
System.out.println("Empty");
}
}
mutex.release();
}
public void addtoCollection(Trade item){
//code omitted
}
public Trade removefromCollection(int tradeNum){
//code omitted
}
}
I'm developing an application that uses a Collection class to store an
array of objects which specifies several operations, such as
addtoCollection, deletefromCollection, displayCollection etc. The
problem is, I want several instances of these collections where items
can be inserted and deleted much similar to the producer consumer
problem. Each collection has its own array and operations can be
performed on these arrays concurrently by concurrently executing
threads of producers and consumers for example. This is fine, only
until a display thread is called, where it can be interupted midway
through outputting the contents of a collection which I don't want. I
know that by making it syncronized this would work but I have no idea
what this entails, could synchronized be used for this method only, I
don't want all the other methods to have to be synchronized if
possible.
example of code is shown below:
Best Regards,
Daniel
public class Collection {
//code omitted
private int TraderID;
private Trade [] body;
public Semaphore mutex = new Semaphore(1);
public Semaphore numAvail = new Semaphore(0);
public Semaphore numFree;
public Collection(int traderId, int size){
//code omitted }
public int findMatch(Trade item)
{
//code omitted
}
public void displayCollection(){
try {
mutex.acquire();
} catch (InterruptedException ex2) {
System.out.println("mutex exception on aquire by
producer");
}
System.out.println("Collection
----------------------------------");
for (int i=0; i<body.length; i++) {
if (body!=null){
System.out.println(body.getTradeID());
}
else {
System.out.println("Empty");
}
}
mutex.release();
}
public void addtoCollection(Trade item){
//code omitted
}
public Trade removefromCollection(int tradeNum){
//code omitted
}
}