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

  • SEARCH
  • Home
  • 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 6027653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:37:27+00:00 2026-05-23T04:37:27+00:00

I have to write this produce consumer application using multithreading. I wrote the following

  • 0

I have to write this produce consumer application using multithreading. I wrote the following java code but havn;t been able to figure out where it is getting wrong. Also i want to know whether my class design is apt or if my coding style is appropriate.

Thanks in Advance!!!

EDIT

I have modified the produce consumer code: But it still has some problem.

import java.util.*;
import java.lang.Thread;

public class pc_example {

public static void main (String [] args) {
    Store store = new Store( 10 );
    produce p = new produce(store);
    consume c = new consume (store);
    p.start();
    c.start();
}

}

class Store {
public Queue<Integer> Q;
public int max_capacity;

Store( int max_capacity ) {
    Q = new LinkedList<Integer>();
    this.max_capacity = max_capacity;
}

}

class produce extends Thread {

private Store store;
private int element;

produce ( Store store ) {
    this.store = store;
    this.element = 0;
}



public void put() {
    synchronized (store) {
        if (store.Q.size() > store.max_capacity) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        else {
            element ++;
            System.out.println( "Producer put: " + element );
            store.Q.add(element);
            notify();
        }               
    }           
}

}

class consume extends Thread {
private int cons;
private Store store;

consume (Store store) {
    this.store = store;
    this.cons = 0;
}

public void get() {
    synchronized (store) {
        if (store.Q.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        else {
            int a = store.Q.remove();
            System.out.println( "Consumer put: " + a );
            cons++;

            if (store.Q.size() < store.max_capacity)
                notify();
        }
    }
}

}

  • 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-23T04:37:28+00:00Added an answer on May 23, 2026 at 4:37 am

    For a complete example see the producer-consumer example in the java api for BlockingQueue.


    There are several errors in the code. For the first the producer and the consumer are not using the same queue e.g. there are two instances of the queues. Secondly notify and wait methods are also operating on different objects.

    Getting your example to work needs several things:

    1. Only one queue
    2. Thread safe handling of the queue
    3. Handling notification and waiting on the same object

    The producer code could be rewritten to:

    public void produce() {
        int i = 0;
        while (i < 100) {
            synchronized(Q) {
                if (Q.size() < max_capacity) {
                    Q.add(i);
                    System.out.println("Produced Item" + i);
                    i++;
                    Q.notify();
                } else {
                    try {
                        Q.wait();
                    } catch (InterruptedException e) {
                        System.out.println("Exception");
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to write this in C#.NET, I have not been encountered using the ^
I have this code to write to a file, it works perfect but I
I'm sure I'm going to have to write supporting javascript code to do this.
I'm doing a bit of coding, where I have to write this sort of
I have just upgraded my server 2003 to serrver 2008. As I write this,
I have to write a Matlab script that does this: The input is 2
I have a string of this format 1.0.x.0 I have to write a regex
I have to write an object in to binary file.My struct looks like this.
I have to write a script which will be hosted on differents domains. This
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),

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.