Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View ggalmazor's full-sized avatar
🏠
Working from home

Guillermo Gutiérrez ggalmazor

🏠
Working from home
View GitHub Profile
@ggalmazor
ggalmazor / read_description.cpp
Last active July 16, 2021 19:52
funcion para leer la frecuencia de transceptor por puerto serie (Arduino)
void read_frequency() {
if (Serial.available() <= 0)
return;
String description = read_serial_message(0xFD);
if (!is_valid(description))
return;
return parse_frequency(description);

Keybase proof

I hereby claim:

  • I am ggalmazor on github.
  • I am ggalmazor (https://keybase.io/ggalmazor) on keybase.
  • I have a public key ASAbZuXpyLdy8y8QZR-YKb2L5yVNgcFhlzkWN3dgyNr8MAo

To claim this, I am signing this object:

@ggalmazor
ggalmazor / README.md
Last active February 16, 2019 14:25
Stopping background jobs: a Java concurrency refactoring case

Stopping background jobs: a Java parallel code refactoring case

I spend most of my working day helping to maintain and evolve the awesome OpenDataKit tool kit, which includes ODK Briefcase, a Java desktop application (Swing) that takes care of pulling, pushing and exporting forms.

Before continuing, I feel like I should give a disclaimer. It's a long post that's served me to put my thoughts together and fix in my head what I've learned. You could read on and miss some obvious and much simpler solution to some problem. If that happens, please, please, write a comment! I'm not trying to write any foundational text. I'm just a guy figuring out how to do parallel jobs in Java, and I'm probably wrong.

The challenge

Briefcase interacts with servers that store blank forms and answered form submissions using an HTTP API to download them to a user's computer for post-processing.

public class Job<T> {
private final JobAwareSupplier<T> block;
private Job(JobAwareSupplier<T> block) {
this.block = block;
}
public static <U> Job<U> supply(JobAwareSupplier<U> supplier) {
return new Job<>(supplier);
}
@ggalmazor
ggalmazor / Source.java
Last active February 8, 2019 16:09
Medium - Java Concurrency - Block that launches the pull process
public ExecutorService pull(List<FormStatus> forms) {
ExecutorService executor = new ForkJoinPool(
commonPool().getParallelism(),
commonPool().getFactory(),
this::handleError,
commonPool().getAsyncMode()
);
forms.stream()
.map(form -> pullOne(form, executor))
@ggalmazor
ggalmazor / ExportPanelForm.java
Created March 20, 2018 06:26
Indirection without abstraction - Snippet 3
public class ExportPanelForm {
private static final String EXPORTING = "Exporting";
private static final String DOT = ".";
private JLabel exportingLabel;
public void updateExportingLabel() {
String newText = cycle(exportingLabel.getText(), 19);
exportingLabel.setText(newText);
}
@ggalmazor
ggalmazor / ExportPanelForm.java
Last active March 20, 2018 06:25
Indirection without abstraction - Snippet 1
public class ExportPanelForm {
private JLabel exportingLabel;
public void updateExportingLabel() {
String text = exportingLabel.getText();
if (text.equals("Exporting..........")) {
text = "Exporting.";
} else {
text += ".";
}
@ggalmazor
ggalmazor / ExportPanelForm.java
Last active March 20, 2018 06:25
Indirection without abstraction - Snippet 2
public class ExportPanelForm {
private static final String EXPORTING = "Exporting";
private static final String DOT = ".";
private JLabel exportingLabel;
public void updateExportingLabel() {
String text = exportingLabel.getText();
if (text.equals(EXPORTING + DOT + DOT + DOT + DOT + DOT + DOT + DOT + DOT + DOT + DOT)) {
text = EXPORTING + DOT;
} else {