Android(Java) Producer/Consumer with Timer - android

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.).

Related

Many postDelayed handlers with different delays (around 2.000)

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();

Stop thread in android application

public class CallEvent extends BroadcastReceiver{
public LEDController ledController = new LEDController();
public ApplicationSettings applicationSettings = new ApplicationSettings();
public boolean ring = false;
#Override
public void onReceive(Context context, Intent intent){
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
ring = true;
blink();
}else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) ||
intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
ring = false;
}
}
public void blink(){
Runnable r = new Runnable() {
#Override
public void run() {
while(ring){
ledController.turnOnFlash();
try {
Thread.sleep(applicationSettings.getDelayOn());
} catch (InterruptedException e) {
e.printStackTrace();
}
ledController.turnOffFlash();
try {
Thread.sleep(applicationSettings.getDelayOff());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread blinkThread = new Thread(r);
blinkThread.start();
}
}
I want to create led messenger when phone is ring. But I can't stop blinkThread. I dont know it is not working. Led start when call income but don't stop when call decline. Variable ring is changing on 'false' when call decline, but thread still working
In general, you don't forcibly stop threads because it's dangerous. You set a flag that tells the thread in question to exit from it's thread loop under controlled circumstances.
Your thread loop looks something along these lines:
void run() {
while (shouldContinue) {
doThreadWorkUnit();
}
}
And somewhere else you set the shouldContinue variable and wait for the thread to finish:
...
thread.shouldContinue = false;
thread.join();
...
(All this is likely not correct Java, since I don't do Java. View it as pseudo code and modify for your actual language/thread library/etc.)
Source: How to stop a 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 update UI from Thread (Handler maybe not an option?)

This is a common question, and I have read up on the various ways of handling it, but each on seems to fall short for what I am trying to do, which is essentially be a good OO-Citizen.
I have an Activity that invokes a CommunicationManager, which basically polls a TCP socket for data. When the CommunicationManager receives data, it throws a custom event (containing the string it just fetched), which is handled by the Activity. I am doing this, A) because other classes will depend on that data, not just the Activity, and B) because the polling is asynchronous, and should fire an event when it receives results.
My problem lies in that I need to surface those results into a TextView on the UI. I have the polling mechanism all set up, it fires every 1000ms, and invokes the event handler on the Activity. However, the UI never updates.
Assumedly this is a thread issue and the UI thread is not the one getting the change to the TextView, but how do I do this?? I have tried using a Handler, but am not sure where to put it, and when I did get it compiling it never updated the UI.
This seems relatively trivial if everything was done within the Activity, but adding in this other class (CommunicationManager) and the event is making it very confusing for me.
Here is what I have so far:
ACTIVITY (polling is invoked by clicking a button on the UI):
public void onClick(View v) {
if (v.getId() == R.id.testUDPBtn) {
statusText.setText("");
commMgr = new CommunicationManager();
commMgr.addEventListener(this);
MediaPositionPollThread poller = new MediaPositionPollThread(commMgr);
poller.startPolling();
}
}
#Override
public void handleMediaPositionFoundEvent(MediaPositionFoundEvent e) {
statusText.append(e.userData);
}
THREAD:
class MediaPositionPollThread extends Thread {
private CommunicationManager commManager;
private static final String TAG = "MediaPositionPollThread";
private boolean isPolling = false;
public MediaPositionPollThread(CommunicationManager cm) {
commManager = cm;
}
public void startPolling() {
isPolling = true;
this.run();
}
public void stopPolling() {
isPolling = false;
}
#Override
public void run() {
while (isPolling) {
try {
commManager.getCurrentMediaPosition();
Thread.sleep(1000);
}
catch (InterruptedException e) {
Log.d(TAG, "EXCEPTION: " + e.getMessage());
}
}
}
}
COMMUNUCATION MANAGER:
public void getCurrentMediaPosition() {
PrintWriter outStream;
BufferedReader inStream;
String resultString = "";
try {
outStream = new PrintWriter(tcpSocket.getOutputStream(), true);
outStream.println("GET?current_pts");
inStream = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
resultString = inStream.readLine();
fireEventWithData(resultString);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void addEventListener(MediaPositionFoundEventListener listener) {
_listeners.add(listener);
}
public synchronized void removeEventListener(MediaPositionFoundEventListener listener) {
_listeners.remove(listener);
}
private synchronized void fireEventWithData(String outputString) {
MediaPositionFoundEvent evt = new MediaPositionFoundEvent(this);
evt.userData = outputString;
Iterator<MediaPositionFoundEventListener> i = _listeners.iterator();
while(i.hasNext()) {
((MediaPositionFoundEventListener) i.next()).handleMediaPositionFoundEvent(evt);
}
}
So I have the Activity making a thread that gets executed every second, calling CommunicationManager >> getCurrentMediaPosition, which in turn fires the MediaPositionFoundEvent, which is handled by the Activity and updates the TextView (statusText) on the screen.
Everything works except the screen not updating. I have tried runOnUiThread, and a Handler, but am obviously not getting it right.
Thanks in advance for any insight or solutions!
In your Activity class, add a private Handler _handler,
Initialize it in your onCreate Activity method,
and change your handleMediaPositionFoundEvent method to
#Override public void handleMediaPositionFoundEvent(MediaPositionFoundEvent e) {
_handler.post(new Runnable(){
public void run(){
statusText.append(e.userData);
});
}
}
It looks like your blocking the UI thread with your custom Thread. Please update this method to call start() vs run().
public void startPolling() {
isPolling = true;
this.start();
}

how to use notify and wait

Can wait/notify be used within one thread?
I'm mean I have a listener and in the moment when that listener gets called I wanna enable a thread to do his work.How could I do that?
UPDATE:My data is written in a database...and is written each time the listener is called.Now the thread that I've created reads that data and sends it somewhere....
Next...I get some other data and do the same thing....The other thread needs to know what was the last data he read it so he can start reading from where he left....
Take a look in here:
using wait and notify within one thread
This is how my problem looks like.Thx
I have the following:
synchronized (syncToken)
{
try {
syncToken.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("MyThread: " + s);
in MyThread....so when I do
MyThread t = new MyThread(syncToken);
t.start();
I put my thread on waiting...yes?
And when I do this:
syncToken.notify();
I get my thread back on track....but the execution of the next line is the one after wait()?
I mean this: System.out.println("MyThread: " + s); ????
When u notify a thred does he continues his execution with the line after wait()???Thx
The following is a simple example of concurrency between two different threads. In the example the main thread start a MyThread thread and every 3 seconds it sets a data to the MyThread instance and then MyThread prints it. The idea is to have a synchronized object that you wait on it and notify in the end of the usage to other threads that they can use it:
Test.java:
package stack;
public class Test {
public static void main (String args[])
{
Object syncToken = new Object();
MyThread t = new MyThread(syncToken);
t.start();
for (int i = 0; i < 10; i++)
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(syncToken)
{
t.setText("Iteration " + i);
syncToken.notify();
}
}
}
}
MyThread.java:
package stack;
public class MyThread extends Thread{
String s;
Object syncToken;
public MyThread(Object syncToken)
{
this.s = "";
this.syncToken = syncToken;
}
public void run()
{
while(true) // you will need to set some condition if you want to stop the thread in a certain time...
{
synchronized (syncToken)
{
try {
syncToken.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("MyThread: " + s);
}
}
public void setText(String s)
{
this.s = s;
}
}
In this example, the main thread sets a string (every 3 seconds) and the MyThread thread prints it.
Adapt it to your needs, it shouldn't be too hard.
I had similar problem. I created an arbiter used by two threads (in your case it can be listeners thread and your task thread):
listener:
arbiter.waitConsumer();
// prepare data
arbiter.dataLoaded();
task thread:
while(true){
arbiter.waitProducer();
// consume data
arbiter.dataConsumed();
}
arbiter:
public class Arbiter {
private boolean dataLoaded = false;
public synchronized void waitProducer(){
while(!dataLoaded){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void waitConsumer(){
while(dataLoaded){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void dataLoaded(){
dataLoaded = true;
notify();
}public synchronized void dataConsumed(){
dataLoaded = false;
notify();
}}
Listener and task will synchronize themselfes against arbiters monitor. Probably you can call your arbiter queue or pipe and store date for consuming in it?

Categories

Resources