I'm trying to make a program that monitors network connectivity, one that shows statuses and bandwidth and refreshes every second. Yesterday I learned that network monitoring occurs on a secondary thread, which I created; it now works.
I believe to have the program refresh every second, I do a while-loop, in which the while-condition is always "true", and at the end of the while loop I "try" a Thread.sleep(1000).
I have one question and one problem.
Question: Do I risk flooding my program? I feel like by setting secondaryThread = null, all the data created during the while loop gets garbage collected, but I'm not sure whether this is the case.
Problem: When I run this, I get a message "[Program] has quit unexpectedly"... leading me to think that I am indeed flooding the program. Is there a way to get around this?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int linkSpeed = -1;
TextView textView = new TextView(this);
while (true)
{
SecondaryThread secondaryThread = new SecondaryThread(this);
new Thread(secondaryThread).start();
linkSpeed = secondaryThread.getLinkSpeed();
secondaryThread = null;
// Create the text view
textView.setTextSize(25);
textView.setText("linkspeed = " + linkSpeed);
// Set the text view as the activity layout
setContentView(textView);
try {
Thread.sleep(1000);
}
catch (Exception e) {
textView.setTextSize(25);
textView.setText("oh shit");
}
}
The LogCat trace stack says everything is running fine, even though that's not the actual case. Specifically, it says the following...
06-27 15:07:58.069: D/gralloc_goldfish(1312): Emulator without GPU emulation detected.
06-27 15:41:45.879: I/dalvikvm(1919): threadid=3: reacting to signal 3
06-27 15:41:45.958: I/dalvikvm(1919): Wrote stack traces to '/data/anr/traces.txt'