| | |
problem with threads :(
![]() |
•
•
Join Date: Dec 2006
Posts: 43
Reputation:
Solved Threads: 0
Hi Everyone,
I'am currently working on this question:
Write a producer-consumer problem that uses threads and shares a common buffer. However do not use semaphores or any other synchronization primitives to guard the shared data structures. Just let each thread access them when it wants to. Use sleep and wakeup to handle the full and empty conditions. See how long it takes for a fatal race condition to occur. For example you might have the producer print a number once in a while. Do not print that number every minute because the I/O could affect the race conditions.
This is what I managed to do:
My problem is that I cant get my program to give me a output, and i dont know where im supposed to put my wakeup statements....
Thanks for your help in advance,
itchap
I'am currently working on this question:
Write a producer-consumer problem that uses threads and shares a common buffer. However do not use semaphores or any other synchronization primitives to guard the shared data structures. Just let each thread access them when it wants to. Use sleep and wakeup to handle the full and empty conditions. See how long it takes for a fatal race condition to occur. For example you might have the producer print a number once in a while. Do not print that number every minute because the I/O could affect the race conditions.
This is what I managed to do:
Java Syntax (Toggle Plain Text)
import java.nio.*; public class Buffer { private int buffer = -1; public void set(int val) { System.out.println("Writing" + val); buffer = val; } public int get() { System.out.println("Reading" + buffer); return buffer; } }
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.nio.*; public class Consumer extends Thread implements Runnable{ private Buffer sharedBuf; // the buffer is the same buffer used by the producer int sum = 0; // total stuff read from the buffer //..constructor should accept(Buffer), and set it to the sharedBuf ... public Consumer (Buffer sharedBuf){ this.sharedBuf=sharedBuf; } public void threadRun() throws InterruptedException { try{ // read the buffer four times, and keep the total for (int i=1; i<=4; i++) { sleep(1); sum = sum + sharedBuf.get(); } } catch(InterruptedException exception) { exception.printStackTrace(); } System.out.println("Consumer finished, ate " + sum + "exiting..."); } }
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.nio.*; public class Producer extends Thread implements Runnable{ private Buffer sharedBuf; // the buffer is the buffer class above // constructor should accept (Buffer), and set it to the sharedBuf ... public Producer (Buffer sharedBuf){ this.sharedBuf=sharedBuf; } public void threadRun()throws InterruptedException { try{ for (int i=1; i<=4; i++) { sleep(1);//sleep for a random time, between 0-3 seconds for example; sharedBuf.set(i); } } catch (InterruptedException exception) { exception.printStackTrace(); } System.out.println("Producer finished, exiting..."); } }
Java Syntax (Toggle Plain Text)
public class test { public static void main(String args[]) { Buffer shared = new Buffer(); Thread a=new Thread( new Producer(shared)); a.start(); Thread b=new Thread( new Consumer(shared)); b.start(); } }
My problem is that I cant get my program to give me a output, and i dont know where im supposed to put my wakeup statements....
Thanks for your help in advance,
itchap
Either extend Thread OR implement Runnable, no need to do both.
And the method you need is simply called "run()", not "threadRun()".
Your threads are running nicely, they're just not doing anything because the empty run() method in Thread (which you tried to override but didn't) doesn't do anything.
And the method you need is simply called "run()", not "threadRun()".
Your threads are running nicely, they're just not doing anything because the empty run() method in Thread (which you tried to override but didn't) doesn't do anything.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Dec 2006
Posts: 43
Reputation:
Solved Threads: 0
Thanks jwenting
....yeah your right, I fixed the code to this:
my threads are working properly, but, i dont think that they're doing what they are supposed to be, because the consumer is eating 10, and it shouldnt be......can you give me a clue to why it keeps doing that?
thanks once again
itchap
....yeah your right, I fixed the code to this: Java Syntax (Toggle Plain Text)
import java.nio.*; public class Buffer { private int buffer = -1; public void set(int val) { System.out.println("Writing" + val); buffer = val; } public int get() { System.out.println("Reading" + buffer); return buffer; } }
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.nio.*; public class Producer implements Runnable{ private Buffer sharedBuf; // the buffer is the buffer class above // constructor should accept (Buffer), and set it to the sharedBuf ... public Producer (Buffer sharedBuf){ this.sharedBuf=sharedBuf; } public void run() { try{ for (int i=1; i<=4; i++) { Thread.currentThread().sleep(3); //sleep for a random time, between 0-3 seconds for example; sharedBuf.set(i); } } catch (InterruptedException exception) { exception.printStackTrace(); } System.out.println("Producer finished, exiting..."); } }
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.nio.*; public class Consumer implements Runnable{ private Buffer sharedBuf; // the buffer is the same buffer used by the producer int sum = 0; // total stuff read from the buffer //..constructor should accept(Buffer), and set it to the sharedBuf ... public Consumer (Buffer sharedBuf){ this.sharedBuf=sharedBuf; } public void run() { try{ // read the buffer four times, and keep the total for (int i=1; i<=4; i++) { Thread.currentThread().sleep(3); sum = sum + sharedBuf.get(); } } catch(InterruptedException exception) { exception.printStackTrace(); } System.out.println("Consumer finished, ate " + sum + "exiting..."); } }
Java Syntax (Toggle Plain Text)
public class test { public static void main(String args[]) { Buffer shared = new Buffer(); Thread t=new Thread( new Producer(shared)); t.start(); Thread b=new Thread( new Consumer(shared)); b.start(); } }
my threads are working properly, but, i dont think that they're doing what they are supposed to be, because the consumer is eating 10, and it shouldnt be......can you give me a clue to why it keeps doing that?
thanks once again
itchap
what do you THINK it should do?
You have of course not created a consumer/producer system here at all.
You just have 2 threads where one sets a value and the other reads it from an object that's accessible to both.
If you get the same output each time that's possible but not certain at all.
You have of course not created a consumer/producer system here at all.
You just have 2 threads where one sets a value and the other reads it from an object that's accessible to both.
If you get the same output each time that's possible but not certain at all.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Help needed for IE6/Netscape problems (Web Browsers)
- MSN and Hotmail Login Problems (Web Browsers)
- Playing with shared memory - problem with non 'simple' data types (C++)
- only sees 2gig of HD - Did all fixes ? Large disk Enabled (Troubleshooting Dead Machines)
- PThreads: question on basic problem (or POSIX Threads) (C)
- javax.comm write error (Java)
- about:blank again sorry tried what ive read and nothing (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Java Help!!
- Next Thread: How is colon : used in for{} loop?
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






