944,065 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1735
  • Java RSS
Dec 22nd, 2006
0

problem with threads :(

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.nio.*;
  3.  
  4. public class Buffer {
  5. private int buffer = -1;
  6.  
  7. public void set(int val)
  8. {
  9. System.out.println("Writing" + val);
  10. buffer = val;
  11. }
  12.  
  13. public int get()
  14. {
  15. System.out.println("Reading" + buffer);
  16. return buffer;
  17. }
  18. }


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.lang.*;
  3. import java.nio.*;
  4.  
  5. public class Consumer extends Thread implements Runnable{
  6.  
  7. private Buffer sharedBuf; // the buffer is the same buffer used by the producer
  8. int sum = 0; // total stuff read from the buffer
  9.  
  10.  
  11. //..constructor should accept(Buffer), and set it to the sharedBuf ...
  12.  
  13. public Consumer (Buffer sharedBuf){
  14. this.sharedBuf=sharedBuf;
  15. }
  16.  
  17. public void threadRun() throws InterruptedException {
  18. try{
  19. // read the buffer four times, and keep the total
  20. for (int i=1; i<=4; i++)
  21. {
  22. sleep(1);
  23. sum = sum + sharedBuf.get();
  24. }
  25. }
  26.  
  27. catch(InterruptedException exception) {
  28. exception.printStackTrace();
  29. }
  30.  
  31. System.out.println("Consumer finished, ate " + sum + "exiting...");
  32.  
  33. }
  34. }

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.lang.*;
  3. import java.nio.*;
  4.  
  5. public class Producer extends Thread implements Runnable{
  6.  
  7. private Buffer sharedBuf; // the buffer is the buffer class above
  8. // constructor should accept (Buffer), and set it to the sharedBuf ...
  9. public Producer (Buffer sharedBuf){
  10. this.sharedBuf=sharedBuf;
  11. }
  12. public void threadRun()throws InterruptedException
  13. {
  14. try{
  15. for (int i=1; i<=4; i++)
  16. {
  17. sleep(1);//sleep for a random time, between 0-3 seconds for example;
  18. sharedBuf.set(i);
  19. }
  20. }
  21.  
  22. catch (InterruptedException exception) {
  23. exception.printStackTrace();
  24. }
  25.  
  26. System.out.println("Producer finished, exiting...");
  27. }
  28. }


Java Syntax (Toggle Plain Text)
  1.  
  2. public class test {
  3. public static void main(String args[]) {
  4. Buffer shared = new Buffer();
  5. Thread a=new Thread( new Producer(shared));
  6. a.start();
  7. Thread b=new Thread( new Consumer(shared));
  8. b.start();
  9. }
  10. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
itchap is offline Offline
43 posts
since Dec 2006
Dec 22nd, 2006
0

Re: problem with threads :(

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 22nd, 2006
0

Re: problem with threads :(

Thanks jwenting ....yeah your right, I fixed the code to this:


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.nio.*;
  3. public class Buffer {
  4. private int buffer = -1;
  5.  
  6. public void set(int val)
  7. {
  8. System.out.println("Writing" + val);
  9. buffer = val;
  10. }
  11.  
  12. public int get()
  13. {
  14. System.out.println("Reading" + buffer);
  15. return buffer;
  16. }
  17. }


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.lang.*;
  3. import java.nio.*;
  4.  
  5. public class Producer implements Runnable{
  6.  
  7. private Buffer sharedBuf; // the buffer is the buffer class above
  8. // constructor should accept (Buffer), and set it to the sharedBuf ...
  9. public Producer (Buffer sharedBuf){
  10. this.sharedBuf=sharedBuf;
  11. }
  12. public void run()
  13. {
  14. try{
  15. for (int i=1; i<=4; i++)
  16. {
  17. Thread.currentThread().sleep(3); //sleep for a random time, between 0-3 seconds for example;
  18. sharedBuf.set(i);
  19. }
  20. }
  21.  
  22. catch (InterruptedException exception) {
  23. exception.printStackTrace();
  24. }
  25.  
  26. System.out.println("Producer finished, exiting...");
  27. }
  28. }


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.lang.*;
  3. import java.nio.*;
  4.  
  5. public class Consumer implements Runnable{
  6.  
  7. private Buffer sharedBuf; // the buffer is the same buffer used by the producer
  8. int sum = 0; // total stuff read from the buffer
  9.  
  10.  
  11. //..constructor should accept(Buffer), and set it to the sharedBuf ...
  12.  
  13. public Consumer (Buffer sharedBuf){
  14. this.sharedBuf=sharedBuf;
  15. }
  16.  
  17. public void run() {
  18. try{
  19. // read the buffer four times, and keep the total
  20. for (int i=1; i<=4; i++)
  21. {
  22. Thread.currentThread().sleep(3);
  23. sum = sum + sharedBuf.get();
  24. }
  25. }
  26.  
  27. catch(InterruptedException exception) {
  28. exception.printStackTrace();
  29. }
  30.  
  31. System.out.println("Consumer finished, ate " + sum + "exiting...");
  32.  
  33. }
  34. }


Java Syntax (Toggle Plain Text)
  1.  
  2. public class test {
  3. public static void main(String args[]) {
  4. Buffer shared = new Buffer();
  5. Thread t=new Thread( new Producer(shared));
  6. t.start();
  7. Thread b=new Thread( new Consumer(shared));
  8. b.start();
  9. }
  10. }

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
Reputation Points: 10
Solved Threads: 0
Light Poster
itchap is offline Offline
43 posts
since Dec 2006
Dec 22nd, 2006
0

Re: problem with threads :(

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Help!!
Next Thread in Java Forum Timeline: How is colon : used in for{} loop?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC