Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:49:24+00:00 2026-05-24T17:49:24+00:00

I am testing a simple producer/ consumer example in android this is what i’m

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:49:24+00:00Added an answer on May 24, 2026 at 5:49 pm

    You should use a blocking queue to communicate between producer-consumer threads in more efficient and easy to understand way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple website I'm testing. It's running on localhost and I can
I think this is going to be one of those simple-when-you-see-it problems, but it
I have a simple java maven project. One of my classes when executing needs
I have some simple code that is comparing two float values to illustrate a
I have the following JUnit test. The method that I'm testing is quite simple,
I have a simple Rails 3 app: no models, one view and one controller
im using JPA2 with Hibernate 3.6.x I have made a simple testing on the
I am doing some simple testing of adding CALayer to a UIView. In my
I have some simple code: procedure TForm1.Button1Click(Sender:TObject); var x: RawByteString; begin x := UTF8Encode('testing
This is probably a simple question. I have a database that I'm using to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.