Many postDelayed handlers with different delays (around 2.000) - android

My app receives messages and has to schedule multiple replies one after another each with a different delay. What is the best way to have about 2000 delayed tasks with different delays? It seems like Android can't run that much handlers at the same time due to limited ressources.
Any ideas?
Thanks in advance.
Code simplified:
class TimedTask extends TimerTask { //runs every 10 seconds and checks for new contacts in queue to reply to (contacts can go up to 500 ones)
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(SendMessagesService.this);
String contacts = sp.getString("contacts", ""); //queued by another Thread
String contacts_array[] = contacts.split(",");
for (String contact : contacts_array) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String message = sp.getString("message1",""); //there are different messages with different delays, depending on which were already sent
ReceiveContactsAdapter mDbHelper = new ReceiveContactsAdapter(SendMessagesService.this);
mDbHelper.open();
mDbHelper.sendMessages(message, contact);
mDbHelper.close();
}
}, delay); //delay depends on which message will be sent
}
}

You can use thread.sleep(milisec) instead. It delays the thread for the particular time.
#Override
public void run()
{
while (!Thread.interrupted())
try
{
Thread.sleep(1000);
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
////
}
});
}
catch (InterruptedException e)
{
}
}
})).start();

Related

Get counter value from server and update in Textview

I have a server giving me live data in JSON format which updates every second. I have to display that in my android app.
I am a beginner and I tried Async Task updating every second via a thread and setting a delay on it.
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Perform the HTTP request for data and process the response.
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
It runs out of memory and crashes after some time
Are there any alternates?
Try putting your code into handler thread
Runnable runnable = new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, 1000);
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
};
// start it with:
mHandler.post(runnable);

TimerTask / Handler with Delay getting called Multiple times

I need a delay for around 5 seconds. I have tried using Timer using below code :
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
Log.d(TAG,"Timer");
}
}, 4000, 5000);
When i check logs, the Timer is getting printed thrice. If I change time, sometimes it gets printed in log 4 times as well.
I have tried using Handler as well like below :
final Handler mHandler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Log.d(Utility.TAG,"Sleep::");
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
But again the log is printing multiple times. I just want to call my method once not multiple times. How can I achieve it ?
EDIT
used handler without thread as well like below :
final Handler h = new Handler();
final int delay = 3000; //milliseconds
h.postDelayed(new Runnable(){
public void run(){
//do something
h.postDelayed(this, delay);
Log.d(Utility.TAG,"Sleep ::");
}
}, delay);
But again, Log is getting printed thrice
Your third approach (no Timer, no Thread) is the closest to being correct. It's printing multiple times because the Runnable is re-posting itself every time it runs. If you only want it to run once, remove this line from the run() method:
h.postDelayed(this, delay);

Android thread encapsulation, cannot get changing variables from the encapsulated class

I am quite new to Android and Java. Basically, I would like to realize an encapsulation of a background thread of Android, and inside this background thread, I have an infinite loop which will periodically take some operations of getting data(like from Internet, or from some hardware devices).
The encapsulated class must provide only a function like getData() for others to get data. But everytime when i call this getData() function from other classes, it never gives me the changing values, but only the initialized values.
I've studied both of the AsyncTask, Handler and Message ways to realize multithread. And both of them give me the initialized values.
Here is the encapsulated class of Handler and Message:
public class getDataFromUSB{
private int usb_data;
private Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
usb_data=msg.arg1;
}
}
};
private Thread thread = new Thread(){
#Override
public void run(){
while(!Thread.currentThread().isInterrupted())
{
int a=read_usb();
Message msg = new Message();
msg.arg1 = a;
msg.what = 1;
handler.sendMessage(msg);
try {
Thread.sleep(15);
} catch (InterruptedException e) {
System.err.println("");
this.interrupt();
}
}
}
};
public void start(){
thread.start();
}
public int get_data(){
return usb_data;
}
public int read_usb()
{
int a=10;
return a;
}
}
And then in another class, here is the code of calling getDataFromUSB:
getDataFromUSB usb1= new getDataFromUSB();
usb1.start();
int a=usb1.getData();
Log.e(TAG,"a = " +a);
Then everytime i call this usb1.getData(), the value is always 0. I don't understand why.
Now I proceed to do some more realistic things. I add an object of random in my getDataFrom USB class to provide different numbers, I also change the way of assigning values to usb_data, I think it's better to do it just in the background thread, there is no need to move it to the handlemessage. So it becomes:
public class getDataFromUSB{
private int usb_data;
private Random random = new Random(555L);
private Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
private Thread thread = new Thread(){
#Override
public void run(){
while(!Thread.currentThread().isInterrupted())
{
int a=read_usb();
usb_data=a;
Message msg = new Message();
handler.sendMessage(msg);
}
}
};
public void start(){
thread.start();
}
public int get_data(){
return usb_data;
}
public int read_usb()
{
return random.nextInt();
}
}
Then I call it from another class like what Nikita suggested:
Handler h = new Handler();
for (int i=0;i<20;i++){
h.postDelayed(new Runnable() {
public void run() {
int data=usb1.get_data();
Log.e(TAG,"data= " +data);
}
},500);
}
The strange thing is that it then gives sometimes all the same numbers, sometimes several some numbers, sometimes all different numbers. As I understand, the usb_data has always been changed inside the background thread, so we are not obliged to wait for handlemessage to proceed. Everytime I call getData(), it should give me the newest value. Isn't that right?
The problem might be following: you run your code from main UI thread. When you new start thread - it sends message to handler. This message is added to main thread's queue and will be processed in main thread when it finishes it's current job. Currently main thread runs your code and there is no chance that message will be processed before you call usb.getData().
To check whether your update thread works properly you can post delayed runnable that will print value of usb1.getData():
final getDataFromUSB usb1= new getDataFromUSB();
usb1.start();
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
int a=usb1.getData();
Log.e(TAG,"a = " +a);
}
}, 500); // Waits 500 milliseconds and runs runnable on current thread.

