-
Notifications
You must be signed in to change notification settings - Fork 882
Add guards around structures to prevent racing. #74
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,9 @@ | |
import java.util.*; | ||
|
||
public class PetData { | ||
static List<Pet> pets = new ArrayList<Pet>(); | ||
static List<Category> categories = new ArrayList<Category>(); | ||
|
||
static List<Pet> pets = Collections.synchronizedList(new ArrayList<Pet>()); | ||
static List<Category> categories = Collections.synchronizedList(new ArrayList<Category>()); | ||
|
||
static { | ||
categories.add(createCategory(1, "Dogs")); | ||
|
@@ -68,11 +69,13 @@ public Pet getPetById(long petId) { | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you in almost all methods synchronize on The iterator is not synchronized, but checks for concurrent modifications (in an unsynchronized way). On the other hand, if you synchronize everything anyways, you could get rid of the synchronizedList. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed the iterator around it. I also removed the synchronizedLists since all the operations are within sync {}. |
||
public boolean deletePet(long petId) { | ||
if(pets.size() > 0) { | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
Pet pet = pets.get(i); | ||
if(pet.getId() == petId) { | ||
pets.remove(i); | ||
return true; | ||
synchronized (pets) { | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
Pet pet = pets.get(i); | ||
if(pet.getId() == petId) { | ||
pets.remove(i); | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -85,10 +88,12 @@ public List<Pet> findPetByStatus(String status) { | |
return result; | ||
} | ||
String[] statuses = status.split(","); | ||
for (Pet pet : pets) { | ||
for (String s : statuses) { | ||
if (s.equals(pet.getStatus())) { | ||
result.add(pet); | ||
synchronized (pets) { | ||
for (Pet pet : pets) { | ||
for (String s : statuses) { | ||
if (s.equals(pet.getStatus())) { | ||
result.add(pet); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -102,12 +107,14 @@ public List<Pet> findPetByTags(String tags) { | |
return result; | ||
} | ||
String[] tagList = tags.split(","); | ||
for (Pet pet : pets) { | ||
if (null != pet.getTags()) { | ||
for (Tag tag : pet.getTags()) { | ||
for (String tagListString : tagList) { | ||
if (tagListString.equals(tag.getName())) | ||
result.add(pet); | ||
synchronized (pets) { | ||
for (Pet pet : pets) { | ||
if (null != pet.getTags()) { | ||
for (Tag tag : pet.getTags()) { | ||
for (String tagListString : tagList) { | ||
if (tagListString.equals(tag.getName())) | ||
result.add(pet); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -116,37 +123,41 @@ public List<Pet> findPetByTags(String tags) { | |
} | ||
|
||
public Pet addPet(Pet pet) { | ||
if(pet.getId() == 0) { | ||
long maxId = 0; | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
if(pets.get(i).getId() > maxId) { | ||
maxId = pets.get(i).getId(); | ||
synchronized (pets) { | ||
if(pet.getId() == 0) { | ||
long maxId = 0; | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
if(pets.get(i).getId() > maxId) { | ||
maxId = pets.get(i).getId(); | ||
} | ||
} | ||
pet.setId(maxId + 1); | ||
} | ||
pet.setId(maxId + 1); | ||
} | ||
if (pets.size() > 0) { | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
if (pets.get(i).getId() == pet.getId()) { | ||
pets.remove(i); | ||
if (pets.size() > 0) { | ||
for (int i = pets.size() - 1; i >= 0; i--) { | ||
if (pets.get(i).getId() == pet.getId()) { | ||
pets.remove(i); | ||
} | ||
} | ||
} | ||
pets.add(pet); | ||
} | ||
pets.add(pet); | ||
return pet; | ||
} | ||
|
||
public Map<String, Integer> getInventoryByStatus() { | ||
Map<String, Integer> output = new HashMap<String, Integer>(); | ||
for(Pet pet : pets) { | ||
String status = pet.getStatus(); | ||
if(status != null && !"".equals(status)) { | ||
Integer count = output.get(status); | ||
if(count == null) | ||
count = new Integer(1); | ||
else | ||
count = count.intValue() + 1; | ||
output.put(status, count); | ||
synchronized (pets) { | ||
for(Pet pet : pets) { | ||
String status = pet.getStatus(); | ||
if(status != null && !"".equals(status)) { | ||
Integer count = output.get(status); | ||
if(count == null) | ||
count = new Integer(1); | ||
else | ||
count = count.intValue() + 1; | ||
output.put(status, count); | ||
} | ||
} | ||
} | ||
return output; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,10 @@ | |
|
||
import io.swagger.sample.model.Order; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.*; | ||
|
||
public class StoreData { | ||
static List<Order> orders = new ArrayList<Order>(); | ||
static List<Order> orders = Collections.synchronizedList(new ArrayList<Order>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you always access this in a synchronized block, there is no need for the additional synchronizedList. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. Thanks, |
||
|
||
static { | ||
orders.add(createOrder(1, 1, 2, new Date(), "placed")); | ||
|
@@ -39,32 +37,38 @@ public class StoreData { | |
} | ||
|
||
public Order findOrderById(long orderId) { | ||
for (Order order : orders) { | ||
if (order.getId() == orderId) { | ||
return order; | ||
synchronized (orders) { | ||
for (Order order : orders) { | ||
if (order.getId() == orderId) { | ||
return order; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Order placeOrder(Order order) { | ||
if (orders.size() > 0) { | ||
for (int i = orders.size() - 1; i >= 0; i--) { | ||
if (orders.get(i).getId() == order.getId()) { | ||
orders.remove(i); | ||
} | ||
synchronized (orders) { | ||
if (orders.size() > 0) { | ||
for (int i = orders.size() - 1; i >= 0; i--) { | ||
if (orders.get(i).getId() == order.getId()) { | ||
orders.remove(i); | ||
} | ||
} | ||
orders.add(order); | ||
} | ||
return order; | ||
} | ||
orders.add(order); | ||
return order; | ||
} | ||
|
||
public boolean deleteOrder(long orderId) { | ||
if (orders.size() > 0) { | ||
for (int i = orders.size() - 1; i >= 0; i--) { | ||
if (orders.get(i).getId() == orderId) { | ||
orders.remove(i); | ||
return true; | ||
synchronized (orders) { | ||
for (int i = orders.size() - 1; i >= 0; i--) { | ||
if (orders.get(i).getId() == orderId) { | ||
orders.remove(i); | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you don't need to synchronize this, as it is not used anywhere outside the static initializer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed. Thanks.