Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Forked from carlosroman/SomeResourceCounter.go
Created March 31, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovacshuni/e873a4e3f97e68d1af0f9dc631cd14ba to your computer and use it in GitHub Desktop.
Save kovacshuni/e873a4e3f97e68d1af0f9dc631cd14ba to your computer and use it in GitHub Desktop.
Java To Go mistakes
type SomeResource struct {
counter int
}
func (sr *SomeResource) incCounter(rw http.ResponseWriter, r *http.Request) {
sr.counter++
}
func (sr *SomeResource) getCounter(rw http.ResponseWriter, r *http.Request) {
v := strconv.Itoa(sr.counter)
rw.Write([]byte(v))
}
@Path("/something")
public class SomeResource {
private int counter = 0;
@POST
public void incCounter() {
this.counter++;
}
@GET
public int getCounter() {
return this.counter;
}
}
type SomeResource struct {
counter int64
}
func (sr *SomeResource) incCounter(rw http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&sr.counter, 1)
}
func (sr *SomeResource) getCounter(rw http.ResponseWriter, r *http.Request) {
v := strconv.FormatInt(atomic.LoadInt64(&sr.counter), 10)
rw.Write([]byte(v))
}
@Path("/something")
public class SomeResource {
private final AtomicInteger counter = new AtomicInteger(0);
@POST
public void incCounter() {
this.counter.incrementAndGet();
}
@GET
public int getCounter() {
return this.counter.get();
}
}
func NewSomeResource() *SomeResource {
sr := SomeResource{
ch: make(chan *SomeObject, 2),
}
go func() {
for so := range sr.ch {
sr.store = append(sr.store, *so)
}
}()
return &sr
}
type SomeResource struct {
store []SomeObject
ch chan *SomeObject
}
func (sr *SomeResource) addItem(rw http.ResponseWriter, r *http.Request) {
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
sr.ch <- &so
}
func (sr *SomeResource) getItems(rw http.ResponseWriter, r *http.Request) {
json.NewEncoder(rw).Encode(sr.store)
}
@Path("/something")
public class SomeResource {
private final ExecutorService es = Executors.newFixedThreadPool(10);
private final List<SomeObject> sos = new ArrayList<>();
@POST
public void addItem(final SomeObject so) {
es.execute(() -> sos.add(so));
}
@GET
public List<SomeObject> getItems() {
return this.sos;
}
}
func NewSomeResource() *SomeResource {
sr := SomeResource{
ch: make(chan *SomeObject, 2),
}
go func() {
for so := range sr.ch {
sr.Lock()
sr.store = append(sr.store, *so)
sr.Unlock()
}
}()
return &sr
}
type SomeResource struct {
sync.RWMutex
store []SomeObject
ch chan *SomeObject
}
func (sr *SomeResource) addItem(rw http.ResponseWriter, r *http.Request) {
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
sr.ch <- &so
}
func (sr *SomeResource) getItems(rw http.ResponseWriter, r *http.Request) {
sr.RLock()
defer sr.RUnlock()
json.NewEncoder(rw).Encode(sr.store)
}
@Path("/something")
public class SomeResource {
private final ExecutorService es = Executors.newFixedThreadPool(10);
private final List<SomeObject> sos = Collections.synchronizedList(new ArrayList<>());
@POST
public void addItem(final SomeObject so) {
es.execute(() -> sos.add(so));
}
@GET
public List<SomeObject> getItems() {
return this.sos;
}
}
func NewSomeResource() *SomeResource {
sr := SomeResource{store: make(map[string]int)}
return &sr
}
type SomeResource struct {
store map[string]int
}
func (sr *SomeResource) setValue(rw http.ResponseWriter, r *http.Request) {
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
sr.store[so.Key] = so.Value
}
func (sr *SomeResource) getValue(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
v := strconv.Itoa(sr.store[vars["key"]])
rw.Write([]byte(v))
}
@Path("/something")
public class SomeResource {
private final Map<String, Integer> map = new HashMap<>();
@POST
public void setValue(final SomeObject so) {
this.map.put(so.getKey(), so.getValue());
}
@GET
@Path("{key}")
public int getValue(@PathParam("key") final String key) {
return this.map.getOrDefault(key, -1);
}
}
func NewSomeResource() *SomeResource {
sr := SomeResource{store: make(map[string]int)}
return &sr
}
type SomeResource struct {
sync.RWMutex
store map[string]int
}
func (sr *SomeResource) setValue(rw http.ResponseWriter, r *http.Request) {
sr.Lock()
defer sr.Unlock()
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
sr.store[so.Key] = so.Value
}
func (sr *SomeResource) getValue(rw http.ResponseWriter, r *http.Request) {
sr.RLock()
defer sr.RUnlock()
vars := mux.Vars(r)
v := strconv.Itoa(sr.store[vars["key"]])
rw.Write([]byte(v))
}
@Path("/something")
public class SomeResource {
private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
@POST
public void setValue(final SomeObject so) {
this.map.put(so.getKey(), so.getValue());
}
@GET
@Path("{key}")
public int getValue(@PathParam("key") final String key) {
return this.map.getOrDefault(key, -1);
}
}
type SomeResource struct {
}
func (sr *SomeResource) calculate(rw http.ResponseWriter, r *http.Request) {
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
var wg sync.WaitGroup
var results []int
for _, val := range so.Values {
wg.Add(1)
go func() {
result := val + 1
results = append(results, result)
defer wg.Done()
}()
}
wg.Wait()
json.NewEncoder(rw).Encode(results)
}
@Path("/something")
public class SomeResource {
private final ExecutorService es = Executors.newFixedThreadPool(10);
@POST
public List<Integer> calculate(final SomeObject so) throws InterruptedException {
final List<Integer> results = new ArrayList<>();
final List<Callable<Boolean>> cb = new ArrayList<>();
so.getValues().forEach(i -> cb.add(() -> {
final int result = i + 1;
results.add(result);
return true;
}));
final List<Future<Boolean>> futures = this.es.invokeAll(cb);
futures.forEach(f -> {
try {
f.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
return results;
}
}
func (sr *SomeResource) calculate(rw http.ResponseWriter, r *http.Request) {
var so SomeObject
json.NewDecoder(r.Body).Decode(&so)
var wg sync.WaitGroup
var results []int
ch := make(chan int)
for _, val := range so.Values {
wg.Add(1)
go func(val int) {
defer wg.Done()
result := val + 1
ch <- result
}(val)
}
go func() {
wg.Wait()
close(ch)
}()
for val := range ch {
results = append(results, val)
}
json.NewEncoder(rw).Encode(results)
}
@Path("/something")
public class SomeResource {
private final ExecutorService es = Executors.newFixedThreadPool(10);
@POST
public List<Integer> calculate(final SomeObject so) throws InterruptedException {
final List<CompletableFuture<Integer>> futures = so
.getValues()
.stream()
.map(i -> CompletableFuture.supplyAsync(()-> i + 1, this.es))
.collect(Collectors.toList());
return futures
.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment