473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Producer / Consumer problems

I'm getting an IllegalMonitorStateException in trying to implement the
above subject. The static declararions made it work, so if this is
part of the problem let me know. I think it has something to do with
the synchronization. I think I'm accessing the bin when I'm not
supposed to be.

**This IS a homework assignment, FYI, and we are using the 1.4.2 API**
So it does have to sleep for a random time, it does have to use as
many consumers as specified as well as number of items. The bin is
either a stack or queue, but that is irrelevant. It does have to
output the exact string and it has to watch for the stop button on the
frame.

Thanks if you can help.

Here's the consumer, which is much like the producer:

// Class Consumer
class Consumer extends Thread
{
// Class data
private int maxItems, currItem, numCons;
private boolean isInterrupted = false;
private static XBin bin;

// Constructor
public Consumer()
{
bin = XThread.bin;
maxItems = ((Number) XThread.sItems.getValue()).intValue();
currItem = 0;
numCons = ((Number) XThread.sConsumer.getValue()).intValue();
isInterrupted = false;
}

// Thread run
public void run()
{
// Catch system interrupts
try {
// Make bin thread-safe
synchronized(bin) {
// Do until requested items produced
while(currItem < maxItems) {
// watch for requested interrupt (stop button)
while (!isInterrupted) {

// Do thread action for as many consumers as
requested
for (int x = 0; x < numCons && !isInterrupted;
x++) {

// Sleep random time
sleep((int)Math.random() * 1000);
// call wait() if nothing to do
while (currItem < maxItems &&
bin.isEmpty()) {
wait();
}
// Notify all when finished
if (currItem >= maxItems &&
bin.isEmpty()) {
notifyAll();
return;
}

// Append Output string
XThread.text.append("item " );
XThread.text.append(new
Integer(currItem).toString());
XThread.text.append(" taken by
Consumer ");
XThread.text.append(new
Integer(x).toString());
XThread.text.append(": ");
bin.printBin();
XThread.text.append("\n");

// Remove Item
bin.pop();

// Release thread and go back to sleep
notifyAll();

currItem++;

} // end for loop

} // number of items produced completed

} // Stop button check

} // End synchronized block

// System interrupt
} catch (InterruptedException it) {
return;
}

} // end run()

// Stop button pressed
public void interrupt() {
isInterrupted = true;
}

}
And for reference, the following:

// Main GUI Code for XThead interface
class XThread extends JFrame {

// Storage bin
static XBin bin;

// threads
private Thread pro;
private Thread con;

// GUI Components
static JToolBar toolBar;
static JPanel mPanel;
static JPanel pcPanel;
static JPanel bPanel;
static JButton button;
static JSpinner sConsumer;
static JSpinner sProducer;
static JSpinner sBin;
static JSpinner sItems;
static JComboBox boxType;

// Output text area
static JTextArea text;

...
Jul 17 '05 #1
0 7436

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Evan Simpson | last post by:
WEBoggle needs a new game board every three minutes. Boards take an unpredictable (much less than 3min, but non-trivial) amount of time to generate. The system is driven by web requests, and I...
2
by: ian douglas | last post by:
I have one process that will be multi-threaded. The parent (A) will sit and deal with TCP/IP issues, and feed data to its child process (B) via shared memory. I need assistance in finding a good...
3
by: smith4894 | last post by:
Hello, I have an application that essentially consists of two threads doing their things. One thread is a producer, and pushes bytes (of a struct) into a pipe, and another is a consumer that...
3
by: MrNobody | last post by:
I am developing an application that has several multi threaded tasks where one thread is doing IO and another thread is grabbing data from the first thread to process it further. I've been...
0
by: Kyle Rowe | last post by:
class Buffer { const int size = 4; int n = 0; public void Put(char ch) { lock(this) { while (n == size) Monitor.Wait(this);
2
by: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is...
2
by: Rene Ruppert | last post by:
Hi, I'm trying to implement the Producer-Consumer-Problem in C#. Below is my code. The problem is, that the buffer always contains only one element...it seems that the Thread.Sleep() in the...
4
by: mps | last post by:
It seems to me that the MSDN code for synchronizing a producer and consumer thread at http://msdn2.microsoft.com/en-us/library/yy12yx1f.aspx is completely wrong. There is no reason to assume that...
10
by: George Sakkis | last post by:
I'd like some feedback on a solution to a variant of the producer- consumer problem. My first few attempts turned out to deadlock occasionally; this one seems to be deadlock-free so far but I can't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.