Multithreading using sockets
up vote
0
down vote
favorite
I'm developing an App that is meant to receive weight measures from industrial scales, one, two or more scales. The weight of each scale is sent through a RS232 interface continuously (I receive every 100 milliseconds the weight from the scale, this means that idle time is low. Once I start receiving the weight from the scale it doesn't stop) using a WiFi Serial adapter. I'm using a TCP Connection Socket and it works fine if I try to get the weight one scale at a time. What I try to achieve is this: I want to listen to two or four scales at the same time and update the UI in real time as I get the weight from the scale. (If I read the weight one scale at the time it works fine). I tried multithreading, ThreadPoolExecutor, ThreadHandlers, etc but I got several errors (pretty sure I didn't implement them correctly I'm new to multithreading). This is the code at the moment:
The Connection Class receives the weight from the scales. I communicate the weight received through an interface to the other classes
public class Connection implements Runnable {
public static final String LOG_TAG = Connection.class.getSimpleName();
public interface ConnectionDefinition {
public ConnectionReader createReader(int timeout) throws IOException;
public String getNameOrAddress();
}
public interface Listener {
public void onException(Exception e);
public void postWeight(double weight, boolean steady);
}
private ConnectionReader m_Reader;
private Listener m_Listener = s_NullListener;
private final ConnectionDefinition m_ConnectionDefinition;
private volatile boolean m_Continue = false;
private static final int s_Timeout = 5000;
public Connection(final ConnectionDefinition connectionDef) {
m_ConnectionDefinition = connectionDef;
start();
}
@Override
public void run() {
m_Continue = true;
while (m_Continue) {
try {
m_Reader = m_ConnectionDefinition.createReader(s_Timeout);
final Result result = new Result();
while (m_Continue) {
m_Reader.read(result);
final double weight = result.weight;
final boolean steady = result.steady;
Log.e(LOG_TAG, "Connection!!! " + String.valueOf(weight));
m_Listener.postWeight(weight, steady);
}
} catch (final IOException e) {
handleException(e);
} finally {
if (m_Reader != null)
try {
m_Reader.close();
} catch (final IOException ignored) {
// ignored
}
m_Reader = null;
}
}
}
private void handleException(final Exception e) {
if (m_Continue) {
m_Listener.onException(e);
try {
Thread.sleep(s_Timeout);
} catch (final InterruptedException e1) {
}
}
}
public void start() {
if (running())
throw new IllegalStateException("Connection already running");
final String name = m_ConnectionDefinition.getNameOrAddress();
//Log.e(LOG_TAG, "Estoy en START! ");
new Thread(this, "Scale Connection: " + name).start();
}
public void stop() {
m_Continue = false;
if (m_Reader != null) {
try {
m_Reader.stop();
} catch (final IOException e) {
}
}
//Log.e(LOG_TAG, "Estoy en STOP! ");
}
public boolean running() {
return m_Continue;
}
public void setListener(final Listener listener) {
if (listener != null)
m_Listener = listener;
else
m_Listener = s_NullListener;
}
public void removeListener() {
m_Listener = s_NullListener;
}
public ConnectionDefinition connectionDefinition() {
return m_ConnectionDefinition;
}
private static Listener s_NullListener = new Listener() {
@Override
public void onException(final Exception e) {
}
@Override
public void postWeight(final double weight, final boolean steady) {
}
};
}
I created a method, where I try to listen to the different weight measures and Update the UI (this method works with one listener at the time. It updates the UI correctly):
private void connectionListener(){
/*I GET THE CONNECTIONS DEFINED*/
final List<ConnectionDefinition> connectionDefinition = Persistor.getInstance().connectionDefinitions();
final ConnectionDefinition conDef = connectionDefinition.get(0);
/*MY APPROACH IS TO CREATE RUNNABLES TO PUT THE TO WORK IN DIFFERENT THREATS. UNTIL NOW IT DIDNT WORK*/
runnable1 = (new Runnable() {
final Connection connection1 = new Connection(conDef);
@Override
public void run() {
if(!connection1.running()) connection1.start();
connection1.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 1 !!!");
inputPesoRuedaDelanteraIzquierda.setText(String.valueOf(weight));
//connection1.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
runnable2 = (new Runnable() {
final Connection connection2 = new Connection(conDef);
@Override
public void run() {
if(!connection2.running()) connection2.start();
connection2.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 2 !!!");
inputPesoRuedaDelanteraDerecha.setText(String.valueOf(weight));
//connection2.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
worker = new WorkerThreadPool();
worker.execute(runnable1);
worker.execute(runnable2);
/*
Executor executor = new ThreadPerTaskExecutor();
executor.execute(runnable1);
executor.execute(runnable2);
*/
//new Thread(runnable1).start();
//new Thread(runnable2).start();
}
As you can see I tried different approaches. I need help to implement the correct one I'm open to other ideas (Maybe listener iterators, even though I like the idea of multithreading)
java android multithreading sockets listener
add a comment |
up vote
0
down vote
favorite
I'm developing an App that is meant to receive weight measures from industrial scales, one, two or more scales. The weight of each scale is sent through a RS232 interface continuously (I receive every 100 milliseconds the weight from the scale, this means that idle time is low. Once I start receiving the weight from the scale it doesn't stop) using a WiFi Serial adapter. I'm using a TCP Connection Socket and it works fine if I try to get the weight one scale at a time. What I try to achieve is this: I want to listen to two or four scales at the same time and update the UI in real time as I get the weight from the scale. (If I read the weight one scale at the time it works fine). I tried multithreading, ThreadPoolExecutor, ThreadHandlers, etc but I got several errors (pretty sure I didn't implement them correctly I'm new to multithreading). This is the code at the moment:
The Connection Class receives the weight from the scales. I communicate the weight received through an interface to the other classes
public class Connection implements Runnable {
public static final String LOG_TAG = Connection.class.getSimpleName();
public interface ConnectionDefinition {
public ConnectionReader createReader(int timeout) throws IOException;
public String getNameOrAddress();
}
public interface Listener {
public void onException(Exception e);
public void postWeight(double weight, boolean steady);
}
private ConnectionReader m_Reader;
private Listener m_Listener = s_NullListener;
private final ConnectionDefinition m_ConnectionDefinition;
private volatile boolean m_Continue = false;
private static final int s_Timeout = 5000;
public Connection(final ConnectionDefinition connectionDef) {
m_ConnectionDefinition = connectionDef;
start();
}
@Override
public void run() {
m_Continue = true;
while (m_Continue) {
try {
m_Reader = m_ConnectionDefinition.createReader(s_Timeout);
final Result result = new Result();
while (m_Continue) {
m_Reader.read(result);
final double weight = result.weight;
final boolean steady = result.steady;
Log.e(LOG_TAG, "Connection!!! " + String.valueOf(weight));
m_Listener.postWeight(weight, steady);
}
} catch (final IOException e) {
handleException(e);
} finally {
if (m_Reader != null)
try {
m_Reader.close();
} catch (final IOException ignored) {
// ignored
}
m_Reader = null;
}
}
}
private void handleException(final Exception e) {
if (m_Continue) {
m_Listener.onException(e);
try {
Thread.sleep(s_Timeout);
} catch (final InterruptedException e1) {
}
}
}
public void start() {
if (running())
throw new IllegalStateException("Connection already running");
final String name = m_ConnectionDefinition.getNameOrAddress();
//Log.e(LOG_TAG, "Estoy en START! ");
new Thread(this, "Scale Connection: " + name).start();
}
public void stop() {
m_Continue = false;
if (m_Reader != null) {
try {
m_Reader.stop();
} catch (final IOException e) {
}
}
//Log.e(LOG_TAG, "Estoy en STOP! ");
}
public boolean running() {
return m_Continue;
}
public void setListener(final Listener listener) {
if (listener != null)
m_Listener = listener;
else
m_Listener = s_NullListener;
}
public void removeListener() {
m_Listener = s_NullListener;
}
public ConnectionDefinition connectionDefinition() {
return m_ConnectionDefinition;
}
private static Listener s_NullListener = new Listener() {
@Override
public void onException(final Exception e) {
}
@Override
public void postWeight(final double weight, final boolean steady) {
}
};
}
I created a method, where I try to listen to the different weight measures and Update the UI (this method works with one listener at the time. It updates the UI correctly):
private void connectionListener(){
/*I GET THE CONNECTIONS DEFINED*/
final List<ConnectionDefinition> connectionDefinition = Persistor.getInstance().connectionDefinitions();
final ConnectionDefinition conDef = connectionDefinition.get(0);
/*MY APPROACH IS TO CREATE RUNNABLES TO PUT THE TO WORK IN DIFFERENT THREATS. UNTIL NOW IT DIDNT WORK*/
runnable1 = (new Runnable() {
final Connection connection1 = new Connection(conDef);
@Override
public void run() {
if(!connection1.running()) connection1.start();
connection1.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 1 !!!");
inputPesoRuedaDelanteraIzquierda.setText(String.valueOf(weight));
//connection1.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
runnable2 = (new Runnable() {
final Connection connection2 = new Connection(conDef);
@Override
public void run() {
if(!connection2.running()) connection2.start();
connection2.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 2 !!!");
inputPesoRuedaDelanteraDerecha.setText(String.valueOf(weight));
//connection2.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
worker = new WorkerThreadPool();
worker.execute(runnable1);
worker.execute(runnable2);
/*
Executor executor = new ThreadPerTaskExecutor();
executor.execute(runnable1);
executor.execute(runnable2);
*/
//new Thread(runnable1).start();
//new Thread(runnable2).start();
}
As you can see I tried different approaches. I need help to implement the correct one I'm open to other ideas (Maybe listener iterators, even though I like the idea of multithreading)
java android multithreading sockets listener
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm developing an App that is meant to receive weight measures from industrial scales, one, two or more scales. The weight of each scale is sent through a RS232 interface continuously (I receive every 100 milliseconds the weight from the scale, this means that idle time is low. Once I start receiving the weight from the scale it doesn't stop) using a WiFi Serial adapter. I'm using a TCP Connection Socket and it works fine if I try to get the weight one scale at a time. What I try to achieve is this: I want to listen to two or four scales at the same time and update the UI in real time as I get the weight from the scale. (If I read the weight one scale at the time it works fine). I tried multithreading, ThreadPoolExecutor, ThreadHandlers, etc but I got several errors (pretty sure I didn't implement them correctly I'm new to multithreading). This is the code at the moment:
The Connection Class receives the weight from the scales. I communicate the weight received through an interface to the other classes
public class Connection implements Runnable {
public static final String LOG_TAG = Connection.class.getSimpleName();
public interface ConnectionDefinition {
public ConnectionReader createReader(int timeout) throws IOException;
public String getNameOrAddress();
}
public interface Listener {
public void onException(Exception e);
public void postWeight(double weight, boolean steady);
}
private ConnectionReader m_Reader;
private Listener m_Listener = s_NullListener;
private final ConnectionDefinition m_ConnectionDefinition;
private volatile boolean m_Continue = false;
private static final int s_Timeout = 5000;
public Connection(final ConnectionDefinition connectionDef) {
m_ConnectionDefinition = connectionDef;
start();
}
@Override
public void run() {
m_Continue = true;
while (m_Continue) {
try {
m_Reader = m_ConnectionDefinition.createReader(s_Timeout);
final Result result = new Result();
while (m_Continue) {
m_Reader.read(result);
final double weight = result.weight;
final boolean steady = result.steady;
Log.e(LOG_TAG, "Connection!!! " + String.valueOf(weight));
m_Listener.postWeight(weight, steady);
}
} catch (final IOException e) {
handleException(e);
} finally {
if (m_Reader != null)
try {
m_Reader.close();
} catch (final IOException ignored) {
// ignored
}
m_Reader = null;
}
}
}
private void handleException(final Exception e) {
if (m_Continue) {
m_Listener.onException(e);
try {
Thread.sleep(s_Timeout);
} catch (final InterruptedException e1) {
}
}
}
public void start() {
if (running())
throw new IllegalStateException("Connection already running");
final String name = m_ConnectionDefinition.getNameOrAddress();
//Log.e(LOG_TAG, "Estoy en START! ");
new Thread(this, "Scale Connection: " + name).start();
}
public void stop() {
m_Continue = false;
if (m_Reader != null) {
try {
m_Reader.stop();
} catch (final IOException e) {
}
}
//Log.e(LOG_TAG, "Estoy en STOP! ");
}
public boolean running() {
return m_Continue;
}
public void setListener(final Listener listener) {
if (listener != null)
m_Listener = listener;
else
m_Listener = s_NullListener;
}
public void removeListener() {
m_Listener = s_NullListener;
}
public ConnectionDefinition connectionDefinition() {
return m_ConnectionDefinition;
}
private static Listener s_NullListener = new Listener() {
@Override
public void onException(final Exception e) {
}
@Override
public void postWeight(final double weight, final boolean steady) {
}
};
}
I created a method, where I try to listen to the different weight measures and Update the UI (this method works with one listener at the time. It updates the UI correctly):
private void connectionListener(){
/*I GET THE CONNECTIONS DEFINED*/
final List<ConnectionDefinition> connectionDefinition = Persistor.getInstance().connectionDefinitions();
final ConnectionDefinition conDef = connectionDefinition.get(0);
/*MY APPROACH IS TO CREATE RUNNABLES TO PUT THE TO WORK IN DIFFERENT THREATS. UNTIL NOW IT DIDNT WORK*/
runnable1 = (new Runnable() {
final Connection connection1 = new Connection(conDef);
@Override
public void run() {
if(!connection1.running()) connection1.start();
connection1.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 1 !!!");
inputPesoRuedaDelanteraIzquierda.setText(String.valueOf(weight));
//connection1.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
runnable2 = (new Runnable() {
final Connection connection2 = new Connection(conDef);
@Override
public void run() {
if(!connection2.running()) connection2.start();
connection2.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 2 !!!");
inputPesoRuedaDelanteraDerecha.setText(String.valueOf(weight));
//connection2.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
worker = new WorkerThreadPool();
worker.execute(runnable1);
worker.execute(runnable2);
/*
Executor executor = new ThreadPerTaskExecutor();
executor.execute(runnable1);
executor.execute(runnable2);
*/
//new Thread(runnable1).start();
//new Thread(runnable2).start();
}
As you can see I tried different approaches. I need help to implement the correct one I'm open to other ideas (Maybe listener iterators, even though I like the idea of multithreading)
java android multithreading sockets listener
I'm developing an App that is meant to receive weight measures from industrial scales, one, two or more scales. The weight of each scale is sent through a RS232 interface continuously (I receive every 100 milliseconds the weight from the scale, this means that idle time is low. Once I start receiving the weight from the scale it doesn't stop) using a WiFi Serial adapter. I'm using a TCP Connection Socket and it works fine if I try to get the weight one scale at a time. What I try to achieve is this: I want to listen to two or four scales at the same time and update the UI in real time as I get the weight from the scale. (If I read the weight one scale at the time it works fine). I tried multithreading, ThreadPoolExecutor, ThreadHandlers, etc but I got several errors (pretty sure I didn't implement them correctly I'm new to multithreading). This is the code at the moment:
The Connection Class receives the weight from the scales. I communicate the weight received through an interface to the other classes
public class Connection implements Runnable {
public static final String LOG_TAG = Connection.class.getSimpleName();
public interface ConnectionDefinition {
public ConnectionReader createReader(int timeout) throws IOException;
public String getNameOrAddress();
}
public interface Listener {
public void onException(Exception e);
public void postWeight(double weight, boolean steady);
}
private ConnectionReader m_Reader;
private Listener m_Listener = s_NullListener;
private final ConnectionDefinition m_ConnectionDefinition;
private volatile boolean m_Continue = false;
private static final int s_Timeout = 5000;
public Connection(final ConnectionDefinition connectionDef) {
m_ConnectionDefinition = connectionDef;
start();
}
@Override
public void run() {
m_Continue = true;
while (m_Continue) {
try {
m_Reader = m_ConnectionDefinition.createReader(s_Timeout);
final Result result = new Result();
while (m_Continue) {
m_Reader.read(result);
final double weight = result.weight;
final boolean steady = result.steady;
Log.e(LOG_TAG, "Connection!!! " + String.valueOf(weight));
m_Listener.postWeight(weight, steady);
}
} catch (final IOException e) {
handleException(e);
} finally {
if (m_Reader != null)
try {
m_Reader.close();
} catch (final IOException ignored) {
// ignored
}
m_Reader = null;
}
}
}
private void handleException(final Exception e) {
if (m_Continue) {
m_Listener.onException(e);
try {
Thread.sleep(s_Timeout);
} catch (final InterruptedException e1) {
}
}
}
public void start() {
if (running())
throw new IllegalStateException("Connection already running");
final String name = m_ConnectionDefinition.getNameOrAddress();
//Log.e(LOG_TAG, "Estoy en START! ");
new Thread(this, "Scale Connection: " + name).start();
}
public void stop() {
m_Continue = false;
if (m_Reader != null) {
try {
m_Reader.stop();
} catch (final IOException e) {
}
}
//Log.e(LOG_TAG, "Estoy en STOP! ");
}
public boolean running() {
return m_Continue;
}
public void setListener(final Listener listener) {
if (listener != null)
m_Listener = listener;
else
m_Listener = s_NullListener;
}
public void removeListener() {
m_Listener = s_NullListener;
}
public ConnectionDefinition connectionDefinition() {
return m_ConnectionDefinition;
}
private static Listener s_NullListener = new Listener() {
@Override
public void onException(final Exception e) {
}
@Override
public void postWeight(final double weight, final boolean steady) {
}
};
}
I created a method, where I try to listen to the different weight measures and Update the UI (this method works with one listener at the time. It updates the UI correctly):
private void connectionListener(){
/*I GET THE CONNECTIONS DEFINED*/
final List<ConnectionDefinition> connectionDefinition = Persistor.getInstance().connectionDefinitions();
final ConnectionDefinition conDef = connectionDefinition.get(0);
/*MY APPROACH IS TO CREATE RUNNABLES TO PUT THE TO WORK IN DIFFERENT THREATS. UNTIL NOW IT DIDNT WORK*/
runnable1 = (new Runnable() {
final Connection connection1 = new Connection(conDef);
@Override
public void run() {
if(!connection1.running()) connection1.start();
connection1.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 1 !!!");
inputPesoRuedaDelanteraIzquierda.setText(String.valueOf(weight));
//connection1.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
runnable2 = (new Runnable() {
final Connection connection2 = new Connection(conDef);
@Override
public void run() {
if(!connection2.running()) connection2.start();
connection2.setListener(new Connection.Listener() {
@Override
public void postWeight(final double weight, final boolean steady) {
mHandler1.post(new Runnable() {
public void run() {
if(!Double.isNaN(weight)){
Log.e(LOG_TAG, "Runnable 2 !!!");
inputPesoRuedaDelanteraDerecha.setText(String.valueOf(weight));
//connection2.stop();
}
}
});
}
@Override
public void onException(final Exception e) {
e.printStackTrace();
//showMessage("Reintentando en 5 segs...");
}
});
/*
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
});
worker = new WorkerThreadPool();
worker.execute(runnable1);
worker.execute(runnable2);
/*
Executor executor = new ThreadPerTaskExecutor();
executor.execute(runnable1);
executor.execute(runnable2);
*/
//new Thread(runnable1).start();
//new Thread(runnable2).start();
}
As you can see I tried different approaches. I need help to implement the correct one I'm open to other ideas (Maybe listener iterators, even though I like the idea of multithreading)
java android multithreading sockets listener
java android multithreading sockets listener
edited 2 days ago
Kling Klang
32k156286
32k156286
asked 2 days ago
Gonzalo Ortellado
17335
17335
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238305%2fmultithreading-using-sockets%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password