how to create a thread to refresh data in 3 second interval

I need a thread (it does httppost ,and parse the answer xml and refresh listview to set the changes from parsed xml) in 3 sec interval
I have already tried this code
Timer timer = new Timer();
timer.scheduleAtFixedRate(
new TimerTask() {
public void run() {
try {
httpPostList(url);
saxParseList();
list.invalidateViews();
Thread.sleep(1000);
} catch (Exception ie) {
}
}
}, 1000, 1000 * 30);
I would appreciate you to create a Service with an AsyncTask in it.
Async Tasks are the Android Synonym to normal Java Tasks, Documentation finding here: http://developer.android.com/reference/android/os/AsyncTask.html
Services are Background Processes, seeing this Doc:
http://developer.android.com/reference/android/app/Service.html
Try using handlers:
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
updateUI();
}
};
Thread thread = new Thread() {
#Override
public void run() {
while(true) {
Message msg = new Message();
handler.sendMessage(msg);
try {
sleep(3*1000); // 3 seconds
} catch (InterruptedException e) {
}
}
}
};
thread.start();
}
private synchronized void updateUI() {
// ...
}
Finally I made it using "Async task".

Android(Java) Producer/Consumer with Timer

I am testing a simple producer/ consumer example in android this is what i'm doing.
I have two EditText boxes, one being a producer and the other a consumer. The app also has a single button once this button is pressed two timers start and the producer produces while the consumer consumes. Here is my code:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer producerTimer = new Timer();
producerTimer .schedule(new TimerTask(){
#Override
public void run(){
producer();
}
},100, 300);
Timer consumerTimer = new Timer();
consumerTimer .schedule(new TimerTask(){
#Override
public void run(){
consumer();
}
},100, 300);
}
});
}
Now for the other methods:
public void producer(){
this.runOnUiThread(producer_Tick);
}
public void consumer(){
this.runOnUiThread(consumer_Tick2);
}
private Runnable producer_Tick = new Runnable(){
public void run(){
put(i++);
}
};
private Runnable consumer_Tick= new Runnable(){
public void run(){
int result = get();
consumerBox.append(Integer.toString(result) + "\n");
}
};
Here are my Synchronized methods:
public synchronized void put(int val){
if (!empty){
try{
wait();
}catch (InterruptedException e) {Log.d(TAG,"Error Putting");}
}
producerBox.append(Integer.toString(val) + "\n");
empty = false;
buffer=val;
notify();
}
public synchronized int get(){
if (empty){
try{
wait();
}catch (InterruptedException e) {Log.d(TAG,"Error getting");}
}
empty = true;
notify();
return buffer;
}
This program runs to random points all the time. Sometimes for a couple of minuites it runs fine where consumer reads from producer etc.. However, everytime, at some point, the program will just freeze at producer and consumer at a certain value (random each time). Does anyone see a problem with the above code?
You should use a blocking queue to communicate between producer-consumer threads in more efficient and easy to understand way.
you should use notifyAll(). notify does not gaurantee which thread is notified. Also check hat empty is actually synchronized (i.e. is owned by the status class where get and put reside.).

Categories

Resources